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, Timothy B. Terriberry,
45 and Yunho Huh */
56/*
3738
3839#include <stdio.h>
3940#include <math.h>
40- #include "mathops.h"
4141#include "bands.h"
42+ #include "cpu_support.h"
43+ #include "float_cast.h"
44+ #include "mathops.h"
4245
4346#ifdef FIXED_POINT
4447#define WORD "%d"
@@ -351,8 +354,94 @@ void testilog2(void)
351354}
352355#endif
353356
357+
358+ #ifndef DISABLE_FLOAT_API
359+
360+ void testcelt_float2int16 (int use_ref_impl , int buffer_size )
361+ {
362+
363+ #define MAX_BUFFER_SIZE 2080
364+ int i , cnt ;
365+ float floatsToConvert [MAX_BUFFER_SIZE ];
366+ short results [MAX_BUFFER_SIZE ] = { 0 };
367+ float scaleInt16RangeTo01 ;
368+
369+ celt_assert (buffer_size <= MAX_BUFFER_SIZE );
370+
371+ scaleInt16RangeTo01 = 1.f / 32768.f ;
372+ cnt = 0 ;
373+
374+ while (cnt + 15 < buffer_size && cnt < buffer_size / 2 )
375+ {
376+ floatsToConvert [cnt ++ ] = 77777.0f * scaleInt16RangeTo01 ;
377+ floatsToConvert [cnt ++ ] = 33000.0f * scaleInt16RangeTo01 ;
378+ floatsToConvert [cnt ++ ] = 32768.0f * scaleInt16RangeTo01 ;
379+ floatsToConvert [cnt ++ ] = 32767.4f * scaleInt16RangeTo01 ;
380+ floatsToConvert [cnt ++ ] = 32766.6f * scaleInt16RangeTo01 ;
381+ floatsToConvert [cnt ++ ] = .501 * scaleInt16RangeTo01 ;
382+ floatsToConvert [cnt ++ ] = .499f * scaleInt16RangeTo01 ;
383+ floatsToConvert [cnt ++ ] = .0f ;
384+ floatsToConvert [cnt ++ ] = -.499f * scaleInt16RangeTo01 ;
385+ floatsToConvert [cnt ++ ] = -.501f * scaleInt16RangeTo01 ;
386+ floatsToConvert [cnt ++ ] = -32767.6f * scaleInt16RangeTo01 ;
387+ floatsToConvert [cnt ++ ] = -32768.4f * scaleInt16RangeTo01 ;
388+ floatsToConvert [cnt ++ ] = -32769.0f * scaleInt16RangeTo01 ;
389+ floatsToConvert [cnt ++ ] = -33000.0f * scaleInt16RangeTo01 ;
390+ floatsToConvert [cnt ++ ] = -77777.0f * scaleInt16RangeTo01 ;
391+
392+ celt_assert (cnt < buffer_size );
393+ }
394+
395+ while (cnt < buffer_size )
396+ {
397+ float inInt16Range = cnt * 7 + .5 ;
398+ inInt16Range += (cnt & 0x01 ) ? .1 : -.1 ;
399+ inInt16Range *= (cnt & 0x02 ) ? 1 : -1 ;
400+ floatsToConvert [cnt ++ ] = inInt16Range * scaleInt16RangeTo01 ;
401+ }
402+
403+ for (i = 0 ; i < MAX_BUFFER_SIZE ; ++ i )
404+ {
405+ results [i ] = 42 ;
406+ }
407+
408+ if (use_ref_impl )
409+ {
410+ celt_float2int16_c (floatsToConvert , results , cnt );
411+ } else {
412+ celt_float2int16 (floatsToConvert , results , cnt , opus_select_arch ());
413+ }
414+
415+ for (i = 0 ; i < cnt ; ++ i )
416+ {
417+ const float expected = FLOAT2INT16 (floatsToConvert [i ]);
418+ if (results [i ] != expected )
419+ {
420+ fprintf (stderr , "testcelt_float2int16 failed: celt_float2int16 converted %f (index: %d) to %d (x*32768=%f, expected: %d, cnt: %d, ref: %d)\n" ,
421+ floatsToConvert [i ], i , (int )results [i ], floatsToConvert [i ] * 32768.0f , (int )expected , buffer_size , use_ref_impl );
422+ ret = 1 ;
423+ }
424+ }
425+
426+ for (i = cnt ; i < MAX_BUFFER_SIZE ; ++ i )
427+ {
428+ if (results [i ] != 42 )
429+ {
430+ fprintf (stderr , "testcelt_float2int16 failed: buffer overflow (cnt: %d, ref: %d)\n" , buffer_size , use_ref_impl );
431+ ret = 1 ;
432+ break ;
433+ }
434+ }
435+ #undef MAX_BUFFER_SIZE
436+ }
437+
438+ #endif
439+
354440int main (void )
355441{
442+ int i ;
443+ int use_ref_impl [2 ] = { 0 , 1 };
444+
356445 testbitexactcos ();
357446 testbitexactlog2tan ();
358447 testdiv ();
@@ -364,6 +453,15 @@ int main(void)
364453 testilog2 ();
365454 testlog2_db ();
366455 testexp2_db ();
456+ #endif
457+ #ifndef DISABLE_FLOAT_API
458+ for (i = 0 ; i <= 1 ; ++ i )
459+ {
460+ testcelt_float2int16 (use_ref_impl [i ], 1 );
461+ testcelt_float2int16 (use_ref_impl [i ], 32 );
462+ testcelt_float2int16 (use_ref_impl [i ], 127 );
463+ testcelt_float2int16 (use_ref_impl [i ], 1031 );
464+ }
367465#endif
368466 return ret ;
369467}
0 commit comments