summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_fp.h
blob: 7a169a20f5cb7ae5682877653a10c298b1fbcb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* EINA - EFL data type library
 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
 * Copyright (C) 2009 Cedric BAIL
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_FP_H_
# define EINA_FP_H_

/**
 * @addtogroup Eina_Fp_Group Fp
 *
 * @brief Floating point numbers data type management.
 */

/**
 * @addtogroup Eina_Data_Types_Group Data Types
 *
 * @{
 */

/**
 * @defgroup Eina_Fp_Group Fp
 *
 * @{
 */

#include "eina_types.h"

#ifdef _MSC_VER
typedef unsigned __int64 uint64_t;
typedef signed __int64   int64_t;
typedef signed int       int32_t;
#else
# include <stdint.h>
#endif

/**
 * @def EINA_F32P32_PI
 * @brief Yields the 32-bit PI constant
 */
#define EINA_F32P32_PI 0x00000003243f6a89

/**
 * @typedef Eina_F32p32
 * Type for floating point number where the size of the integer part is 32-bit
 * and the size of the decimal part is 32-bit
 */
typedef int64_t Eina_F32p32;

/**
 * @typedef Eina_F16p16
 * Type for floating point number where the size of the integer part is 16-bit
 * and the size of the decimal part is 16-bit
 */
typedef int32_t Eina_F16p16;

/**
 * @typedef Eina_F8p24
 * Type for floating point number where the size of the integer part is 8-bit
 * and the size of the decimal part is 24bits
 */
typedef int32_t Eina_F8p24;

/**
 * @brief Create a new Eina_F32p32 floating point number from standard 32-bit
 * integer
 *
 * @param v 32-bit integer value to convert
 * @return The value converted into Eina_F32p32 format
 */
static inline Eina_F32p32  eina_f32p32_int_from(int32_t v);

/**
 * @brief Create a new standard 32-bit integer from Eina_F32p32 floating point
 * number
 *
 * @param v Eina_F32p32 value to convert
 * @return The value converted into 32-bit integer
 */
static inline int32_t      eina_f32p32_int_to(Eina_F32p32 v);

/**
 * @brief Create a new Eina_F32p32 floating point number from standard double
 *
 * @param v double value to convert
 * @return The value converted into Eina_F32p32 format
 */
static inline Eina_F32p32  eina_f32p32_double_from(double v);

/**
 * @brief Create a new standard double from Eina_F32p32 floating point
 * number
 *
 * @param v Eina_F32p32 value to convert
 * @return The value converted into double
 */
static inline double       eina_f32p32_double_to(Eina_F32p32 v);

/**
 * @brief Calculates the sum of two Eina_F32p32 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The sum result of the two numbers @p a + @p b
 */
static inline Eina_F32p32  eina_f32p32_add(Eina_F32p32 a, Eina_F32p32 b);

/**
 * @brief Calculates the substraction of two Eina_F32p32 floating point numbers
 *
 * @param a The first number
 * @param b The substracted number
 * @return The substaction result of the two numbers @p a - @p b
 */
static inline Eina_F32p32  eina_f32p32_sub(Eina_F32p32 a, Eina_F32p32 b);

/**
 * @brief Calculates the multiplication of two Eina_F32p32 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The mutliplication result of the two numbers @p a * @p b
 *
 * To prevent overflow during multiplication we need to reduce the precision of
 * the fraction part Shift both values to only contain 16 bit of the fraction
 * part (rounded).  After multiplication we again have a value with a 32-bit
 * fraction part.
 */
static inline Eina_F32p32  eina_f32p32_mul(Eina_F32p32 a, Eina_F32p32 b);

/**
 * @brief Calculates the scale multiplication of one Eina_F32p32 floating point
 * number with an integer 
 *
 * @param a The Eina_F32p32 number
 * @param b The integer value
 * @return The mutliplication result of the two numbers @p a * @p b
 */
static inline Eina_F32p32  eina_f32p32_scale(Eina_F32p32 a, int b);

/**
 * @brief Calculates the division of two Eina_F32p32 floating point numbers
 *
 * @param a The numerator number
 * @param b The denominator number
 * @return The division result of the two numbers @p a / @p b
 */
static inline Eina_F32p32  eina_f32p32_div(Eina_F32p32 a, Eina_F32p32 b);

/**
 * @brief Calculates the square root of an Eina_F32p32 floating point number
 *
 * @param a The number to calculate the square root from
 * @return The square root result for the number @p a
 */
static inline Eina_F32p32  eina_f32p32_sqrt(Eina_F32p32 a);

/**
 * @brief Get the absolute value of the integer part of and Eina_F32p32 floating
 * point number
 *
 * @param a The floating point number
 * @return The positive integer part of the number @p a
 */
static inline unsigned int eina_f32p32_fracc_get(Eina_F32p32 v);

/**
 * @brief Get the absolute value of an Eina_F32p32 floating point number
 *
 * @param a The floating point number
 * @return The absolute value for the number @p a
 * @warning Has known issues on 64-bit architecture, prefer
 * eina_f32p32_fracc_get() instead
 */
#define eina_fp32p32_llabs(a) ((a < 0) ? -(a) : (a))

/**
 * @brief Calculates the cosinus of a floating point number
 *
 * @param a The angle in radians to calculate the cosinus from.
 * @return The cosinus value of the angle @p a
 */
EAPI Eina_F32p32           eina_f32p32_cos(Eina_F32p32 a);

/**
 * @brief Calculates the sinus of a floating point number
 *
 * @param a The angle in radians to calculate the sinus from.
 * @return The cosinus value of the angle @p a
 */
EAPI Eina_F32p32           eina_f32p32_sin(Eina_F32p32 a);


/**
 * @def EINA_F16P16_ONE
 *
 * Yields the maximum 16-bit unsigned integer size (= 65536)
 */
#define EINA_F16P16_ONE (1 << 16)

/**
 * @def EINA_F16P16_HALF
 *
 * Yields the maximum 16-bit signed integer size (= 32768)
 */
#define EINA_F16P16_HALF (1 << 15)

/**
 * @brief Create a new Eina_F16p316 floating point number from standard 32-bit
 * integer
 *
 * @param v 32-bit integer value to convert
 * @return The value converted into Eina_F16p16 format
 */
static inline Eina_F16p16  eina_f16p16_int_from(int32_t v);

/**
 * @brief Create a new standard 32-bit integer from Eina_F16p16 floating point
 * number 
 *
 * @param v Eina_F16p16 value to convert
 * @return The value converted into 32-bit integer
 */
static inline int32_t      eina_f16p16_int_to(Eina_F16p16 v);

/**
 * @brief Create a new Eina_F16p16 floating point number from standard double
 *
 * @param v double value to convert
 * @return The value converted into Eina_F16p16 format
 */
static inline Eina_F16p16  eina_f16p16_double_from(double v);

/**
 * @brief Create a new standard double from Eina_F16p16 floating point
 * number 
 *
 * @param v Eina_F16p16 value to convert
 * @return The value converted into double
 */
static inline double       eina_f16p16_double_to(Eina_F16p16 v);

/**
 * @brief Create a new Eina_F16p16 floating point number from standard float
 *
 * @param v float value to convert
 * @return The value converted into Eina_F16p16 format
 */
static inline Eina_F16p16  eina_f16p16_float_from(float v);

/**
 * @brief Create a new standard float from Eina_F16p16 floating point
 * number
 *
 * @param v Eina_F16p16 value to convert
 * @return The value converted into float
 */
static inline float        eina_f16p16_float_to(Eina_F16p16 v);

/**
 * @brief Calculates the sum of two Eina_F16p16 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The sum result of the two numbers @p a + @p b
 */
static inline Eina_F16p16  eina_f16p16_add(Eina_F16p16 a, Eina_F16p16 b);

/**
 * @brief Calculates the substraction of two Eina_F16p16 floating point numbers
 *
 * @param a The first number
 * @param b The substracted number
 * @return The substaction result of the two numbers @p a - @p b
 */
static inline Eina_F16p16  eina_f16p16_sub(Eina_F16p16 a, Eina_F16p16 b);

/**
 * @brief Calculates the multiplication of two Eina_F16p16 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The mutliplication result of the two numbers @p a * @p b
 */
static inline Eina_F16p16  eina_f16p16_mul(Eina_F16p16 a, Eina_F16p16 b);

/**
 * @brief Calculates the scale multiplication of one Eina_F16p16 floating point
 * number with an integer
 *
 * @param a The Eina_F16p16 number
 * @param b The integer value
 * @return The mutliplication result of the two numbers @p a * @p b
 */
static inline Eina_F16p16  eina_f16p16_scale(Eina_F16p16 a, int b);

/**
 * @brief Calculates the division of two Eina_F16p16 floating point numbers
 *
 * @param a The numerator number
 * @param b The denominator number
 * @return The division result of the two numbers @p a / @p b
 */
static inline Eina_F16p16  eina_f16p16_div(Eina_F16p16 a, Eina_F16p16 b);

/**
 * @brief Calculates the square root of an Eina_F16p16 floating point number
 *
 * @param a The number to calculate the square root from
 * @return The square root result for the number @p a
 */
static inline Eina_F16p16  eina_f16p16_sqrt(Eina_F16p16 a);

/**
 * @brief Get the absolute value of the integer part of and Eina_F16p16 floating
 * point number
 *
 * @param a The floating point number
 * @return The positive integer part of the number @p a
 */
static inline unsigned int eina_f16p16_fracc_get(Eina_F16p16 v);


/**
 * @brief Create a new Eina_F16p316 floating point number from standard 32-bit
 * integer
 *
 * @param v 32-bit integer value to convert
 * @return The value converted into Eina_F8p24 format
 */
static inline Eina_F8p24   eina_f8p24_int_from(int32_t v);

/**
 * @brief Create a new standard 32-bit integer from Eina_F8p24 floating point
 * number
 *
 * @param v Eina_F8p24 value to convert
 * @return The value converted into 32-bit integer
 */
static inline int32_t      eina_f8p24_int_to(Eina_F8p24 v);

/**
 * @brief Create a new Eina_F8p24 floating point number from standard float
 *
 * @param v float value to convert
 * @return The value converted into Eina_F8p24 format
 */
static inline Eina_F8p24   eina_f8p24_float_from(float v);

/**
 * @brief Create a new standard float from Eina_F8p24 floating point number
 *
 * @param v Eina_F8p24 value to convert
 * @return The value converted into float
 */
static inline float        eina_f8p24_float_to(Eina_F8p24 v);

/**
 * @brief Calculates the sum of two Eina_F8p24 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The sum result of the two numbers @p a + @p b
 */
static inline Eina_F8p24   eina_f8p24_add(Eina_F8p24 a, Eina_F8p24 b);

/**
 * @brief Calculates the substraction of two Eina_F8p24 floating point numbers
 *
 * @param a The first number
 * @param b The substracted number
 * @return The substaction result of the two numbers @p a - @p b
 */
static inline Eina_F8p24   eina_f8p24_sub(Eina_F8p24 a, Eina_F8p24 b);

/**
 * @brief Calculates the multiplication of two Eina_F8p24 floating point numbers
 *
 * @param a The first number
 * @param b The second number
 * @return The mutliplication result of the two numbers @p a * @p b
 */
static inline Eina_F8p24   eina_f8p24_mul(Eina_F8p24 a, Eina_F8p24 b);

/**
 * @brief Calculates the scale multiplication of one Eina_F8p24 floating point
 * number with an integer
 *
 * @param a The Eina_F16p16 number
 * @param b The integer value
 * @return The mutliplication result of the two numbers @p a * @p b
 */
static inline Eina_F8p24   eina_f8p24_scale(Eina_F8p24 a, int b);

/**
 * @brief Calculates the division of two Eina_F8p24 floating point numbers
 *
 * @param a The numerator number
 * @param b The denominator number
 * @return The division result of the two numbers @p a / @p b
 */
static inline Eina_F8p24   eina_f8p24_div(Eina_F8p24 a, Eina_F8p24 b);

/**
 * @brief Calculates the square root of an Eina_F8p24 floating point number
 *
 * @param a The number to calculate the square root from
 * @return The square root result for the number @p a
 */
static inline Eina_F8p24   eina_f8p24_sqrt(Eina_F8p24 a);

/**
 * @brief Get the absolute value of the integer part of and Eina_F8p24 floating
 * point number
 *
 * @param a The floating point number
 * @return The positive integer part of the number @p a
 */
static inline unsigned int eina_f8p24_fracc_get(Eina_F8p24 v);

/**
 * @brief Converts an Eina_F16p16 floating point number into Eina_F32p32 format
 *
 * @param a The Eina_F16p16 floating point number
 * @return The converted Eina_F32p32 floating point number
 */
static inline Eina_F32p32  eina_f16p16_to_f32p32(Eina_F16p16 a);

/**
 * @brief Converts an Eina_F8p24 floating point number into Eina_F32p32 format
 *
 * @param a The Eina_F8p24 floating point number
 * @return The converted Eina_F32p32 floating point number
 */
static inline Eina_F32p32  eina_f8p24_to_f32p32(Eina_F8p24 a);

/**
 * @brief Converts an Eina_F32p32 floating point number into Eina_F16p16 format
 *
 * @param a The Eina_F32p32 floating point number
 * @return The converted Eina_F16p16 floating point number
 */
static inline Eina_F16p16  eina_f32p32_to_f16p16(Eina_F32p32 a);

/**
 * @brief Converts an Eina_F8p24 floating point number into Eina_F16p16 format
 *
 * @param a The Eina_F8p24 floating point number
 * @return The converted Eina_F16p16 floating point number
 */
static inline Eina_F16p16  eina_f8p24_to_f16p16(Eina_F8p24 a);

/**
 * @brief Converts an Eina_F32p32 floating point number into Eina_F8p24 format
 *
 * @param a The Eina_F32p32 floating point number
 * @return The converted Eina_F8p16 floating point number
 */
static inline Eina_F8p24   eina_f32p32_to_f8p24(Eina_F32p32 a);

/**
 * @brief Converts an Eina_F16p16 floating point number into Eina_F8p16 format
 *
 * @param a The Eina_F16p16 floating point number
 * @return The converted Eina_F8p16 floating point number
 */
static inline Eina_F8p24   eina_f16p16_to_f8p24(Eina_F16p16 a);

#include "eina_inline_f32p32.x"
#include "eina_inline_f16p16.x"
#include "eina_inline_f8p24.x"
#include "eina_inline_fp.x"

/**
 * @}
 */

/**
 * @}
 */

#endif