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
, "cot")) {
81 } else if (!strcmp(f
, "floor")) {
83 a
->f32
.f32__f32
= floorf
;
84 a
->f64
.f64__f64
= floor
;
85 } else if (!strcmp(f
, "fma")) {
86 a
->a
= A__FLT_FLT_FLT__FLT
;
87 a
->f32
.f32_f32_f32__f32
= fmaf
;
88 a
->f64
.f64_f64_f64__f64
= fma
;
89 } else if (!strcmp(f
, "exp")) {
91 a
->f32
.f32__f32
= expf
;
92 a
->f64
.f64__f64
= exp
;
93 } else if (!strcmp(f
, "expm1")) {
95 a
->f32
.f32__f32
= expm1f
;
96 a
->f64
.f64__f64
= expm1
;
97 } else if (!strcmp(f
, "log")) {
99 a
->f32
.f32__f32
= logf
;
100 a
->f64
.f64__f64
= log
;
101 } else if (!strcmp(f
, "log1p")) {
103 a
->f32
.f32__f32
= log1pf
;
104 a
->f64
.f64__f64
= log1p
;
105 } else if (!strcmp(f
, "powr")) {
106 /* libcs don't seem to have powr (yet?), just pow */
108 } else if (!strcmp(f
, "sin")) {
110 a
->f32
.f32__f32
= sinf
;
111 a
->f64
.f64__f64
= sin
;
112 } else if (!strcmp(f
, "sqrt")) {
114 a
->f32
.f32__f32
= sqrtf
;
115 a
->f64
.f64__f64
= sqrt
;
116 } else if (!strcmp(f
, "tan")) {
118 a
->f32
.f32__f32
= tanf
;
119 a
->f64
.f64__f64
= tan
;
120 } else if (!strcmp(f
, "trunc")) {
122 a
->f32
.f32__f32
= truncf
;
123 a
->f64
.f64__f64
= trunc
;
125 fprintf(stderr
, "impl-libc: unknown function \"%s\"\n", f
);
130 void read_buf(char *b
, ssize_t len
)
135 while (total
< len
) {
136 r
= read(0, (b
+ total
), (len
- total
));
141 } else if (r
== -1) {
142 perror("impl-libc: read");
150 void write_buf(const char *b
, ssize_t len
)
155 while (total
< len
) {
156 r
= write(1, (b
+ total
), (len
- total
));
159 perror("impl-libc: write");
167 size_t input_width(argtype a
, precision p
)
169 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
177 case A__FLT_FLT_FLT__FLT
:
185 size_t output_width(argtype a
, precision p
)
187 size_t w
= (p
== P_SINGLE
) ? 4 : 8;
195 case A__FLT_FLT_FLT__FLT
:
203 void io_loop(action a
, size_t n
)
207 size_t in_sz
= input_width(a
.a
, a
.p
);
208 size_t out_sz
= output_width(a
.a
, a
.p
);
210 if ((in_sz
* n
) / n
!= in_sz
) {
211 fprintf(stderr
, "impl-libc: input length overflow\n");
215 if ((out_sz
* n
) / n
!= out_sz
) {
216 fprintf(stderr
, "impl-libc: output length overflow\n");
220 if (!(in_buf
= malloc(in_sz
* n
))) {
221 perror("impl-libc: malloc");
225 if (!(out_buf
= malloc(out_sz
* n
))) {
226 perror("impl-libc: malloc");
231 read_buf(in_buf
, in_sz
* n
);
235 fprintf(stderr
, "impl-libc: impossible\n");
243 for (size_t j
= 0; j
< n
; ++j
) {
244 float *x
= (float *) (in_buf
+ (in_sz
*
246 float *y
= (float *) (out_buf
+
249 *y
= a
.f32
.f32__f32(*x
);
255 for (size_t j
= 0; j
< n
; ++j
) {
256 double *x
= (double *) (in_buf
+
258 double *y
= (double *) (out_buf
+
261 *y
= a
.f64
.f64__f64(*x
);
268 case A__FLT_FLT_FLT__FLT
:
273 for (size_t j
= 0; j
< n
; ++j
) {
274 float *x1
= (float *) (in_buf
+ (in_sz
*
276 float *x2
= (float *) (in_buf
+ (in_sz
*
279 float *x3
= (float *) (in_buf
+ (in_sz
*
282 float *y
= (float *) (out_buf
+
285 *y
= a
.f32
.f32_f32_f32__f32(*x1
, *x2
,
292 for (size_t j
= 0; j
< n
; ++j
) {
293 double *x1
= (double *) (in_buf
+
295 double *x2
= (double *) (in_buf
+
298 double *x3
= (double *) (in_buf
+
301 double *y
= (double *) (out_buf
+
304 *y
= a
.f64
.f64_f64_f64__f64(*x1
, *x2
,
314 write_buf(out_buf
, out_sz
* n
);
318 int main(int argc
, char **argv
)
321 action a
= { .p
= P_SINGLE
};
324 while ((c
= getopt(argc
, argv
, "sdf:n:")) != -1) {
333 determine_function(optarg
, &a
);
337 n
= strtoll(optarg
, 0, 0);
340 perror("impl-libc: unparsable");
352 if (a
.a
== A_UNKNOWN
) {