softfloat_ieee754.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVUTIL_SOFTFLOAT_IEEE754_H
  21. #define AVUTIL_SOFTFLOAT_IEEE754_H
  22. #include <stdint.h>
  23. #define EXP_BIAS 127
  24. #define MANT_BITS 23
  25. typedef struct SoftFloat_IEEE754 {
  26. int32_t sign;
  27. uint64_t mant;
  28. int32_t exp;
  29. } SoftFloat_IEEE754;
  30. static const SoftFloat_IEEE754 FLOAT_0 = {0, 0, -126};
  31. static const SoftFloat_IEEE754 FLOAT_1 = {0, 0, 0};
  32. /** Normalize the softfloat as defined by IEEE 754 single-recision floating
  33. * point specification
  34. */
  35. static SoftFloat_IEEE754 av_normalize_sf_ieee754(SoftFloat_IEEE754 sf) {
  36. while( sf.mant >= 0x1000000UL ) {
  37. sf.exp++;
  38. sf.mant >>= 1;
  39. }
  40. sf.mant &= 0x007fffffUL;
  41. return sf;
  42. }
  43. /** Convert integer to softfloat.
  44. * @return softfloat with value n * 2^e
  45. */
  46. static SoftFloat_IEEE754 av_int2sf_ieee754(int64_t n, int e) {
  47. int sign = 0;
  48. if (n < 0) {
  49. sign = 1;
  50. n *= -1;
  51. }
  52. return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, n << MANT_BITS, 0 + e});
  53. }
  54. /** Make a softfloat out of the bitstream. Assumes the bits are in the form as defined
  55. * by the IEEE 754 spec.
  56. */
  57. static SoftFloat_IEEE754 av_bits2sf_ieee754(uint32_t n) {
  58. return ((SoftFloat_IEEE754) { (n & 0x80000000UL), (n & 0x7FFFFFUL), (n & 0x7F800000UL) });
  59. }
  60. /** Convert the softfloat to integer
  61. */
  62. static int av_sf2int_ieee754(SoftFloat_IEEE754 a) {
  63. if(a.exp >= 0) return a.mant << a.exp ;
  64. else return a.mant >>(-a.exp);
  65. }
  66. /** Divide a by b. b should not be zero.
  67. * @return normalized result
  68. */
  69. static SoftFloat_IEEE754 av_div_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) {
  70. int32_t mant, exp, sign;
  71. a = av_normalize_sf_ieee754(a);
  72. b = av_normalize_sf_ieee754(b);
  73. sign = a.sign ^ b.sign;
  74. mant = ((((uint64_t) (a.mant | 0x00800000UL)) << MANT_BITS) / (b.mant| 0x00800000UL));
  75. exp = a.exp - b.exp;
  76. return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp});
  77. }
  78. /** Multiply a with b
  79. * #return normalized result
  80. */
  81. static SoftFloat_IEEE754 av_mul_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) {
  82. int32_t sign, mant, exp;
  83. a = av_normalize_sf_ieee754(a);
  84. b = av_normalize_sf_ieee754(b);
  85. sign = a.sign ^ b.sign;
  86. mant = (((uint64_t)(a.mant|0x00800000UL) * (uint64_t)(b.mant|0x00800000UL))>>MANT_BITS);
  87. exp = a.exp + b.exp;
  88. return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp});
  89. }
  90. /** Compare a with b strictly
  91. * @returns 1 if the a and b are equal, 0 otherwise.
  92. */
  93. static int av_cmp_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) {
  94. a = av_normalize_sf_ieee754(a);
  95. b = av_normalize_sf_ieee754(b);
  96. if (a.sign != b.sign) return 0;
  97. if (a.mant != b.mant) return 0;
  98. if (a.exp != b.exp ) return 0;
  99. return 1;
  100. }
  101. #endif /*AVUTIL_SOFTFLOAT_IEEE754_H*/