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.

121 lines
3.3KB

  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #define MIN_EXP -126
  20. #define MAX_EXP 126
  21. #define ONE_BITS 29
  22. typedef struct SoftFloat{
  23. int32_t exp;
  24. int32_t mant;
  25. }SoftFloat;
  26. static SoftFloat av_normalize_sf(SoftFloat a){
  27. if(a.mant){
  28. #if 1
  29. while((a.mant + 0x20000000U)<0x40000000U){
  30. a.mant += a.mant;
  31. a.exp -= 1;
  32. }
  33. #else
  34. int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
  35. a.exp -= s;
  36. a.mant <<= s;
  37. #endif
  38. if(a.exp < MIN_EXP){
  39. a.exp = MIN_EXP;
  40. a.mant= 0;
  41. }
  42. }else{
  43. a.exp= MIN_EXP;
  44. }
  45. return a;
  46. }
  47. static inline SoftFloat av_normalize1_sf(SoftFloat a){
  48. #if 1
  49. if(a.mant + 0x40000000 < 0){
  50. a.exp++;
  51. a.mant>>=1;
  52. }
  53. return a;
  54. #elif 1
  55. int t= a.mant + 0x40000000 < 0;
  56. return (SoftFloat){a.exp+t, a.mant>>t};
  57. #else
  58. int t= (a.mant + 0x40000000U)>>31;
  59. return (SoftFloat){a.exp+t, a.mant>>t};
  60. #endif
  61. }
  62. /**
  63. *
  64. * @return will not be more denormalized then a+b, so if either input is
  65. * normalized then the output wont be worse then the other input
  66. * if both are normalized then the output will be normalized
  67. */
  68. static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
  69. a.exp += b.exp;
  70. a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
  71. return av_normalize1_sf(a);
  72. }
  73. /**
  74. *
  75. * b has to be normalized and not zero
  76. * @return will not be more denormalized then a
  77. */
  78. static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
  79. a.exp -= b.exp+1;
  80. a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
  81. return av_normalize1_sf(a);
  82. }
  83. static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
  84. int t= a.exp - b.exp;
  85. if(t<0) return (a.mant >> (-t)) - b.mant ;
  86. else return a.mant - (b.mant >> t);
  87. }
  88. static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
  89. int t= a.exp - b.exp;
  90. if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
  91. else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
  92. }
  93. static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
  94. return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
  95. }
  96. //FIXME sqrt, log, exp, pow, sin, cos
  97. static inline SoftFloat av_int2sf(int v, int frac_bits){
  98. return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
  99. }
  100. /**
  101. *
  102. * rounding is to -inf
  103. */
  104. static inline int av_sf2int(SoftFloat v, int frac_bits){
  105. v.exp += frac_bits - ONE_BITS;
  106. if(v.exp >= 0) return v.mant << v.exp ;
  107. else return v.mant >>(-v.exp);
  108. }