ulps: Fix long lines
[mathlib.git] / etc / testrand2.c
1 #include <assert.h>
2 #include <math.h>
3 #include <mytypes.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "subr_random.h"
8
9 struct
10 stats {
11         const char *st_type;
12         size_t st_normals;
13         size_t st_subnormals;
14         size_t st_nans;
15         size_t st_inf;
16         size_t st_zero;
17         size_t st_invalid;
18 };
19
20 #define DECL(type)                                              \
21 static void                                                     \
22 probe_##type(struct stats *st, size_t N)                        \
23 {                                                               \
24         type x;                                                 \
25         size_t i;                                               \
26                                                                 \
27         assert(st);                                             \
28         memset(st, 0, sizeof(*st));                             \
29                                                                 \
30         st->st_type = #type;                                    \
31                                                                 \
32         for (i = 0; i < N; i++) {                               \
33                 x = random_##type(1);                           \
34                 switch (fpclassify(x)) {                        \
35                 case FP_NORMAL:                                 \
36                         ++st->st_normals;                       \
37                         break;                                  \
38                 case FP_SUBNORMAL:                              \
39                         ++st->st_subnormals;                    \
40                         break;                                  \
41                 case FP_NAN:                                    \
42                         ++st->st_nans;                          \
43                         break;                                  \
44                 case FP_INFINITE:                               \
45                         ++st->st_inf;                           \
46                         break;                                  \
47                 case FP_ZERO:                                   \
48                         ++st->st_zero;                          \
49                         break;                                  \
50                 default:                                        \
51                         /* invalid floating-point number */     \
52                         ++st->st_invalid;                       \
53                         break;                                  \
54                 }                                               \
55         }                                                       \
56 }
57
58 typedef long double long_double;
59 DECL(float);
60 DECL(double);
61 DECL(long_double);
62
63 static void
64 print_stats(const struct stats *st)
65 {
66         assert(st);
67
68         printf("---------- %s ----------\n", st->st_type);
69         printf("  Normals: %u\n", st->st_normals);
70         printf("Subnormals: %u\n", st->st_subnormals);
71         printf("     NaNs: %u\n", st->st_nans);
72         printf("      Inf: %u\n", st->st_inf);
73         printf("     Zero: %u\n", st->st_zero);
74         printf("  Invalid: %u\n", st->st_invalid);
75         fflush(stdout);
76 }
77
78 int
79 main(void)
80 {
81         struct stats s;
82         const size_t N = 100*1000*1000;
83
84         init_randgen();
85 #if 0
86         /* floats */
87         probe_float(&s, N);
88         print_stats(&s);
89
90         /* double */
91         probe_double(&s, N);
92         print_stats(&s);
93 #endif
94
95         /* long double */
96         probe_long_double(&s, N);
97         print_stats(&s);
98
99         return 0;
100 }