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
;
96 fprintf(stderr
, "impl-libc: unknown function \"%s\"\n", f
);
101 void read_buf(char *b
, ssize_t len
)
106 while (total
< len
) {
107 r
= read(0, (b
+ total
), (len
- total
));
112 } else if (r
== -1) {
113 perror("impl-libc: read");
121 void write_buf(const char *b
, ssize_t len
)
126 while (total
< len
) {
127 r
= write(1, (b
+ total
), (len
- total
));
130 perror("impl-libc: write");
138 size_t input_width(argtype a
, precision p
)
140 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
148 case A__FLT_FLT_FLT__FLT
:
156 size_t output_width(argtype a
, precision p
)
158 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
166 case A__FLT_FLT_FLT__FLT
:
174 void io_loop(action a
, size_t n
)
178 size_t in_sz
= input_width(a
.a
, a
.p
);
179 size_t out_sz
= output_width(a
.a
, a
.p
);
181 if ((in_sz
* n
) / n
!= in_sz
) {
182 fprintf(stderr
, "impl-libc: input length overflow\n");
186 if ((out_sz
* n
) / n
!= out_sz
) {
187 fprintf(stderr
, "impl-libc: output length overflow\n");
191 if (!(in_buf
= malloc(in_sz
* n
))) {
192 perror("impl-libc: malloc");
196 if (!(out_buf
= malloc(out_sz
* n
))) {
197 perror("impl-libc: malloc");
202 read_buf(in_buf
, in_sz
* n
);
206 fprintf(stderr
, "impl-libc: impossible\n");
214 for (size_t j
= 0; j
< n
; ++j
) {
215 float *x
= (float *) (in_buf
+ (in_sz
*
217 float *y
= (float *) (out_buf
+
220 *y
= a
.f32
.f32__f32(*x
);
226 for (size_t j
= 0; j
< n
; ++j
) {
227 double *x
= (double *) (in_buf
+
229 double *y
= (double *) (out_buf
+
232 *y
= a
.f64
.f64__f64(*x
);
239 case A__FLT_FLT_FLT__FLT
:
244 for (size_t j
= 0; j
< n
; ++j
) {
245 float *x1
= (float *) (in_buf
+ (in_sz
*
247 float *x2
= (float *) (in_buf
+ (in_sz
*
250 float *x3
= (float *) (in_buf
+ (in_sz
*
253 float *y
= (float *) (out_buf
+
256 *y
= a
.f32
.f32_f32_f32__f32(*x1
, *x2
,
263 for (size_t j
= 0; j
< n
; ++j
) {
264 double *x1
= (double *) (in_buf
+
266 double *x2
= (double *) (in_buf
+
269 double *x3
= (double *) (in_buf
+
272 double *y
= (double *) (out_buf
+
275 *y
= a
.f64
.f64_f64_f64__f64(*x1
, *x2
,
285 write_buf(out_buf
, out_sz
* n
);
289 int main(int argc
, char **argv
)
292 action a
= { .p
= P_SINGLE
};
295 while ((c
= getopt(argc
, argv
, "sdf:n:")) != -1) {
304 determine_function(optarg
, &a
);
308 n
= strtoll(optarg
, 0, 0);
311 perror("impl-libc: unparsable");
323 if (a
.a
== A_UNKNOWN
) {