mathops-test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #define CELT_C
  5. #include "mathops.c"
  6. #include "entenc.c"
  7. #include "entdec.c"
  8. #include "entcode.c"
  9. #include "bands.c"
  10. #include "vq.c"
  11. #include "cwrs.c"
  12. #include <stdio.h>
  13. #include <math.h>
  14. #ifdef FIXED_POINT
  15. #define WORD "%d"
  16. #else
  17. #define WORD "%f"
  18. #endif
  19. #ifdef FIXED_DEBUG
  20. long long celt_mips=0;
  21. #endif
  22. int ret = 0;
  23. void testdiv(void)
  24. {
  25. opus_int32 i;
  26. for (i=1;i<=327670;i++)
  27. {
  28. double prod;
  29. opus_val32 val;
  30. val = celt_rcp(i);
  31. #ifdef FIXED_POINT
  32. prod = (1./32768./65526.)*val*i;
  33. #else
  34. prod = val*i;
  35. #endif
  36. if (fabs(prod-1) > .00025)
  37. {
  38. fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
  39. ret = 1;
  40. }
  41. }
  42. }
  43. void testsqrt(void)
  44. {
  45. opus_int32 i;
  46. for (i=1;i<=1000000000;i++)
  47. {
  48. double ratio;
  49. opus_val16 val;
  50. val = celt_sqrt(i);
  51. ratio = val/sqrt(i);
  52. if (fabs(ratio - 1) > .0005 && fabs(val-sqrt(i)) > 2)
  53. {
  54. fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
  55. ret = 1;
  56. }
  57. i+= i>>10;
  58. }
  59. }
  60. void testbitexactcos(void)
  61. {
  62. int i;
  63. opus_int32 min_d,max_d,last,chk;
  64. chk=max_d=0;
  65. last=min_d=32767;
  66. for(i=64;i<=16320;i++)
  67. {
  68. opus_int32 d;
  69. opus_int32 q=bitexact_cos(i);
  70. chk ^= q*i;
  71. d = last - q;
  72. if (d>max_d)max_d=d;
  73. if (d<min_d)min_d=d;
  74. last = q;
  75. }
  76. if ((chk!=89408644)||(max_d!=5)||(min_d!=0)||(bitexact_cos(64)!=32767)||
  77. (bitexact_cos(16320)!=200)||(bitexact_cos(8192)!=23171))
  78. {
  79. fprintf (stderr, "bitexact_cos failed\n");
  80. ret = 1;
  81. }
  82. }
  83. void testbitexactlog2tan(void)
  84. {
  85. int i,fail;
  86. opus_int32 min_d,max_d,last,chk;
  87. fail=chk=max_d=0;
  88. last=min_d=15059;
  89. for(i=64;i<8193;i++)
  90. {
  91. opus_int32 d;
  92. opus_int32 mid=bitexact_cos(i);
  93. opus_int32 side=bitexact_cos(16384-i);
  94. opus_int32 q=bitexact_log2tan(mid,side);
  95. chk ^= q*i;
  96. d = last - q;
  97. if (q!=-1*bitexact_log2tan(side,mid))
  98. fail = 1;
  99. if (d>max_d)max_d=d;
  100. if (d<min_d)min_d=d;
  101. last = q;
  102. }
  103. if ((chk!=15821257)||(max_d!=61)||(min_d!=-2)||fail||
  104. (bitexact_log2tan(32767,200)!=15059)||(bitexact_log2tan(30274,12540)!=2611)||
  105. (bitexact_log2tan(23171,23171)!=0))
  106. {
  107. fprintf (stderr, "bitexact_log2tan failed\n");
  108. ret = 1;
  109. }
  110. }
  111. #ifndef FIXED_POINT
  112. void testlog2(void)
  113. {
  114. float x;
  115. for (x=0.001;x<1677700.0;x+=(x/8.0))
  116. {
  117. float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
  118. if (error>0.0009)
  119. {
  120. fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
  121. ret = 1;
  122. }
  123. }
  124. }
  125. void testexp2(void)
  126. {
  127. float x;
  128. for (x=-11.0;x<24.0;x+=0.0007)
  129. {
  130. float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
  131. if (error>0.0002)
  132. {
  133. fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
  134. ret = 1;
  135. }
  136. }
  137. }
  138. void testexp2log2(void)
  139. {
  140. float x;
  141. for (x=-11.0;x<24.0;x+=0.0007)
  142. {
  143. float error = fabs(x-(celt_log2(celt_exp2(x))));
  144. if (error>0.001)
  145. {
  146. fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
  147. ret = 1;
  148. }
  149. }
  150. }
  151. #else
  152. void testlog2(void)
  153. {
  154. opus_val32 x;
  155. for (x=8;x<1073741824;x+=(x>>3))
  156. {
  157. float error = fabs((1.442695040888963387*log(x/16384.0))-celt_log2(x)/1024.0);
  158. if (error>0.003)
  159. {
  160. fprintf (stderr, "celt_log2 failed: x = %ld, error = %f\n", (long)x,error);
  161. ret = 1;
  162. }
  163. }
  164. }
  165. void testexp2(void)
  166. {
  167. opus_val16 x;
  168. for (x=-32768;x<15360;x++)
  169. {
  170. float error1 = fabs(x/1024.0-(1.442695040888963387*log(celt_exp2(x)/65536.0)));
  171. float error2 = fabs(exp(0.6931471805599453094*x/1024.0)-celt_exp2(x)/65536.0);
  172. if (error1>0.0002&&error2>0.00004)
  173. {
  174. fprintf (stderr, "celt_exp2 failed: x = "WORD", error1 = %f, error2 = %f\n", x,error1,error2);
  175. ret = 1;
  176. }
  177. }
  178. }
  179. void testexp2log2(void)
  180. {
  181. opus_val32 x;
  182. for (x=8;x<65536;x+=(x>>3))
  183. {
  184. float error = fabs(x-0.25*celt_exp2(celt_log2(x)))/16384;
  185. if (error>0.004)
  186. {
  187. fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_exp2(celt_log2(x))))>0.001 (x = %ld, error = %f)\n", (long)x,error);
  188. ret = 1;
  189. }
  190. }
  191. }
  192. void testilog2(void)
  193. {
  194. opus_val32 x;
  195. for (x=1;x<=268435455;x+=127)
  196. {
  197. opus_val32 error = abs(celt_ilog2(x)-(int)floor(log2(x)));
  198. if (error!=0)
  199. {
  200. printf("celt_ilog2 failed: celt_ilog2(x)!=floor(log2(x)) (x = %d, error = %d)\n",x,error);
  201. ret = 1;
  202. }
  203. }
  204. }
  205. #endif
  206. int main(void)
  207. {
  208. testbitexactcos();
  209. testbitexactlog2tan();
  210. testdiv();
  211. testsqrt();
  212. testlog2();
  213. testexp2();
  214. testexp2log2();
  215. #ifdef FIXED_POINT
  216. testilog2();
  217. #endif
  218. return ret;
  219. }