annotate src/opus-1.3/celt/tests/test_unit_mathops.c @ 83:ae30d91d2ffe

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