1414
1515#include <assert.h>
1616#include <float.h> /* FLT_HAS_SUBNORM */
17- #include <inttypes.h>
1817#include <math.h> /* isnormal */
1918#include <stdlib.h>
2019#include <stdio.h>
2120
21+ #include "../float32.h"
22+
2223#if FLT_HAS_SUBNORM != 1
2324#error float does not have subnormal numbers
2425#endif
2526
26- typedef struct {
27- uint32_t sign , exponent , fraction ;
28- } Float32 ;
29-
30- Float32 float32_from_float (float f ) {
31- uint32_t bytes ;
32- Float32 float32 ;
33- bytes = * (uint32_t * )& f ;
34- float32 .fraction = bytes & 0x007FFFFF ;
35- bytes >>= 23 ;
36- float32 .exponent = bytes & 0x000000FF ;
37- bytes >>= 8 ;
38- float32 .sign = bytes & 0x000000001 ;
39- bytes >>= 1 ;
40- return float32 ;
41- }
42-
43- float float_from_bytes (
44- uint32_t sign ,
45- uint32_t exponent ,
46- uint32_t fraction
47- ) {
48- uint32_t bytes ;
49- bytes = 0 ;
50- bytes |= sign ;
51- bytes <<= 8 ;
52- bytes |= exponent ;
53- bytes <<= 23 ;
54- bytes |= fraction ;
55- return * (float * )& bytes ;
56- }
57-
58- int float32_equal (
59- float f ,
60- uint32_t sign ,
61- uint32_t exponent ,
62- uint32_t fraction
63- ) {
64- Float32 float32 ;
65- float32 = float32_from_float (f );
66- return
67- (float32 .sign == sign ) &&
68- (float32 .exponent == exponent ) &&
69- (float32 .fraction == fraction )
70- ;
71- }
72-
73- void float32_print (float f ) {
74- Float32 float32 = float32_from_float (f );
75- printf (
76- "%" PRIu32 " %" PRIu32 " %" PRIu32 "\n" ,
77- float32 .sign , float32 .exponent , float32 .fraction
78- );
79- }
80-
8127int main (void ) {
8228 /* Basic examples. */
83- assert (float32_equal (0.5f , 0 , 126 , 0 ));
84- assert (float32_equal (1.0f , 0 , 127 , 0 ));
85- assert (float32_equal (2.0f , 0 , 128 , 0 ));
29+ assert (lkmc_float32_equal (0.5f , 0 , 126 , 0 ));
30+ assert (lkmc_float32_equal (1.0f , 0 , 127 , 0 ));
31+ assert (lkmc_float32_equal (2.0f , 0 , 128 , 0 ));
8632 assert (isnormal (0.5f ));
8733 assert (isnormal (1.0f ));
8834 assert (isnormal (2.0f ));
@@ -93,33 +39,33 @@ int main(void) {
9339 assert (2.0f == 0x1.0p1f );
9440
9541 /* Sign bit. */
96- assert (float32_equal (-0.5f , 1 , 126 , 0 ));
97- assert (float32_equal (-1.0f , 1 , 127 , 0 ));
98- assert (float32_equal (-2.0f , 1 , 128 , 0 ));
42+ assert (lkmc_float32_equal (-0.5f , 1 , 126 , 0 ));
43+ assert (lkmc_float32_equal (-1.0f , 1 , 127 , 0 ));
44+ assert (lkmc_float32_equal (-2.0f , 1 , 128 , 0 ));
9945 assert (isnormal (-0.5f ));
10046 assert (isnormal (-1.0f ));
10147 assert (isnormal (-2.0f ));
10248
10349 /* The special case of 0.0 and -0.0. */
104- assert (float32_equal ( 0.0f , 0 , 0 , 0 ));
105- assert (float32_equal (-0.0f , 1 , 0 , 0 ));
50+ assert (lkmc_float32_equal ( 0.0f , 0 , 0 , 0 ));
51+ assert (lkmc_float32_equal (-0.0f , 1 , 0 , 0 ));
10652 assert (!isnormal ( 0.0f ));
10753 assert (!isnormal (-0.0f ));
10854 assert (0.0f == -0.0f );
10955
11056 /* ANSI C defines FLT_MIN as the smallest non-subnormal number. */
11157 assert (FLT_MIN == 0x1.0p-126f );
112- assert (float32_equal (FLT_MIN , 0 , 1 , 0 ));
58+ assert (lkmc_float32_equal (FLT_MIN , 0 , 1 , 0 ));
11359 assert (isnormal (FLT_MIN ));
11460
11561 /* The largest subnormal number. */
116- float largest_subnormal = float_from_bytes (0 , 0 , 0x7FFFFF );
62+ float largest_subnormal = lkmc_float32_from_bytes (0 , 0 , 0x7FFFFF );
11763 assert (largest_subnormal == 0x0.FFFFFEp-126f );
11864 assert (largest_subnormal < FLT_MIN );
11965 assert (!isnormal (largest_subnormal ));
12066
12167 /* The smallest non-zero subnormal number. */
122- float smallest_subnormal = float_from_bytes (0 , 0 , 1 );
68+ float smallest_subnormal = lkmc_float32_from_bytes (0 , 0 , 1 );
12369 assert (smallest_subnormal == 0x0.000002p-126f );
12470 assert (0.0f < smallest_subnormal );
12571 assert (!isnormal (smallest_subnormal ));
0 commit comments