9 /* Whether we're looking at 32-bit or 64-bit floats */
10 typedef enum { P_SINGLE
, P_DOUBLE
} precision
;
12 /* What type of arguments we expect, and what we'll give back. */
20 /* Types of functions we could call */
21 typedef float (*f__f32__f32
)(float);
22 typedef float (*f__f32_f32_f32__f32
)(float, float, float);
23 typedef double (*f__f64__f64
)(double);
24 typedef double (*f__f64_f64_f64__f64
)(double, double, double);
26 /* Wrapper around a function pointer */
35 f__f32_f32_f32__f32 f32_f32_f32__f32
;
41 f__f64_f64_f64__f64 f64_f64_f64__f64
;
49 "usage: impl-libc [-s|-d] -f <function_name> -n <num_inputs>\n");
63 void determine_function(const char *f
, action
*a
)
65 if (!strcmp(f
, "zzzzzz")) {
67 } else if (!strcmp(f
, "id")) {
69 a
->f32
.f32__f32
= idf
;
70 a
->f64
.f64__f64
= idd
;
71 } else if (!strcmp(f
, "ceil")) {
73 a
->f32
.f32__f32
= ceilf
;
74 a
->f64
.f64__f64
= ceil
;
75 } else if (!strcmp(f
, "cos")) {
77 a
->f32
.f32__f32
= cosf
;
78 a
->f64
.f64__f64
= cos
;
79 } else if (!strcmp(f
, "floor")) {
81 a
->f32
.f32__f32
= floorf
;
82 a
->f64
.f64__f64
= floor
;
83 } else if (!strcmp(f
, "fma")) {
84 a
->a
= A__FLT_FLT_FLT__FLT
;
85 a
->f32
.f32_f32_f32__f32
= fmaf
;
86 a
->f64
.f64_f64_f64__f64
= fma
;
87 } else if (!strcmp(f
, "exp")) {
89 a
->f32
.f32__f32
= expf
;
90 a
->f64
.f64__f64
= exp
;
91 } else if (!strcmp(f
, "expm1")) {
93 a
->f32
.f32__f32
= expm1f
;
94 a
->f64
.f64__f64
= expm1
;
95 } else if (!strcmp(f
, "log")) {
97 a
->f32
.f32__f32
= logf
;
98 a
->f64
.f64__f64
= log
;
99 } else if (!strcmp(f
, "log1p")) {
101 a
->f32
.f32__f32
= log1pf
;
102 a
->f64
.f64__f64
= log1p
;
103 } else if (!strcmp(f
, "sin")) {
105 a
->f32
.f32__f32
= sinf
;
106 a
->f64
.f64__f64
= sin
;
107 } else if (!strcmp(f
, "sqrt")) {
109 a
->f32
.f32__f32
= sqrtf
;
110 a
->f64
.f64__f64
= sqrt
;
111 } else if (!strcmp(f
, "trunc")) {
113 a
->f32
.f32__f32
= truncf
;
114 a
->f64
.f64__f64
= trunc
;
116 fprintf(stderr
, "impl-libc: unknown function \"%s\"\n", f
);
121 void read_buf(char *b
, ssize_t len
)
126 while (total
< len
) {
127 r
= read(0, (b
+ total
), (len
- total
));
132 } else if (r
== -1) {
133 perror("impl-libc: read");
141 void write_buf(const char *b
, ssize_t len
)
146 while (total
< len
) {
147 r
= write(1, (b
+ total
), (len
- total
));
150 perror("impl-libc: write");
158 size_t input_width(argtype a
, precision p
)
160 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
168 case A__FLT_FLT_FLT__FLT
:
176 size_t output_width(argtype a
, precision p
)
178 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
186 case A__FLT_FLT_FLT__FLT
:
194 void io_loop(action a
, size_t n
)
198 size_t in_sz
= input_width(a
.a
, a
.p
);
199 size_t out_sz
= output_width(a
.a
, a
.p
);
201 if ((in_sz
* n
) / n
!= in_sz
) {
202 fprintf(stderr
, "impl-libc: input length overflow\n");
206 if ((out_sz
* n
) / n
!= out_sz
) {
207 fprintf(stderr
, "impl-libc: output length overflow\n");
211 if (!(in_buf
= malloc(in_sz
* n
))) {
212 perror("impl-libc: malloc");
216 if (!(out_buf
= malloc(out_sz
* n
))) {
217 perror("impl-libc: malloc");
222 read_buf(in_buf
, in_sz
* n
);
226 fprintf(stderr
, "impl-libc: impossible\n");
234 for (size_t j
= 0; j
< n
; ++j
) {
235 float *x
= (float *) (in_buf
+ (in_sz
*
237 float *y
= (float *) (out_buf
+
240 *y
= a
.f32
.f32__f32(*x
);
246 for (size_t j
= 0; j
< n
; ++j
) {
247 double *x
= (double *) (in_buf
+
249 double *y
= (double *) (out_buf
+
252 *y
= a
.f64
.f64__f64(*x
);
259 case A__FLT_FLT_FLT__FLT
:
264 for (size_t j
= 0; j
< n
; ++j
) {
265 float *x1
= (float *) (in_buf
+ (in_sz
*
267 float *x2
= (float *) (in_buf
+ (in_sz
*
270 float *x3
= (float *) (in_buf
+ (in_sz
*
273 float *y
= (float *) (out_buf
+
276 *y
= a
.f32
.f32_f32_f32__f32(*x1
, *x2
,
283 for (size_t j
= 0; j
< n
; ++j
) {
284 double *x1
= (double *) (in_buf
+
286 double *x2
= (double *) (in_buf
+
289 double *x3
= (double *) (in_buf
+
292 double *y
= (double *) (out_buf
+
295 *y
= a
.f64
.f64_f64_f64__f64(*x1
, *x2
,
305 write_buf(out_buf
, out_sz
* n
);
309 int main(int argc
, char **argv
)
312 action a
= { .p
= P_SINGLE
};
315 while ((c
= getopt(argc
, argv
, "sdf:n:")) != -1) {
324 determine_function(optarg
, &a
);
328 n
= strtoll(optarg
, 0, 0);
331 perror("impl-libc: unparsable");
343 if (a
.a
== A_UNKNOWN
) {