22#include <stddef.h>
33#include <stdint.h>
44#include <stdio.h>
5+ #include <string.h>
56#include <math.h>
7+ #include <errno.h>
8+ #include <fenv.h>
69#include <assert.h>
710#include <ti/screen.h>
811#include <ti/getcsc.h>
1316
1417#define ARRAY_LENGTH (x ) (sizeof(x) / sizeof(x[0]))
1518
19+ #if 0
20+ #define test_printf printf
21+ #else
22+ #define test_printf (...)
23+ #endif
24+
25+ long double truth_logbl (long double x ) {
26+ if (isfinite (x )) {
27+ if (iszero (x )) {
28+ errno = EDOM ;
29+ feraiseexcept (FE_DIVBYZERO );
30+ return - HUGE_VALL ;
31+ }
32+ return (long double )ilogbl (x );
33+ }
34+ // infinity and NaN
35+ return fabsl (x );
36+ }
37+
1638size_t run_test (void ) {
1739 typedef long double input_t ;
1840 typedef int output_t ;
@@ -24,9 +46,32 @@ size_t run_test(void) {
2446 for (size_t i = 0 ; i < length ; i ++ ) {
2547 int result = ilogbl (input [i ]);
2648 if (result != output [i ]) {
27- #if 0
28- printf ("%4zu: %016llX\n %d != %d\n" , i , input [i ], result , output [i ]);
29- #endif
49+ test_printf ("%4zu: %016llX\n%d != %d\n" , i , input [i ], result , output [i ]);
50+ return i ;
51+ }
52+
53+ errno = 0 ;
54+ feclearexcept (FE_ALL_EXCEPT );
55+ long double f_truth = truth_logbl (input [i ]);
56+ unsigned char fe_truth = __fe_cur_env ;
57+ int errno_truth = errno ;
58+
59+ errno = 0 ;
60+ feclearexcept (FE_ALL_EXCEPT );
61+ long double f_guess = logbl (input [i ]);
62+ unsigned char fe_guess = __fe_cur_env ;
63+ int errno_guess = errno ;
64+
65+ if (memcmp (& f_guess , & f_truth , sizeof (long double )) != 0 ) {
66+ test_printf ("%4zu:\n%016llX %d\n%016llX !=\n%016llX\n" , i , input [i ], output [i ], f_guess , f_truth );
67+ return i ;
68+ }
69+ if (fe_guess != fe_truth ) {
70+ fputs ("fenv fail\n" , stdout );
71+ return i ;
72+ }
73+ if (errno_guess != errno_truth ) {
74+ fputs ("errno fail\n" , stdout );
3075 return i ;
3176 }
3277 }
0 commit comments