2
0

softfloat.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  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_H
  21. #define AVUTIL_SOFTFLOAT_H
  22. #include <stdint.h>
  23. #include "common.h"
  24. #include "avassert.h"
  25. #include "softfloat_tables.h"
  26. #define MIN_EXP -149
  27. #define MAX_EXP 126
  28. #define ONE_BITS 29
  29. typedef struct SoftFloat{
  30. int32_t mant;
  31. int32_t exp;
  32. }SoftFloat;
  33. static const SoftFloat FLOAT_0 = { 0, MIN_EXP}; ///< 0.0
  34. static const SoftFloat FLOAT_05 = { 0x20000000, 0}; ///< 0.5
  35. static const SoftFloat FLOAT_1 = { 0x20000000, 1}; ///< 1.0
  36. static const SoftFloat FLOAT_EPSILON = { 0x29F16B12, -16}; ///< A small value
  37. static const SoftFloat FLOAT_1584893192 = { 0x32B771ED, 1}; ///< 1.584893192 (10^.2)
  38. static const SoftFloat FLOAT_100000 = { 0x30D40000, 17}; ///< 100000
  39. static const SoftFloat FLOAT_0999999 = { 0x3FFFFBCE, 0}; ///< 0.999999
  40. static const SoftFloat FLOAT_MIN = { 0x20000000, MIN_EXP};
  41. /**
  42. * Convert a SoftFloat to a double precision float.
  43. */
  44. static inline av_const double av_sf2double(SoftFloat v) {
  45. v.exp -= ONE_BITS +1;
  46. return ldexp(v.mant, v.exp);
  47. }
  48. static av_const SoftFloat av_normalize_sf(SoftFloat a){
  49. if(a.mant){
  50. #if 1
  51. while((a.mant + 0x1FFFFFFFU)<0x3FFFFFFFU){
  52. a.mant += a.mant;
  53. a.exp -= 1;
  54. }
  55. #else
  56. int s=ONE_BITS - av_log2(FFABS(a.mant));
  57. a.exp -= s;
  58. a.mant <<= s;
  59. #endif
  60. if(a.exp < MIN_EXP){
  61. a.exp = MIN_EXP;
  62. a.mant= 0;
  63. }
  64. }else{
  65. a.exp= MIN_EXP;
  66. }
  67. return a;
  68. }
  69. static inline av_const SoftFloat av_normalize1_sf(SoftFloat a){
  70. #if 1
  71. if((int32_t)(a.mant + 0x40000000U) <= 0){
  72. a.exp++;
  73. a.mant>>=1;
  74. }
  75. av_assert2(a.mant < 0x40000000 && a.mant > -0x40000000);
  76. av_assert2(a.exp <= MAX_EXP);
  77. return a;
  78. #elif 1
  79. int t= a.mant + 0x40000000 < 0;
  80. return (SoftFloat){ a.mant>>t, a.exp+t};
  81. #else
  82. int t= (a.mant + 0x3FFFFFFFU)>>31;
  83. return (SoftFloat){a.mant>>t, a.exp+t};
  84. #endif
  85. }
  86. /**
  87. * @return Will not be more denormalized than a*b. So if either input is
  88. * normalized, then the output will not be worse then the other input.
  89. * If both are normalized, then the output will be normalized.
  90. */
  91. static inline av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
  92. a.exp += b.exp;
  93. av_assert2((int32_t)((a.mant * (int64_t)b.mant) >> ONE_BITS) == (a.mant * (int64_t)b.mant) >> ONE_BITS);
  94. a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
  95. a = av_normalize1_sf((SoftFloat){a.mant, a.exp - 1});
  96. if (!a.mant || a.exp < MIN_EXP)
  97. return FLOAT_0;
  98. return a;
  99. }
  100. /**
  101. * b has to be normalized and not zero.
  102. * @return Will not be more denormalized than a.
  103. */
  104. static inline av_const SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
  105. int64_t temp = (int64_t)a.mant * (1<<(ONE_BITS+1));
  106. temp /= b.mant;
  107. a.exp -= b.exp;
  108. a.mant = temp;
  109. while (a.mant != temp) {
  110. temp /= 2;
  111. a.exp--;
  112. a.mant = temp;
  113. }
  114. a = av_normalize1_sf(a);
  115. if (!a.mant || a.exp < MIN_EXP)
  116. return FLOAT_0;
  117. return a;
  118. }
  119. /**
  120. * Compares two SoftFloats.
  121. * @returns < 0 if the first is less
  122. * > 0 if the first is greater
  123. * 0 if they are equal
  124. */
  125. static inline av_const int av_cmp_sf(SoftFloat a, SoftFloat b){
  126. int t= a.exp - b.exp;
  127. if (t <-31) return - b.mant ;
  128. else if (t < 0) return (a.mant >> (-t)) - b.mant ;
  129. else if (t < 32) return a.mant - (b.mant >> t);
  130. else return a.mant ;
  131. }
  132. /**
  133. * Compares two SoftFloats.
  134. * @returns 1 if a is greater than b, 0 otherwise
  135. */
  136. static inline av_const int av_gt_sf(SoftFloat a, SoftFloat b)
  137. {
  138. int t= a.exp - b.exp;
  139. if (t <-31) return 0 > b.mant ;
  140. else if (t < 0) return (a.mant >> (-t)) > b.mant ;
  141. else if (t < 32) return a.mant > (b.mant >> t);
  142. else return a.mant > 0 ;
  143. }
  144. /**
  145. * @returns the sum of 2 SoftFloats.
  146. */
  147. static inline av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
  148. int t= a.exp - b.exp;
  149. if (t <-31) return b;
  150. else if (t < 0) return av_normalize_sf(av_normalize1_sf((SoftFloat){ b.mant + (a.mant >> (-t)), b.exp}));
  151. else if (t < 32) return av_normalize_sf(av_normalize1_sf((SoftFloat){ a.mant + (b.mant >> t ), a.exp}));
  152. else return a;
  153. }
  154. /**
  155. * @returns the difference of 2 SoftFloats.
  156. */
  157. static inline av_const SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
  158. return av_add_sf(a, (SoftFloat){ -b.mant, b.exp});
  159. }
  160. //FIXME log, exp, pow
  161. /**
  162. * Converts a mantisse and exponent to a SoftFloat.
  163. * This converts a fixed point value v with frac_bits fractional bits to a
  164. * SoftFloat.
  165. * @returns a SoftFloat with value v * 2^-frac_bits
  166. */
  167. static inline av_const SoftFloat av_int2sf(int v, int frac_bits){
  168. int exp_offset = 0;
  169. if(v <= INT_MIN + 1){
  170. exp_offset = 1;
  171. v>>=1;
  172. }
  173. return av_normalize_sf(av_normalize1_sf((SoftFloat){v, ONE_BITS + 1 - frac_bits + exp_offset}));
  174. }
  175. /**
  176. * Converts a SoftFloat to an integer.
  177. * Rounding is to -inf.
  178. */
  179. static inline av_const int av_sf2int(SoftFloat v, int frac_bits){
  180. v.exp += frac_bits - (ONE_BITS + 1);
  181. if(v.exp >= 0) return v.mant << v.exp ;
  182. else return v.mant >>(-v.exp);
  183. }
  184. /**
  185. * Rounding-to-nearest used.
  186. */
  187. static av_always_inline SoftFloat av_sqrt_sf(SoftFloat val)
  188. {
  189. int tabIndex, rem;
  190. if (val.mant == 0)
  191. val.exp = MIN_EXP;
  192. else if (val.mant < 0)
  193. abort();
  194. else
  195. {
  196. tabIndex = (val.mant - 0x20000000) >> 20;
  197. rem = val.mant & 0xFFFFF;
  198. val.mant = (int)(((int64_t)av_sqrttbl_sf[tabIndex] * (0x100000 - rem) +
  199. (int64_t)av_sqrttbl_sf[tabIndex + 1] * rem +
  200. 0x80000) >> 20);
  201. val.mant = (int)(((int64_t)av_sqr_exp_multbl_sf[val.exp & 1] * val.mant +
  202. 0x10000000) >> 29);
  203. if (val.mant < 0x40000000)
  204. val.exp -= 2;
  205. else
  206. val.mant >>= 1;
  207. val.exp = (val.exp >> 1) + 1;
  208. }
  209. return val;
  210. }
  211. /**
  212. * Rounding-to-nearest used.
  213. */
  214. static av_unused void av_sincos_sf(int a, int *s, int *c)
  215. {
  216. int idx, sign;
  217. int sv, cv;
  218. int st, ct;
  219. idx = a >> 26;
  220. sign = (int32_t)((unsigned)idx << 27) >> 31;
  221. cv = av_costbl_1_sf[idx & 0xf];
  222. cv = (cv ^ sign) - sign;
  223. idx -= 8;
  224. sign = (int32_t)((unsigned)idx << 27) >> 31;
  225. sv = av_costbl_1_sf[idx & 0xf];
  226. sv = (sv ^ sign) - sign;
  227. idx = a >> 21;
  228. ct = av_costbl_2_sf[idx & 0x1f];
  229. st = av_sintbl_2_sf[idx & 0x1f];
  230. idx = (int)(((int64_t)cv * ct - (int64_t)sv * st + 0x20000000) >> 30);
  231. sv = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  232. cv = idx;
  233. idx = a >> 16;
  234. ct = av_costbl_3_sf[idx & 0x1f];
  235. st = av_sintbl_3_sf[idx & 0x1f];
  236. idx = (int)(((int64_t)cv * ct - (int64_t)sv * st + 0x20000000) >> 30);
  237. sv = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  238. cv = idx;
  239. idx = a >> 11;
  240. ct = (int)(((int64_t)av_costbl_4_sf[idx & 0x1f] * (0x800 - (a & 0x7ff)) +
  241. (int64_t)av_costbl_4_sf[(idx & 0x1f)+1]*(a & 0x7ff) +
  242. 0x400) >> 11);
  243. st = (int)(((int64_t)av_sintbl_4_sf[idx & 0x1f] * (0x800 - (a & 0x7ff)) +
  244. (int64_t)av_sintbl_4_sf[(idx & 0x1f) + 1] * (a & 0x7ff) +
  245. 0x400) >> 11);
  246. *c = (int)(((int64_t)cv * ct + (int64_t)sv * st + 0x20000000) >> 30);
  247. *s = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  248. }
  249. #endif /* AVUTIL_SOFTFLOAT_H */