1 /* EINA - EFL data type library 2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga 3 * Copyright (C) 2009 Cedric BAIL 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; 17 * if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef EINA_FP_H_ 21 # define EINA_FP_H_ 22 23 /** 24 * @addtogroup Eina_Fp_Group Fp 25 * 26 * @brief Floating point numbers data type management. 27 */ 28 29 /** 30 * @addtogroup Eina_Data_Types_Group Data Types 31 * 32 * @{ 33 */ 34 35 /** 36 * @defgroup Eina_Fp_Group Fp 37 * 38 * @{ 39 */ 40 41 #include "eina_types.h" 42 43 #ifdef _MSC_VER 44 typedef unsigned __int64 uint64_t; 45 typedef signed __int64 int64_t; 46 typedef signed int int32_t; 47 #else 48 # include <stdint.h> 49 #endif 50 51 /** 52 * @def EINA_F32P32_PI 53 * @brief Yields the 32-bit PI constant 54 */ 55 #define EINA_F32P32_PI 0x00000003243f6a89 56 57 /** 58 * @typedef Eina_F32p32 59 * Type for floating point number where the size of the integer part is 32-bit 60 * and the size of the decimal part is 32-bit 61 */ 62 typedef int64_t Eina_F32p32; 63 64 /** 65 * @typedef Eina_F16p16 66 * Type for floating point number where the size of the integer part is 16-bit 67 * and the size of the decimal part is 16-bit 68 */ 69 typedef int32_t Eina_F16p16; 70 71 /** 72 * @typedef Eina_F8p24 73 * Type for floating point number where the size of the integer part is 8-bit 74 * and the size of the decimal part is 24bits 75 */ 76 typedef int32_t Eina_F8p24; 77 78 /** 79 * @brief Creates a new Eina_F32p32 floating point number from standard 32-bit 80 * integer 81 * 82 * @param[in] v 32-bit integer value to convert 83 * @return The value converted into Eina_F32p32 format 84 */ 85 static inline Eina_F32p32 eina_f32p32_int_from(int32_t v); 86 87 /** 88 * @brief Creates a new standard 32-bit integer from Eina_F32p32 floating point 89 * number 90 * 91 * @param[in] v Eina_F32p32 value to convert 92 * @return The value converted into 32-bit integer 93 */ 94 static inline int32_t eina_f32p32_int_to(Eina_F32p32 v); 95 96 /** 97 * @brief Creates a new Eina_F32p32 floating point number from standard double 98 * 99 * @param[in] v Double value to convert 100 * @return The value converted into Eina_F32p32 format 101 */ 102 static inline Eina_F32p32 eina_f32p32_double_from(double v); 103 104 /** 105 * @brief Creates a new standard double from Eina_F32p32 floating point 106 * number 107 * 108 * @param[in] v Eina_F32p32 value to convert 109 * @return The value converted into double 110 */ 111 static inline double eina_f32p32_double_to(Eina_F32p32 v); 112 113 /** 114 * @brief Calculates the sum of two Eina_F32p32 floating point numbers 115 * 116 * @param[in] a The first number 117 * @param[in] b The second number 118 * @return The sum result of the two numbers @p a + @p b 119 */ 120 static inline Eina_F32p32 eina_f32p32_add(Eina_F32p32 a, Eina_F32p32 b); 121 122 /** 123 * @brief Calculates the subtraction of two Eina_F32p32 floating point numbers 124 * 125 * @param[in] a The first number 126 * @param[in] b The subtracted number 127 * @return The subtraction result of the two numbers @p a - @p b 128 */ 129 static inline Eina_F32p32 eina_f32p32_sub(Eina_F32p32 a, Eina_F32p32 b); 130 131 /** 132 * @brief Calculates the multiplication of two Eina_F32p32 floating point numbers 133 * 134 * @param[in] a The first number 135 * @param[in] b The second number 136 * @return The multiplication result of the two numbers @p a * @p b 137 * 138 * To prevent overflow during multiplication we need to reduce the precision of 139 * the fraction part Shift both values to only contain 16 bit of the fraction 140 * part (rounded). After multiplication we again have a value with a 32-bit 141 * fraction part. 142 */ 143 static inline Eina_F32p32 eina_f32p32_mul(Eina_F32p32 a, Eina_F32p32 b); 144 145 /** 146 * @brief Calculates the scale multiplication of one Eina_F32p32 floating point 147 * number with an integer 148 * 149 * @param[in] a The Eina_F32p32 number 150 * @param[in] b The integer value 151 * @return The multiplication result of the two numbers @p a * @p b 152 */ 153 static inline Eina_F32p32 eina_f32p32_scale(Eina_F32p32 a, int b); 154 155 /** 156 * @brief Calculates the division of two Eina_F32p32 floating point numbers 157 * 158 * @param[in] a The numerator number 159 * @param[in] b The denominator number 160 * @return The division result of the two numbers @p a / @p b 161 */ 162 static inline Eina_F32p32 eina_f32p32_div(Eina_F32p32 a, Eina_F32p32 b); 163 164 /** 165 * @brief Calculates the square root of an Eina_F32p32 floating point number 166 * 167 * @param[in] a The number to calculate the square root from 168 * @return The square root result for the number @p a 169 */ 170 static inline Eina_F32p32 eina_f32p32_sqrt(Eina_F32p32 a); 171 172 /** 173 * @brief Gets the absolute value of the integer part of and Eina_F32p32 floating 174 * point number 175 * 176 * @param[in] v The floating point number 177 * @return The positive integer part of the number @p v 178 */ 179 static inline unsigned int eina_f32p32_fracc_get(Eina_F32p32 v); 180 181 /** 182 * @brief Gets the absolute value of an Eina_F32p32 floating point number 183 * 184 * @param[in] a The floating point number 185 * @return The absolute value for the number @p a 186 * @warning Has known issues on 64-bit architecture, prefer 187 * eina_f32p32_fracc_get() instead 188 */ 189 #define eina_fp32p32_llabs(a) ((a < 0) ? -(a) : (a)) 190 191 /** 192 * @brief Calculates the cosine of a floating point number 193 * 194 * @param[in] a The angle in radians to calculate the cosine from. 195 * @return The cosine of the angle @p a 196 */ 197 EAPI Eina_F32p32 eina_f32p32_cos(Eina_F32p32 a); 198 199 /** 200 * @brief Calculates the sine of a floating point number 201 * 202 * @param[in] a The angle in radians to calculate the sine from. 203 * @return The cosine of the angle @p a 204 */ 205 EAPI Eina_F32p32 eina_f32p32_sin(Eina_F32p32 a); 206 207 208 /** 209 * @def EINA_F16P16_ONE 210 * 211 * Yields the maximum 16-bit unsigned integer size (= 65536) 212 */ 213 #define EINA_F16P16_ONE (1 << 16) 214 215 /** 216 * @def EINA_F16P16_HALF 217 * 218 * Yields the maximum 16-bit signed integer size (= 32768) 219 */ 220 #define EINA_F16P16_HALF (1 << 15) 221 222 /** 223 * @brief Creates a new Eina_F16p316 floating point number from standard 32-bit 224 * integer 225 * 226 * @param[in] v 32-bit integer value to convert 227 * @return The value converted into Eina_F16p16 format 228 */ 229 static inline Eina_F16p16 eina_f16p16_int_from(int32_t v); 230 231 /** 232 * @brief Creates a new standard 32-bit integer from Eina_F16p16 floating point 233 * number 234 * 235 * @param[in] v Eina_F16p16 value to convert 236 * @return The value converted into 32-bit integer 237 */ 238 static inline int32_t eina_f16p16_int_to(Eina_F16p16 v); 239 240 /** 241 * @brief Creates a new Eina_F16p16 floating point number from standard double 242 * 243 * @param[in] v Double value to convert 244 * @return The value converted into Eina_F16p16 format 245 */ 246 static inline Eina_F16p16 eina_f16p16_double_from(double v); 247 248 /** 249 * @brief Creates a new standard double from Eina_F16p16 floating point 250 * number 251 * 252 * @param[in] v Eina_F16p16 value to convert 253 * @return The value converted into double 254 */ 255 static inline double eina_f16p16_double_to(Eina_F16p16 v); 256 257 /** 258 * @brief Creates a new Eina_F16p16 floating point number from standard float 259 * 260 * @param[in] v Float value to convert 261 * @return The value converted into Eina_F16p16 format 262 */ 263 static inline Eina_F16p16 eina_f16p16_float_from(float v); 264 265 /** 266 * @brief Creates a new standard float from Eina_F16p16 floating point 267 * number 268 * 269 * @param[in] v Eina_F16p16 value to convert 270 * @return The value converted into float 271 */ 272 static inline float eina_f16p16_float_to(Eina_F16p16 v); 273 274 /** 275 * @brief Calculates the sum of two Eina_F16p16 floating point numbers 276 * 277 * @param[in] a The first number 278 * @param[in] b The second number 279 * @return The sum result of the two numbers @p a + @p b 280 */ 281 static inline Eina_F16p16 eina_f16p16_add(Eina_F16p16 a, Eina_F16p16 b); 282 283 /** 284 * @brief Calculates the subtraction of two Eina_F16p16 floating point numbers 285 * 286 * @param[in] a The first number 287 * @param[in] b The subtracted number 288 * @return The subtraction result of the two numbers @p a - @p b 289 */ 290 static inline Eina_F16p16 eina_f16p16_sub(Eina_F16p16 a, Eina_F16p16 b); 291 292 /** 293 * @brief Calculates the multiplication of two Eina_F16p16 floating point numbers 294 * 295 * @param[in] a The first number 296 * @param[in] b The second number 297 * @return The multiplication result of the two numbers @p a * @p b 298 */ 299 static inline Eina_F16p16 eina_f16p16_mul(Eina_F16p16 a, Eina_F16p16 b); 300 301 /** 302 * @brief Calculates the scale multiplication of one Eina_F16p16 floating point 303 * number with an integer 304 * 305 * @param[in] a The Eina_F16p16 number 306 * @param[in] b The integer value 307 * @return The multiplication result of the two numbers @p a * @p b 308 */ 309 static inline Eina_F16p16 eina_f16p16_scale(Eina_F16p16 a, int b); 310 311 /** 312 * @brief Calculates the division of two Eina_F16p16 floating point numbers 313 * 314 * @param[in] a The numerator number 315 * @param[in] b The denominator number 316 * @return The division result of the two numbers @p a / @p b 317 */ 318 static inline Eina_F16p16 eina_f16p16_div(Eina_F16p16 a, Eina_F16p16 b); 319 320 /** 321 * @brief Calculates the square root of an Eina_F16p16 floating point number 322 * 323 * @param[in] a The number to calculate the square root from 324 * @return The square root result for the number @p a 325 */ 326 static inline Eina_F16p16 eina_f16p16_sqrt(Eina_F16p16 a); 327 328 /** 329 * @brief Gets the absolute value of the integer part of and Eina_F16p16 floating 330 * point number 331 * 332 * @param[in] v The floating point number 333 * @return The positive integer part of the number @p v 334 */ 335 static inline unsigned int eina_f16p16_fracc_get(Eina_F16p16 v); 336 337 338 /** 339 * @brief Creates a new Eina_F16p316 floating point number from standard 32-bit 340 * integer 341 * 342 * @param[in] v 32-bit integer value to convert 343 * @return The value converted into Eina_F8p24 format 344 */ 345 static inline Eina_F8p24 eina_f8p24_int_from(int32_t v); 346 347 /** 348 * @brief Creates a new standard 32-bit integer from Eina_F8p24 floating point 349 * number 350 * 351 * @param[in] v Eina_F8p24 value to convert 352 * @return The value converted into 32-bit integer 353 */ 354 static inline int32_t eina_f8p24_int_to(Eina_F8p24 v); 355 356 /** 357 * @brief Creates a new Eina_F8p24 floating point number from standard float 358 * 359 * @param[in] v Float value to convert 360 * @return The value converted into Eina_F8p24 format 361 */ 362 static inline Eina_F8p24 eina_f8p24_float_from(float v); 363 364 /** 365 * @brief Create a new standard float from Eina_F8p24 floating point number 366 * 367 * @param[in] v Eina_F8p24 value to convert 368 * @return The value converted into float 369 */ 370 static inline float eina_f8p24_float_to(Eina_F8p24 v); 371 372 /** 373 * @brief Calculates the sum of two Eina_F8p24 floating point numbers 374 * 375 * @param[in] a The first number 376 * @param[in] b The second number 377 * @return The sum result of the two numbers @p a + @p b 378 */ 379 static inline Eina_F8p24 eina_f8p24_add(Eina_F8p24 a, Eina_F8p24 b); 380 381 /** 382 * @brief Calculates the subtraction of two Eina_F8p24 floating point numbers 383 * 384 * @param[in] a The first number 385 * @param[in] b The subtracted number 386 * @return The subtraction result of the two numbers @p a - @p b 387 */ 388 static inline Eina_F8p24 eina_f8p24_sub(Eina_F8p24 a, Eina_F8p24 b); 389 390 /** 391 * @brief Calculates the multiplication of two Eina_F8p24 floating point numbers 392 * 393 * @param[in] a The first number 394 * @param[in] b The second number 395 * @return The multiplication result of the two numbers @p a * @p b 396 */ 397 static inline Eina_F8p24 eina_f8p24_mul(Eina_F8p24 a, Eina_F8p24 b); 398 399 /** 400 * @brief Calculates the scale multiplication of one Eina_F8p24 floating point 401 * number with an integer 402 * 403 * @param[in] a The Eina_F16p16 number 404 * @param[in] b The integer value 405 * @return The multiplication result of the two numbers @p a * @p b 406 */ 407 static inline Eina_F8p24 eina_f8p24_scale(Eina_F8p24 a, int b); 408 409 /** 410 * @brief Calculates the division of two Eina_F8p24 floating point numbers 411 * 412 * @param[in] a The numerator number 413 * @param[in] b The denominator number 414 * @return The division result of the two numbers @p a / @p b 415 */ 416 static inline Eina_F8p24 eina_f8p24_div(Eina_F8p24 a, Eina_F8p24 b); 417 418 /** 419 * @brief Calculates the square root of an Eina_F8p24 floating point number 420 * 421 * @param[in] a The number to calculate the square root from 422 * @return The square root result for the number @p a 423 */ 424 static inline Eina_F8p24 eina_f8p24_sqrt(Eina_F8p24 a); 425 426 /** 427 * @brief Gets the absolute value of the integer part of and Eina_F8p24 floating 428 * point number 429 * 430 * @param[in] v The floating point number 431 * @return The positive integer part of the number @p v 432 */ 433 static inline unsigned int eina_f8p24_fracc_get(Eina_F8p24 v); 434 435 /** 436 * @brief Converts an Eina_F16p16 floating point number into Eina_F32p32 format 437 * 438 * @param[in] a The Eina_F16p16 floating point number 439 * @return The converted Eina_F32p32 floating point number 440 */ 441 static inline Eina_F32p32 eina_f16p16_to_f32p32(Eina_F16p16 a); 442 443 /** 444 * @brief Converts an Eina_F8p24 floating point number into Eina_F32p32 format 445 * 446 * @param[in] a The Eina_F8p24 floating point number 447 * @return The converted Eina_F32p32 floating point number 448 */ 449 static inline Eina_F32p32 eina_f8p24_to_f32p32(Eina_F8p24 a); 450 451 /** 452 * @brief Converts an Eina_F32p32 floating point number into Eina_F16p16 format 453 * 454 * @param[in] a The Eina_F32p32 floating point number 455 * @return The converted Eina_F16p16 floating point number 456 */ 457 static inline Eina_F16p16 eina_f32p32_to_f16p16(Eina_F32p32 a); 458 459 /** 460 * @brief Converts an Eina_F8p24 floating point number into Eina_F16p16 format 461 * 462 * @param[in] a The Eina_F8p24 floating point number 463 * @return The converted Eina_F16p16 floating point number 464 */ 465 static inline Eina_F16p16 eina_f8p24_to_f16p16(Eina_F8p24 a); 466 467 /** 468 * @brief Converts an Eina_F32p32 floating point number into Eina_F8p24 format 469 * 470 * @param[in] a The Eina_F32p32 floating point number 471 * @return The converted Eina_F8p16 floating point number 472 */ 473 static inline Eina_F8p24 eina_f32p32_to_f8p24(Eina_F32p32 a); 474 475 /** 476 * @brief Converts an Eina_F16p16 floating point number into Eina_F8p16 format 477 * 478 * @param[in] a The Eina_F16p16 floating point number 479 * @return The converted Eina_F8p16 floating point number 480 */ 481 static inline Eina_F8p24 eina_f16p16_to_f8p24(Eina_F16p16 a); 482 483 #include "eina_inline_f32p32.x" 484 #include "eina_inline_f16p16.x" 485 #include "eina_inline_f8p24.x" 486 #include "eina_inline_fp.x" 487 488 /** 489 * @} 490 */ 491 492 /** 493 * @} 494 */ 495 496 #endif 497