2 * Floating Point Convert Single to Various
4 * Copyright (c) 2019 Linaro
6 * SPDX-License-Identifier: GPL-3.0-or-later
16 #include "float_helpers.h"
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25 float_mapping round_flags
[] = {
26 { FE_TONEAREST
, "to nearest" },
28 { FE_UPWARD
, "upwards" },
31 { FE_DOWNWARD
, "downwards" },
34 { FE_TOWARDZERO
, "to zero" }
38 static void print_input(float input
)
40 char *in_fmt
= fmt_f32(input
);
41 printf("from single: %s\n", in_fmt
);
45 static void convert_single_to_double(float input
)
48 char *out_fmt
, *flag_fmt
;
50 feclearexcept(FE_ALL_EXCEPT
);
54 out_fmt
= fmt_f64(output
);
55 flag_fmt
= fmt_flags();
56 printf(" to double: %s (%s)\n", out_fmt
, flag_fmt
);
61 #define xstr(a) str(a)
64 #define CONVERT_SINGLE_TO_INT(TYPE, FMT) \
65 static void convert_single_to_ ## TYPE(float input) \
69 const char to[] = "to " xstr(TYPE); \
70 feclearexcept(FE_ALL_EXCEPT); \
72 flag_fmt = fmt_flags(); \
73 printf("%11s: %" FMT " (%s)\n", to, output, flag_fmt); \
77 CONVERT_SINGLE_TO_INT( int32
, PRId32
)
78 CONVERT_SINGLE_TO_INT(uint32
, PRId32
)
79 CONVERT_SINGLE_TO_INT( int64
, PRId64
)
80 CONVERT_SINGLE_TO_INT(uint64
, PRId64
)
82 int main(int argc
, char *argv
[argc
])
88 for (i
= 0; i
< ARRAY_SIZE(round_flags
); ++i
) {
89 if (fesetround(round_flags
[i
].flag
) != 0) {
90 printf("### Rounding %s skipped\n", round_flags
[i
].desc
);
93 printf("### Rounding %s\n", round_flags
[i
].desc
);
94 for (j
= 0; j
< nums
; j
++) {
95 float input
= get_f32(j
);
97 /* convert_single_to_half(input); */
98 convert_single_to_double(input
);
99 convert_single_to_int32(input
);
100 convert_single_to_int64(input
);
101 convert_single_to_uint32(input
);
102 convert_single_to_uint64(input
);