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
, "sin")) {
85 a
->f32
.f32__f32
= sinf
;
86 a
->f64
.f64__f64
= sin
;
87 } else if (!strcmp(f
, "trunc")) {
89 a
->f32
.f32__f32
= truncf
;
90 a
->f64
.f64__f64
= trunc
;
91 } else if (!strcmp(f
, "fma")) {
92 a
->a
= A__FLT_FLT_FLT__FLT
;
93 a
->f32
.f32_f32_f32__f32
= fmaf
;
94 a
->f64
.f64_f64_f64__f64
= fma
;
95 } else if (!strcmp(f
, "sqrt")) {
97 a
->f32
.f32__f32
= sqrtf
;
98 a
->f64
.f64__f64
= sqrt
;
100 fprintf(stderr
, "impl-libc: unknown function \"%s\"\n", f
);
105 void read_buf(char *b
, ssize_t len
)
110 while (total
< len
) {
111 r
= read(0, (b
+ total
), (len
- total
));
116 } else if (r
== -1) {
117 perror("impl-libc: read");
125 void write_buf(const char *b
, ssize_t len
)
130 while (total
< len
) {
131 r
= write(1, (b
+ total
), (len
- total
));
134 perror("impl-libc: write");
142 size_t input_width(argtype a
, precision p
)
144 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
152 case A__FLT_FLT_FLT__FLT
:
160 size_t output_width(argtype a
, precision p
)
162 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
170 case A__FLT_FLT_FLT__FLT
:
178 void io_loop(action a
, size_t n
)
182 size_t in_sz
= input_width(a
.a
, a
.p
);
183 size_t out_sz
= output_width(a
.a
, a
.p
);
185 if ((in_sz
* n
) / n
!= in_sz
) {
186 fprintf(stderr
, "impl-libc: input length overflow\n");
190 if ((out_sz
* n
) / n
!= out_sz
) {
191 fprintf(stderr
, "impl-libc: output length overflow\n");
195 if (!(in_buf
= malloc(in_sz
* n
))) {
196 perror("impl-libc: malloc");
200 if (!(out_buf
= malloc(out_sz
* n
))) {
201 perror("impl-libc: malloc");
206 read_buf(in_buf
, in_sz
* n
);
210 fprintf(stderr
, "impl-libc: impossible\n");
218 for (size_t j
= 0; j
< n
; ++j
) {
219 float *x
= (float *) (in_buf
+ (in_sz
*
221 float *y
= (float *) (out_buf
+
224 *y
= a
.f32
.f32__f32(*x
);
230 for (size_t j
= 0; j
< n
; ++j
) {
231 double *x
= (double *) (in_buf
+
233 double *y
= (double *) (out_buf
+
236 *y
= a
.f64
.f64__f64(*x
);
243 case A__FLT_FLT_FLT__FLT
:
248 for (size_t j
= 0; j
< n
; ++j
) {
249 float *x1
= (float *) (in_buf
+ (in_sz
*
251 float *x2
= (float *) (in_buf
+ (in_sz
*
254 float *x3
= (float *) (in_buf
+ (in_sz
*
257 float *y
= (float *) (out_buf
+
260 *y
= a
.f32
.f32_f32_f32__f32(*x1
, *x2
,
267 for (size_t j
= 0; j
< n
; ++j
) {
268 double *x1
= (double *) (in_buf
+
270 double *x2
= (double *) (in_buf
+
273 double *x3
= (double *) (in_buf
+
276 double *y
= (double *) (out_buf
+
279 *y
= a
.f64
.f64_f64_f64__f64(*x1
, *x2
,
289 write_buf(out_buf
, out_sz
* n
);
293 int main(int argc
, char **argv
)
296 action a
= { .p
= P_SINGLE
};
299 while ((c
= getopt(argc
, argv
, "sdf:n:")) != -1) {
308 determine_function(optarg
, &a
);
312 n
= strtoll(optarg
, 0, 0);
315 perror("impl-libc: unparsable");
327 if (a
.a
== A_UNKNOWN
) {