softfloat.h 8.5 KB

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