blob: f0e6e22d88ae8890b0a1ffd992fd016d0579134e [file] [log] [blame]
Jean-Marc Valin92518982008-03-13 17:20:08 +11001#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include "mathops.h"
6#include <stdio.h>
7#include <math.h>
8
9#ifdef FIXED_POINT
10#define WORD "%d"
11#else
12#define WORD "%f"
13#endif
14
Gregory Maxwell7c422652009-07-01 03:50:44 -040015#ifdef FIXED_DEBUG
16long long celt_mips=0;
17#endif
Jean-Marc Valin92518982008-03-13 17:20:08 +110018int ret = 0;
19
Jean-Marc Valina82dfdd2008-03-13 23:01:55 +110020void testdiv(void)
Jean-Marc Valin92518982008-03-13 17:20:08 +110021{
22 celt_int32_t i;
Jean-Marc Valin385795e2008-03-26 15:56:07 +110023 for (i=1;i<=327670;i++)
Jean-Marc Valin92518982008-03-13 17:20:08 +110024 {
25 double prod;
26 celt_word32_t val;
Jean-Marc Valin92518982008-03-13 17:20:08 +110027 val = celt_rcp(i);
28#ifdef FIXED_POINT
29 prod = (1./32768./65526.)*val*i;
30#else
31 prod = val*i;
32#endif
33 if (fabs(prod-1) > .001)
34 {
35 fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
36 ret = 1;
37 }
38 }
39}
40
Jean-Marc Valina82dfdd2008-03-13 23:01:55 +110041void testsqrt(void)
Jean-Marc Valin92518982008-03-13 17:20:08 +110042{
43 celt_int32_t i;
44 for (i=1;i<=1000000000;i++)
45 {
46 double ratio;
47 celt_word16_t val;
48 val = celt_sqrt(i);
49 ratio = val/sqrt(i);
50 if (fabs(ratio - 1) > .001 && fabs(val-sqrt(i)) > 2)
51 {
52 fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
53 ret = 1;
54 }
55 i+= i>>10;
56 }
57}
58
Jean-Marc Valin189acec2008-03-26 16:42:42 +110059void testrsqrt(void)
60{
61 celt_int32_t i;
62 for (i=1;i<=2000000;i++)
63 {
64 double ratio;
65 celt_word16_t val;
66 val = celt_rsqrt(i);
Jean-Marc Valin5a7d9b22008-04-05 14:36:05 +100067 ratio = val*sqrt(i)/Q15ONE;
Jean-Marc Valin189acec2008-03-26 16:42:42 +110068 if (fabs(ratio - 1) > .05)
69 {
Jean-Marc Valin5a7d9b22008-04-05 14:36:05 +100070 fprintf (stderr, "rsqrt failed: rsqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
Jean-Marc Valin189acec2008-03-26 16:42:42 +110071 ret = 1;
72 }
73 i+= i>>10;
74 }
75}
76
Gregory Maxwellb92dce32009-06-28 19:51:30 -040077#ifndef FIXED_POINT
78void testlog2(void)
79{
80 float x;
81 for (x=0.001;x<1677700.0;x+=(x/8.0))
82 {
83 float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
84 if (error>0.001)
85 {
86 fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
87 ret = 1;
88 }
89 }
90}
91
92void testexp2(void)
93{
94 float x;
95 for (x=-11.0;x<24.0;x+=0.0007)
96 {
97 float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
98 if (error>0.0005)
99 {
100 fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
101 ret = 1;
102 }
103 }
104}
105
106void testexp2log2(void)
107{
108 float x;
109 for (x=-11.0;x<24.0;x+=0.0007)
110 {
111 float error = fabs(x-(celt_log2(celt_exp2(x))));
112 if (error>0.001)
113 {
114 fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
115 ret = 1;
116 }
117 }
118}
119#else
120void testilog2(void)
121{
122 celt_word32_t x;
123 for (x=1;x<=268435455;x+=127)
124 {
125 celt_word32_t error = abs(celt_ilog2(x)-(int)floor(log2(x)));
126 if (error!=0)
127 {
128 printf("celt_ilog2 failed: celt_ilog2(x)!=floor(log2(x)) (x = %d, error = %d)\n",x,error);
129 ret = 1;
130 }
131 }
132}
133#endif
134
Jean-Marc Valina82dfdd2008-03-13 23:01:55 +1100135int main(void)
Jean-Marc Valin92518982008-03-13 17:20:08 +1100136{
137 testdiv();
138 testsqrt();
Jean-Marc Valin189acec2008-03-26 16:42:42 +1100139 testrsqrt();
Gregory Maxwellb92dce32009-06-28 19:51:30 -0400140#ifndef FIXED_POINT
141 testlog2();
142 testexp2();
143 testexp2log2();
144#else
145 testilog2();
146#endif
Jean-Marc Valin385795e2008-03-26 15:56:07 +1100147 return ret;
Jean-Marc Valin92518982008-03-13 17:20:08 +1100148}