test_unit_mathops.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Copyright (c) 2008-2011 Xiph.Org Foundation, Mozilla Corporation,
  2. Gregory Maxwell
  3. Written by Jean-Marc Valin, Gregory Maxwell, and Timothy B. Terriberry */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #ifndef CUSTOM_MODES
  29. #define CUSTOM_MODES
  30. #endif
  31. #define CELT_C
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include "mathops.c"
  35. #include "entenc.c"
  36. #include "entdec.c"
  37. #include "entcode.c"
  38. #include "bands.c"
  39. #include "quant_bands.c"
  40. #include "laplace.c"
  41. #include "vq.c"
  42. #include "cwrs.c"
  43. #include "pitch.c"
  44. #include "celt_lpc.c"
  45. #include "celt.c"
  46. #if defined(OPUS_X86_MAY_HAVE_SSE) || defined(OPUS_X86_MAY_HAVE_SSE2) || defined(OPUS_X86_MAY_HAVE_SSE4_1)
  47. # if defined(OPUS_X86_MAY_HAVE_SSE)
  48. # include "x86/pitch_sse.c"
  49. # endif
  50. # if defined(OPUS_X86_MAY_HAVE_SSE2)
  51. # include "x86/pitch_sse2.c"
  52. # endif
  53. # if defined(OPUS_X86_MAY_HAVE_SSE4_1)
  54. # include "x86/pitch_sse4_1.c"
  55. # include "x86/celt_lpc_sse.c"
  56. # endif
  57. # include "x86/x86_celt_map.c"
  58. #elif defined(OPUS_ARM_ASM) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  59. # include "arm/armcpu.c"
  60. # if defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  61. # include "arm/celt_neon_intr.c"
  62. # if defined(HAVE_ARM_NE10)
  63. # include "kiss_fft.c"
  64. # include "mdct.c"
  65. # include "arm/celt_ne10_fft.c"
  66. # include "arm/celt_ne10_mdct.c"
  67. # endif
  68. # endif
  69. # include "arm/arm_celt_map.c"
  70. #endif
  71. #ifdef FIXED_POINT
  72. #define WORD "%d"
  73. #else
  74. #define WORD "%f"
  75. #endif
  76. int ret = 0;
  77. void testdiv(void)
  78. {
  79. opus_int32 i;
  80. for (i=1;i<=327670;i++)
  81. {
  82. double prod;
  83. opus_val32 val;
  84. val = celt_rcp(i);
  85. #ifdef FIXED_POINT
  86. prod = (1./32768./65526.)*val*i;
  87. #else
  88. prod = val*i;
  89. #endif
  90. if (fabs(prod-1) > .00025)
  91. {
  92. fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
  93. ret = 1;
  94. }
  95. }
  96. }
  97. void testsqrt(void)
  98. {
  99. opus_int32 i;
  100. for (i=1;i<=1000000000;i++)
  101. {
  102. double ratio;
  103. opus_val16 val;
  104. val = celt_sqrt(i);
  105. ratio = val/sqrt(i);
  106. if (fabs(ratio - 1) > .0005 && fabs(val-sqrt(i)) > 2)
  107. {
  108. fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
  109. ret = 1;
  110. }
  111. i+= i>>10;
  112. }
  113. }
  114. void testbitexactcos(void)
  115. {
  116. int i;
  117. opus_int32 min_d,max_d,last,chk;
  118. chk=max_d=0;
  119. last=min_d=32767;
  120. for(i=64;i<=16320;i++)
  121. {
  122. opus_int32 d;
  123. opus_int32 q=bitexact_cos(i);
  124. chk ^= q*i;
  125. d = last - q;
  126. if (d>max_d)max_d=d;
  127. if (d<min_d)min_d=d;
  128. last = q;
  129. }
  130. if ((chk!=89408644)||(max_d!=5)||(min_d!=0)||(bitexact_cos(64)!=32767)||
  131. (bitexact_cos(16320)!=200)||(bitexact_cos(8192)!=23171))
  132. {
  133. fprintf (stderr, "bitexact_cos failed\n");
  134. ret = 1;
  135. }
  136. }
  137. void testbitexactlog2tan(void)
  138. {
  139. int i,fail;
  140. opus_int32 min_d,max_d,last,chk;
  141. fail=chk=max_d=0;
  142. last=min_d=15059;
  143. for(i=64;i<8193;i++)
  144. {
  145. opus_int32 d;
  146. opus_int32 mid=bitexact_cos(i);
  147. opus_int32 side=bitexact_cos(16384-i);
  148. opus_int32 q=bitexact_log2tan(mid,side);
  149. chk ^= q*i;
  150. d = last - q;
  151. if (q!=-1*bitexact_log2tan(side,mid))
  152. fail = 1;
  153. if (d>max_d)max_d=d;
  154. if (d<min_d)min_d=d;
  155. last = q;
  156. }
  157. if ((chk!=15821257)||(max_d!=61)||(min_d!=-2)||fail||
  158. (bitexact_log2tan(32767,200)!=15059)||(bitexact_log2tan(30274,12540)!=2611)||
  159. (bitexact_log2tan(23171,23171)!=0))
  160. {
  161. fprintf (stderr, "bitexact_log2tan failed\n");
  162. ret = 1;
  163. }
  164. }
  165. #ifndef FIXED_POINT
  166. void testlog2(void)
  167. {
  168. float x;
  169. for (x=0.001;x<1677700.0;x+=(x/8.0))
  170. {
  171. float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
  172. if (error>0.0009)
  173. {
  174. fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
  175. ret = 1;
  176. }
  177. }
  178. }
  179. void testexp2(void)
  180. {
  181. float x;
  182. for (x=-11.0;x<24.0;x+=0.0007)
  183. {
  184. float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
  185. if (error>0.0002)
  186. {
  187. fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
  188. ret = 1;
  189. }
  190. }
  191. }
  192. void testexp2log2(void)
  193. {
  194. float x;
  195. for (x=-11.0;x<24.0;x+=0.0007)
  196. {
  197. float error = fabs(x-(celt_log2(celt_exp2(x))));
  198. if (error>0.001)
  199. {
  200. fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
  201. ret = 1;
  202. }
  203. }
  204. }
  205. #else
  206. void testlog2(void)
  207. {
  208. opus_val32 x;
  209. for (x=8;x<1073741824;x+=(x>>3))
  210. {
  211. float error = fabs((1.442695040888963387*log(x/16384.0))-celt_log2(x)/1024.0);
  212. if (error>0.003)
  213. {
  214. fprintf (stderr, "celt_log2 failed: x = %ld, error = %f\n", (long)x,error);
  215. ret = 1;
  216. }
  217. }
  218. }
  219. void testexp2(void)
  220. {
  221. opus_val16 x;
  222. for (x=-32768;x<15360;x++)
  223. {
  224. float error1 = fabs(x/1024.0-(1.442695040888963387*log(celt_exp2(x)/65536.0)));
  225. float error2 = fabs(exp(0.6931471805599453094*x/1024.0)-celt_exp2(x)/65536.0);
  226. if (error1>0.0002&&error2>0.00004)
  227. {
  228. fprintf (stderr, "celt_exp2 failed: x = "WORD", error1 = %f, error2 = %f\n", x,error1,error2);
  229. ret = 1;
  230. }
  231. }
  232. }
  233. void testexp2log2(void)
  234. {
  235. opus_val32 x;
  236. for (x=8;x<65536;x+=(x>>3))
  237. {
  238. float error = fabs(x-0.25*celt_exp2(celt_log2(x)))/16384;
  239. if (error>0.004)
  240. {
  241. fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_exp2(celt_log2(x))))>0.001 (x = %ld, error = %f)\n", (long)x,error);
  242. ret = 1;
  243. }
  244. }
  245. }
  246. void testilog2(void)
  247. {
  248. opus_val32 x;
  249. for (x=1;x<=268435455;x+=127)
  250. {
  251. opus_val32 lg;
  252. opus_val32 y;
  253. lg = celt_ilog2(x);
  254. if (lg<0 || lg>=31)
  255. {
  256. printf("celt_ilog2 failed: 0<=celt_ilog2(x)<31 (x = %d, celt_ilog2(x) = %d)\n",x,lg);
  257. ret = 1;
  258. }
  259. y = 1<<lg;
  260. if (x<y || (x>>1)>=y)
  261. {
  262. printf("celt_ilog2 failed: 2**celt_ilog2(x)<=x<2**(celt_ilog2(x)+1) (x = %d, 2**celt_ilog2(x) = %d)\n",x,y);
  263. ret = 1;
  264. }
  265. }
  266. }
  267. #endif
  268. int main(void)
  269. {
  270. testbitexactcos();
  271. testbitexactlog2tan();
  272. testdiv();
  273. testsqrt();
  274. testlog2();
  275. testexp2();
  276. testexp2log2();
  277. #ifdef FIXED_POINT
  278. testilog2();
  279. #endif
  280. return ret;
  281. }