1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mathops.h"
#include <stdio.h>
#include <math.h>
#ifdef FIXED_POINT
#define WORD "%d"
#else
#define WORD "%f"
#endif
#ifdef FIXED_DEBUG
long long celt_mips=0;
#endif
int ret = 0;
void testdiv(void)
{
celt_int32 i;
for (i=1;i<=327670;i++)
{
double prod;
celt_word32 val;
val = celt_rcp(i);
#ifdef FIXED_POINT
prod = (1./32768./65526.)*val*i;
#else
prod = val*i;
#endif
if (fabs(prod-1) > .001)
{
fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
ret = 1;
}
}
}
void testsqrt(void)
{
celt_int32 i;
for (i=1;i<=1000000000;i++)
{
double ratio;
celt_word16 val;
val = celt_sqrt(i);
ratio = val/sqrt(i);
if (fabs(ratio - 1) > .001 && fabs(val-sqrt(i)) > 2)
{
fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
ret = 1;
}
i+= i>>10;
}
}
void testrsqrt(void)
{
celt_int32 i;
for (i=1;i<=2000000;i++)
{
double ratio;
celt_word16 val;
val = celt_rsqrt(i);
ratio = val*sqrt(i)/Q15ONE;
if (fabs(ratio - 1) > .05)
{
fprintf (stderr, "rsqrt failed: rsqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
ret = 1;
}
i+= i>>10;
}
}
#ifndef FIXED_POINT
void testlog2(void)
{
float x;
for (x=0.001;x<1677700.0;x+=(x/8.0))
{
float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
if (error>0.001)
{
fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
ret = 1;
}
}
}
void testexp2(void)
{
float x;
for (x=-11.0;x<24.0;x+=0.0007)
{
float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
if (error>0.0005)
{
fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
ret = 1;
}
}
}
void testexp2log2(void)
{
float x;
for (x=-11.0;x<24.0;x+=0.0007)
{
float error = fabs(x-(celt_log2(celt_exp2(x))));
if (error>0.001)
{
fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
ret = 1;
}
}
}
#else
void testilog2(void)
{
celt_word32 x;
for (x=1;x<=268435455;x+=127)
{
celt_word32 error = abs(celt_ilog2(x)-(int)floor(log2(x)));
if (error!=0)
{
printf("celt_ilog2 failed: celt_ilog2(x)!=floor(log2(x)) (x = %d, error = %d)\n",x,error);
ret = 1;
}
}
}
#endif
int main(void)
{
testdiv();
testsqrt();
testrsqrt();
#ifndef FIXED_POINT
testlog2();
testexp2();
testexp2log2();
#else
testilog2();
#endif
return ret;
}
|