You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.3KB

  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 -126
  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 av_const SoftFloat av_normalize_sf(SoftFloat a){
  34. if(a.mant){
  35. #if 1
  36. while((a.mant + 0x20000000U)<0x40000000U){
  37. a.mant += a.mant;
  38. a.exp -= 1;
  39. }
  40. #else
  41. int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
  42. a.exp -= s;
  43. a.mant <<= s;
  44. #endif
  45. if(a.exp < MIN_EXP){
  46. a.exp = MIN_EXP;
  47. a.mant= 0;
  48. }
  49. }else{
  50. a.exp= MIN_EXP;
  51. }
  52. return a;
  53. }
  54. static inline av_const SoftFloat av_normalize1_sf(SoftFloat a){
  55. #if 1
  56. if((int32_t)(a.mant + 0x40000000U) < 0){
  57. a.exp++;
  58. a.mant>>=1;
  59. }
  60. av_assert2(a.mant < 0x40000000 && a.mant > -0x40000000);
  61. return a;
  62. #elif 1
  63. int t= a.mant + 0x40000000 < 0;
  64. return (SoftFloat){ a.mant>>t, a.exp+t};
  65. #else
  66. int t= (a.mant + 0x40000000U)>>31;
  67. return (SoftFloat){a.mant>>t, a.exp+t};
  68. #endif
  69. }
  70. /**
  71. * @return Will not be more denormalized than a+b. So if either input is
  72. * normalized, then the output will not be worse then the other input.
  73. * If both are normalized, then the output will be normalized.
  74. */
  75. static inline av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
  76. a.exp += b.exp;
  77. av_assert2((int32_t)((a.mant * (int64_t)b.mant) >> ONE_BITS) == (a.mant * (int64_t)b.mant) >> ONE_BITS);
  78. a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
  79. return av_normalize1_sf((SoftFloat){a.mant, a.exp - 1});
  80. }
  81. /**
  82. * b has to be normalized and not zero.
  83. * @return Will not be more denormalized than a.
  84. */
  85. static av_const SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
  86. a.exp -= b.exp;
  87. a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
  88. return av_normalize1_sf(a);
  89. }
  90. static inline av_const int av_cmp_sf(SoftFloat a, SoftFloat b){
  91. int t= a.exp - b.exp;
  92. if(t<0) return (a.mant >> (-t)) - b.mant ;
  93. else return a.mant - (b.mant >> t);
  94. }
  95. static inline av_const int av_gt_sf(SoftFloat a, SoftFloat b)
  96. {
  97. int t= a.exp - b.exp;
  98. if(t<0) return (a.mant >> (-t)) > b.mant ;
  99. else return a.mant > (b.mant >> t);
  100. }
  101. static inline av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
  102. int t= a.exp - b.exp;
  103. if (t <-31) return b;
  104. else if (t < 0) return av_normalize_sf(av_normalize1_sf((SoftFloat){ b.mant + (a.mant >> (-t)), b.exp}));
  105. else if (t < 32) return av_normalize_sf(av_normalize1_sf((SoftFloat){ a.mant + (b.mant >> t ), a.exp}));
  106. else return a;
  107. }
  108. static inline av_const SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
  109. return av_add_sf(a, (SoftFloat){ -b.mant, b.exp});
  110. }
  111. //FIXME log, exp, pow
  112. /**
  113. * Converts a mantisse and exponent to a SoftFloat
  114. * @returns a SoftFloat with value v * 2^frac_bits
  115. */
  116. static inline av_const SoftFloat av_int2sf(int v, int frac_bits){
  117. return av_normalize_sf((SoftFloat){v, ONE_BITS + 1 - frac_bits});
  118. }
  119. /**
  120. * Rounding is to -inf.
  121. */
  122. static inline av_const int av_sf2int(SoftFloat v, int frac_bits){
  123. v.exp += frac_bits - (ONE_BITS + 1);
  124. if(v.exp >= 0) return v.mant << v.exp ;
  125. else return v.mant >>(-v.exp);
  126. }
  127. /**
  128. * Rounding-to-nearest used.
  129. */
  130. static av_always_inline SoftFloat av_sqrt_sf(SoftFloat val)
  131. {
  132. int tabIndex, rem;
  133. if (val.mant == 0)
  134. val.exp = 0;
  135. else
  136. {
  137. tabIndex = (val.mant - 0x20000000) >> 20;
  138. rem = val.mant & 0xFFFFF;
  139. val.mant = (int)(((int64_t)av_sqrttbl_sf[tabIndex] * (0x100000 - rem) +
  140. (int64_t)av_sqrttbl_sf[tabIndex + 1] * rem +
  141. 0x80000) >> 20);
  142. val.mant = (int)(((int64_t)av_sqr_exp_multbl_sf[val.exp & 1] * val.mant +
  143. 0x10000000) >> 29);
  144. if (val.mant < 0x40000000)
  145. val.exp -= 2;
  146. else
  147. val.mant >>= 1;
  148. val.exp = (val.exp >> 1) + 1;
  149. }
  150. return val;
  151. }
  152. /**
  153. * Rounding-to-nearest used.
  154. */
  155. static av_always_inline void av_sincos_sf(int a, int *s, int *c)
  156. {
  157. int idx, sign;
  158. int sv, cv;
  159. int st, ct;
  160. idx = a >> 26;
  161. sign = (idx << 27) >> 31;
  162. cv = av_costbl_1_sf[idx & 0xf];
  163. cv = (cv ^ sign) - sign;
  164. idx -= 8;
  165. sign = (idx << 27) >> 31;
  166. sv = av_costbl_1_sf[idx & 0xf];
  167. sv = (sv ^ sign) - sign;
  168. idx = a >> 21;
  169. ct = av_costbl_2_sf[idx & 0x1f];
  170. st = av_sintbl_2_sf[idx & 0x1f];
  171. idx = (int)(((int64_t)cv * ct - (int64_t)sv * st + 0x20000000) >> 30);
  172. sv = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  173. cv = idx;
  174. idx = a >> 16;
  175. ct = av_costbl_3_sf[idx & 0x1f];
  176. st = av_sintbl_3_sf[idx & 0x1f];
  177. idx = (int)(((int64_t)cv * ct - (int64_t)sv * st + 0x20000000) >> 30);
  178. sv = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  179. cv = idx;
  180. idx = a >> 11;
  181. ct = (int)(((int64_t)av_costbl_4_sf[idx & 0x1f] * (0x800 - (a & 0x7ff)) +
  182. (int64_t)av_costbl_4_sf[(idx & 0x1f)+1]*(a & 0x7ff) +
  183. 0x400) >> 11);
  184. st = (int)(((int64_t)av_sintbl_4_sf[idx & 0x1f] * (0x800 - (a & 0x7ff)) +
  185. (int64_t)av_sintbl_4_sf[(idx & 0x1f) + 1] * (a & 0x7ff) +
  186. 0x400) >> 11);
  187. *c = (int)(((int64_t)cv * ct + (int64_t)sv * st + 0x20000000) >> 30);
  188. *s = (int)(((int64_t)cv * st + (int64_t)sv * ct + 0x20000000) >> 30);
  189. }
  190. #endif /* AVUTIL_SOFTFLOAT_H */