pseudofloat.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Copyright (C) 2005 Jean-Marc Valin */
  2. /**
  3. @file pseudofloat.h
  4. @brief Pseudo-floating point
  5. * This header file provides a lightweight floating point type for
  6. * use on fixed-point platforms when a large dynamic range is
  7. * required. The new type is not compatible with the 32-bit IEEE format,
  8. * it is not even remotely as accurate as 32-bit floats, and is not
  9. * even guaranteed to produce even remotely correct results for code
  10. * other than Speex. It makes all kinds of shortcuts that are acceptable
  11. * for Speex, but may not be acceptable for your application. You're
  12. * quite welcome to reuse this code and improve it, but don't assume
  13. * it works out of the box. Most likely, it doesn't.
  14. */
  15. /*
  16. Redistribution and use in source and binary forms, with or without
  17. modification, are permitted provided that the following conditions
  18. are met:
  19. - Redistributions of source code must retain the above copyright
  20. notice, this list of conditions and the following disclaimer.
  21. - Redistributions in binary form must reproduce the above copyright
  22. notice, this list of conditions and the following disclaimer in the
  23. documentation and/or other materials provided with the distribution.
  24. - Neither the name of the Xiph.org Foundation nor the names of its
  25. contributors may be used to endorse or promote products derived from
  26. this software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #ifndef PSEUDOFLOAT_H
  40. #define PSEUDOFLOAT_H
  41. #include "arch.h"
  42. #include "os_support.h"
  43. #include "math_approx.h"
  44. #include <math.h>
  45. #ifdef FIXED_POINT
  46. typedef struct {
  47. spx_int16_t m;
  48. spx_int16_t e;
  49. } spx_float_t;
  50. static const spx_float_t FLOAT_ZERO = {0,0};
  51. static const spx_float_t FLOAT_ONE = {16384,-14};
  52. static const spx_float_t FLOAT_HALF = {16384,-15};
  53. #define MIN(a,b) ((a)<(b)?(a):(b))
  54. static inline spx_float_t PSEUDOFLOAT(spx_int32_t x)
  55. {
  56. int e=0;
  57. int sign=0;
  58. if (x<0)
  59. {
  60. sign = 1;
  61. x = -x;
  62. }
  63. if (x==0)
  64. {
  65. spx_float_t r = {0,0};
  66. return r;
  67. }
  68. e = spx_ilog2(ABS32(x))-14;
  69. x = VSHR32(x, e);
  70. if (sign)
  71. {
  72. spx_float_t r;
  73. r.m = -x;
  74. r.e = e;
  75. return r;
  76. }
  77. else
  78. {
  79. spx_float_t r;
  80. r.m = x;
  81. r.e = e;
  82. return r;
  83. }
  84. }
  85. static inline spx_float_t FLOAT_ADD(spx_float_t a, spx_float_t b)
  86. {
  87. spx_float_t r;
  88. if (a.m==0)
  89. return b;
  90. else if (b.m==0)
  91. return a;
  92. if ((a).e > (b).e)
  93. {
  94. r.m = ((a).m>>1) + ((b).m>>MIN(15,(a).e-(b).e+1));
  95. r.e = (a).e+1;
  96. }
  97. else
  98. {
  99. r.m = ((b).m>>1) + ((a).m>>MIN(15,(b).e-(a).e+1));
  100. r.e = (b).e+1;
  101. }
  102. if (r.m>0)
  103. {
  104. if (r.m<16384)
  105. {
  106. r.m<<=1;
  107. r.e-=1;
  108. }
  109. } else {
  110. if (r.m>-16384)
  111. {
  112. r.m<<=1;
  113. r.e-=1;
  114. }
  115. }
  116. /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/
  117. return r;
  118. }
  119. static inline spx_float_t FLOAT_SUB(spx_float_t a, spx_float_t b)
  120. {
  121. spx_float_t r;
  122. if (a.m==0)
  123. return b;
  124. else if (b.m==0)
  125. return a;
  126. if ((a).e > (b).e)
  127. {
  128. r.m = ((a).m>>1) - ((b).m>>MIN(15,(a).e-(b).e+1));
  129. r.e = (a).e+1;
  130. }
  131. else
  132. {
  133. r.m = ((a).m>>MIN(15,(b).e-(a).e+1)) - ((b).m>>1);
  134. r.e = (b).e+1;
  135. }
  136. if (r.m>0)
  137. {
  138. if (r.m<16384)
  139. {
  140. r.m<<=1;
  141. r.e-=1;
  142. }
  143. } else {
  144. if (r.m>-16384)
  145. {
  146. r.m<<=1;
  147. r.e-=1;
  148. }
  149. }
  150. /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/
  151. return r;
  152. }
  153. static inline int FLOAT_LT(spx_float_t a, spx_float_t b)
  154. {
  155. if (a.m==0)
  156. return b.m>0;
  157. else if (b.m==0)
  158. return a.m<0;
  159. if ((a).e > (b).e)
  160. return ((a).m>>1) < ((b).m>>MIN(15,(a).e-(b).e+1));
  161. else
  162. return ((b).m>>1) > ((a).m>>MIN(15,(b).e-(a).e+1));
  163. }
  164. static inline int FLOAT_GT(spx_float_t a, spx_float_t b)
  165. {
  166. return FLOAT_LT(b,a);
  167. }
  168. static inline spx_float_t FLOAT_MULT(spx_float_t a, spx_float_t b)
  169. {
  170. spx_float_t r;
  171. r.m = (spx_int16_t)((spx_int32_t)(a).m*(b).m>>15);
  172. r.e = (a).e+(b).e+15;
  173. if (r.m>0)
  174. {
  175. if (r.m<16384)
  176. {
  177. r.m<<=1;
  178. r.e-=1;
  179. }
  180. } else {
  181. if (r.m>-16384)
  182. {
  183. r.m<<=1;
  184. r.e-=1;
  185. }
  186. }
  187. /*printf ("%f * %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/
  188. return r;
  189. }
  190. static inline spx_float_t FLOAT_AMULT(spx_float_t a, spx_float_t b)
  191. {
  192. spx_float_t r;
  193. r.m = (spx_int16_t)((spx_int32_t)(a).m*(b).m>>15);
  194. r.e = (a).e+(b).e+15;
  195. return r;
  196. }
  197. static inline spx_float_t FLOAT_SHL(spx_float_t a, int b)
  198. {
  199. spx_float_t r;
  200. r.m = a.m;
  201. r.e = a.e+b;
  202. return r;
  203. }
  204. static inline spx_int16_t FLOAT_EXTRACT16(spx_float_t a)
  205. {
  206. if (a.e<0)
  207. return EXTRACT16((EXTEND32(a.m)+(EXTEND32(1)<<(-a.e-1)))>>-a.e);
  208. else
  209. return a.m<<a.e;
  210. }
  211. static inline spx_int32_t FLOAT_EXTRACT32(spx_float_t a)
  212. {
  213. if (a.e<0)
  214. return (EXTEND32(a.m)+(EXTEND32(1)<<(-a.e-1)))>>-a.e;
  215. else
  216. return EXTEND32(a.m)<<a.e;
  217. }
  218. static inline spx_int32_t FLOAT_MUL32(spx_float_t a, spx_word32_t b)
  219. {
  220. return VSHR32(MULT16_32_Q15(a.m, b),-a.e-15);
  221. }
  222. static inline spx_float_t FLOAT_MUL32U(spx_word32_t a, spx_word32_t b)
  223. {
  224. int e1, e2;
  225. spx_float_t r;
  226. if (a==0 || b==0)
  227. {
  228. return FLOAT_ZERO;
  229. }
  230. e1 = spx_ilog2(ABS32(a));
  231. a = VSHR32(a, e1-14);
  232. e2 = spx_ilog2(ABS32(b));
  233. b = VSHR32(b, e2-14);
  234. r.m = MULT16_16_Q15(a,b);
  235. r.e = e1+e2-13;
  236. return r;
  237. }
  238. /* Do NOT attempt to divide by a negative number */
  239. static inline spx_float_t FLOAT_DIV32_FLOAT(spx_word32_t a, spx_float_t b)
  240. {
  241. int e=0;
  242. spx_float_t r;
  243. if (a==0)
  244. {
  245. return FLOAT_ZERO;
  246. }
  247. e = spx_ilog2(ABS32(a))-spx_ilog2(b.m-1)-15;
  248. a = VSHR32(a, e);
  249. if (ABS32(a)>=SHL32(EXTEND32(b.m-1),15))
  250. {
  251. a >>= 1;
  252. e++;
  253. }
  254. r.m = DIV32_16(a,b.m);
  255. r.e = e-b.e;
  256. return r;
  257. }
  258. /* Do NOT attempt to divide by a negative number */
  259. static inline spx_float_t FLOAT_DIV32(spx_word32_t a, spx_word32_t b)
  260. {
  261. int e0=0,e=0;
  262. spx_float_t r;
  263. if (a==0)
  264. {
  265. return FLOAT_ZERO;
  266. }
  267. if (b>32767)
  268. {
  269. e0 = spx_ilog2(b)-14;
  270. b = VSHR32(b, e0);
  271. e0 = -e0;
  272. }
  273. e = spx_ilog2(ABS32(a))-spx_ilog2(b-1)-15;
  274. a = VSHR32(a, e);
  275. if (ABS32(a)>=SHL32(EXTEND32(b-1),15))
  276. {
  277. a >>= 1;
  278. e++;
  279. }
  280. e += e0;
  281. r.m = DIV32_16(a,b);
  282. r.e = e;
  283. return r;
  284. }
  285. /* Do NOT attempt to divide by a negative number */
  286. static inline spx_float_t FLOAT_DIVU(spx_float_t a, spx_float_t b)
  287. {
  288. int e=0;
  289. spx_int32_t num;
  290. spx_float_t r;
  291. if (b.m<=0)
  292. {
  293. speex_warning_int("Attempted to divide by", b.m);
  294. return FLOAT_ONE;
  295. }
  296. num = a.m;
  297. a.m = ABS16(a.m);
  298. while (a.m >= b.m)
  299. {
  300. e++;
  301. a.m >>= 1;
  302. }
  303. num = num << (15-e);
  304. r.m = DIV32_16(num,b.m);
  305. r.e = a.e-b.e-15+e;
  306. return r;
  307. }
  308. static inline spx_float_t FLOAT_SQRT(spx_float_t a)
  309. {
  310. spx_float_t r;
  311. spx_int32_t m;
  312. m = SHL32(EXTEND32(a.m), 14);
  313. r.e = a.e - 14;
  314. if (r.e & 1)
  315. {
  316. r.e -= 1;
  317. m <<= 1;
  318. }
  319. r.e >>= 1;
  320. r.m = spx_sqrt(m);
  321. return r;
  322. }
  323. #else
  324. #define spx_float_t float
  325. #define FLOAT_ZERO 0.f
  326. #define FLOAT_ONE 1.f
  327. #define FLOAT_HALF 0.5f
  328. #define PSEUDOFLOAT(x) (x)
  329. #define FLOAT_MULT(a,b) ((a)*(b))
  330. #define FLOAT_AMULT(a,b) ((a)*(b))
  331. #define FLOAT_MUL32(a,b) ((a)*(b))
  332. #define FLOAT_DIV32(a,b) ((a)/(b))
  333. #define FLOAT_EXTRACT16(a) (a)
  334. #define FLOAT_EXTRACT32(a) (a)
  335. #define FLOAT_ADD(a,b) ((a)+(b))
  336. #define FLOAT_SUB(a,b) ((a)-(b))
  337. #define REALFLOAT(x) (x)
  338. #define FLOAT_DIV32_FLOAT(a,b) ((a)/(b))
  339. #define FLOAT_MUL32U(a,b) ((a)*(b))
  340. #define FLOAT_SHL(a,b) (a)
  341. #define FLOAT_LT(a,b) ((a)<(b))
  342. #define FLOAT_GT(a,b) ((a)>(b))
  343. #define FLOAT_DIVU(a,b) ((a)/(b))
  344. #define FLOAT_SQRT(a) (spx_sqrt(a))
  345. #endif
  346. #endif