ulps: Fix long lines
[mathlib.git] / ulps / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "gen.h"
6 #include "subr_random.h"
7 #include "ulp.h"
8
9 static int isbanned(const char *fname, int argc, char *argv[]);
10 static void usage(void);
11
12 int
13 main(int argc, char *argv[])
14 {
15         struct ulp u;
16         struct ulp_complex uc;
17         int i, total, all, realonly, complexonly, ban, list;
18         const char *target;
19         const struct fentry *f;
20
21         /* Skip program name */
22         argv++;
23         argc--;
24
25         /*
26          * Parse command line arguments
27          * all  = probe all supported functions
28          * list = list  all supported functions
29          */
30         all = 0;
31         realonly = 0;
32         complexonly = 0;
33         list = 0;
34         if (argc == 0) {
35                 usage();
36                 /* never reached */
37         } else if (argc == 1 && !strcmp(argv[0], "all")) {
38                 all = 1;
39                 argv++; argc--;
40         } else if (argc == 1 && !strcmp(argv[0], "real")) {
41                 all = realonly = 1;
42                 argv++; argc--;
43         } else if (argc == 1 && !strcmp(argv[0], "complex")) {
44                 all = complexonly = 1;
45                 argv++; argc--;
46         } else if (argc > 1 && !strcmp(argv[0], "ban")) {
47                 all = ban = 1;
48                 argv++; argc--;
49         } else if (argc == 1 && !strcmp(argv[0], "list")) {
50                 list = 1;
51                 argv++; argc--;
52         }
53
54         if (list)
55                 printfunctions();
56
57         /* Initialize random number generator */
58         init_randgen();
59
60         /* Print header */
61         if (!list)
62                 printf("\tFUNCTION     Max ULP    Min ULP    Avg ULP    "
63                     "skipped\n");
64
65         total = all ? fsize : argc;
66
67         for (i = 0; i < total; i++) {
68                 if (all) {
69                         f = getfunctionbyidx(i);
70                         if ((realonly    && !f->f_mpfr) ||
71                             (complexonly &&  f->f_mpfr))
72                                 continue;
73                         
74                         if (ban) {
75                                 if (isbanned(f->f_name, argc, argv)) {
76                                         printf("[%2u/%2u] %-12s %s\n",
77                                             i+1, total, f->f_name,
78                                             "\t\t--- banned ---");
79                                         continue;
80                                 }
81                         }
82
83                         target = f->f_name;
84                 } else {
85                         target = argv[i];
86                 }
87
88                 /* Is it real or complex value function ? */
89                 f = getfunctionbyname(target);
90                 if (f == NULL) {
91                         fprintf(stderr, "function: %s not found\n",
92                             argv[i]);
93                         continue;
94                 }
95
96                 /* Calculate ulp */
97                 if (f->f_mpfr)
98                         getfunctionulp_real(f, &u);
99                 else
100                         getfunctionulp_complex(f, &uc);
101
102                 if (f->f_mpfr) {
103                         printf("[%2u/%2u] %-12s ", i+1, total, target);
104                         printulps_double(u);
105                         if (all && f->f_libml_real) {
106                                 printf("        %-12s ", f->f_namel);
107                                 printulps_long_double(u);
108                         }
109                 } else {
110                         printf("[%2u/%2u] %-12s ", i+1, total, target);
111                         printulps_double_complex(uc);
112                 }
113         }
114
115         return EXIT_SUCCESS;
116 }
117
118 static int
119 isbanned(const char *fname, int argc, char *argv[])
120 {
121         int i;
122
123         for (i = 0; i < argc; i++) {
124                 if (!strcmp(fname, argv[i])) {
125                         return 1;
126                 }
127         }
128
129         return 0;
130 }
131
132 static void
133 usage(void)
134 {
135         fprintf(stderr,
136             "ulps: [list | all | real | complex | "
137             "ban <function1> <function2> ...] | "
138             "<function1> <function2> ... \n");
139         exit(EXIT_FAILURE);
140 }