11/* Copyright (c) 2008-2011 Xiph.Org Foundation, Mozilla Corporation,
22 Gregory Maxwell
3+ Copyright (c) 2024 Arm Limited
34 Written by Jean-Marc Valin, Gregory Maxwell, and Timothy B. Terriberry */
45/*
56 Redistribution and use in source and binary forms, with or without
3637
3738#include <stdio.h>
3839#include <math.h>
39- #include "mathops.h"
4040#include "bands.h"
41+ #include "cpu_support.h"
42+ #include "float_cast.h"
43+ #include "mathops.h"
4144
4245#ifdef FIXED_POINT
4346#define WORD "%d"
@@ -250,8 +253,94 @@ void testilog2(void)
250253}
251254#endif
252255
256+
257+ #ifndef DISABLE_FLOAT_API
258+
259+ void testcelt_float2int16 (int use_ref_impl , int buffer_size )
260+ {
261+
262+ #define MAX_BUFFER_SIZE 2080
263+ int i , cnt ;
264+ float floatsToConvert [MAX_BUFFER_SIZE ];
265+ short results [MAX_BUFFER_SIZE ] = { 0 };
266+ float scaleInt16RangeTo01 ;
267+
268+ celt_assert (buffer_size <= MAX_BUFFER_SIZE );
269+
270+ scaleInt16RangeTo01 = 1.f / 32768.f ;
271+ cnt = 0 ;
272+
273+ while (cnt + 15 < buffer_size && cnt < buffer_size / 2 )
274+ {
275+ floatsToConvert [cnt ++ ] = 77777.0f * scaleInt16RangeTo01 ;
276+ floatsToConvert [cnt ++ ] = 33000.0f * scaleInt16RangeTo01 ;
277+ floatsToConvert [cnt ++ ] = 32768.0f * scaleInt16RangeTo01 ;
278+ floatsToConvert [cnt ++ ] = 32767.4f * scaleInt16RangeTo01 ;
279+ floatsToConvert [cnt ++ ] = 32766.6f * scaleInt16RangeTo01 ;
280+ floatsToConvert [cnt ++ ] = .501 * scaleInt16RangeTo01 ;
281+ floatsToConvert [cnt ++ ] = .499f * scaleInt16RangeTo01 ;
282+ floatsToConvert [cnt ++ ] = .0f ;
283+ floatsToConvert [cnt ++ ] = -.499f * scaleInt16RangeTo01 ;
284+ floatsToConvert [cnt ++ ] = -.501f * scaleInt16RangeTo01 ;
285+ floatsToConvert [cnt ++ ] = -32767.6f * scaleInt16RangeTo01 ;
286+ floatsToConvert [cnt ++ ] = -32768.4f * scaleInt16RangeTo01 ;
287+ floatsToConvert [cnt ++ ] = -32769.0f * scaleInt16RangeTo01 ;
288+ floatsToConvert [cnt ++ ] = -33000.0f * scaleInt16RangeTo01 ;
289+ floatsToConvert [cnt ++ ] = -77777.0f * scaleInt16RangeTo01 ;
290+
291+ celt_assert (cnt < buffer_size );
292+ }
293+
294+ while (cnt < buffer_size )
295+ {
296+ float inInt16Range = cnt * 7 + .5 ;
297+ inInt16Range += (cnt & 0x01 ) ? .1 : -.1 ;
298+ inInt16Range *= (cnt & 0x02 ) ? 1 : -1 ;
299+ floatsToConvert [cnt ++ ] = inInt16Range * scaleInt16RangeTo01 ;
300+ }
301+
302+ for (i = 0 ; i < MAX_BUFFER_SIZE ; ++ i )
303+ {
304+ results [i ] = 42 ;
305+ }
306+
307+ if (use_ref_impl )
308+ {
309+ celt_float2int16_c (floatsToConvert , results , cnt );
310+ } else {
311+ celt_float2int16 (floatsToConvert , results , cnt , opus_select_arch ());
312+ }
313+
314+ for (i = 0 ; i < cnt ; ++ i )
315+ {
316+ const float expected = FLOAT2INT16 (floatsToConvert [i ]);
317+ if (results [i ] != expected )
318+ {
319+ fprintf (stderr , "testcelt_float2int16 failed: celt_float2int16 converted %f (index: %d) to %d (x*32768=%f, expected: %d, cnt: %d, ref: %d)\n" ,
320+ floatsToConvert [i ], i , (int )results [i ], floatsToConvert [i ] * 32768.0f , (int )expected , buffer_size , use_ref_impl );
321+ ret = 1 ;
322+ }
323+ }
324+
325+ for (i = cnt ; i < MAX_BUFFER_SIZE ; ++ i )
326+ {
327+ if (results [i ] != 42 )
328+ {
329+ fprintf (stderr , "testcelt_float2int16 failed: buffer overflow (cnt: %d, ref: %d)\n" , buffer_size , use_ref_impl );
330+ ret = 1 ;
331+ break ;
332+ }
333+ }
334+ #undef MAX_BUFFER_SIZE
335+ }
336+
337+ #endif
338+
253339int main (void )
254340{
341+ int i ;
342+ int use_ref_impl [2 ] = { 0 , 1 };
343+
255344 testbitexactcos ();
256345 testbitexactlog2tan ();
257346 testdiv ();
@@ -261,6 +350,15 @@ int main(void)
261350 testexp2log2 ();
262351#ifdef FIXED_POINT
263352 testilog2 ();
353+ #endif
354+ #ifndef DISABLE_FLOAT_API
355+ for (i = 0 ; i <= 1 ; ++ i )
356+ {
357+ testcelt_float2int16 (use_ref_impl [i ], 1 );
358+ testcelt_float2int16 (use_ref_impl [i ], 32 );
359+ testcelt_float2int16 (use_ref_impl [i ], 127 );
360+ testcelt_float2int16 (use_ref_impl [i ], 1031 );
361+ }
264362#endif
265363 return ret ;
266364}
0 commit comments