-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedfloat16_encode.c
More file actions
64 lines (53 loc) · 1.56 KB
/
medfloat16_encode.c
File metadata and controls
64 lines (53 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) 2025, Christophe Dufaza
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "cmedfloat/medfloat16.h"
#include "cmedfloat_cli_utils.h"
static void usage(void)
{
puts("usage: medfloat16-encode [-p <n>] [-v] <x>");
puts("");
puts("-v verbose output");
puts("-p <n> requested precision for fixed-point conversions");
puts("<x> floating-point literal, e.g. 1e-3, 0.001, INFINITY, NAN");
}
int main(int argc, char *argv[])
{
struct cmedfloat_cli_args_encode cli_args;
struct cmedfloat_medfloat16 mfloat16;
uint8_t data[MEDFLOAT16_LEN];
if (!cmedfloat_cli_encode_parse_args(argc, argv, &cli_args, false)) {
return -1;
}
if (cli_args.opt_h) {
usage();
return 0;
}
if (cli_args.opt_p) {
mfloat16 = cmedfloat_medfloat16_from_float_fixed(cli_args.x_float, cli_args.p);
} else {
mfloat16 = cmedfloat_medfloat16_from_float(cli_args.x_float);
}
cmedfloat_medfloat16_encode(mfloat16, data);
if (cli_args.opt_v) {
if (cli_args.opt_p) {
printf("encoding %#g with precision %d:\n", cli_args.x_float, cli_args.p);
} else {
printf("encoding %#g with default precision:\n", cli_args.x_float);
}
}
if (cmedfloat_medfloat16_isnres(mfloat16)) {
printf("NRes 0x%02x%02x\n", data[0], data[1]);
} else if (cmedfloat_medfloat16_isnormal(mfloat16)) {
printf("%de%d 0x%02x%02x\n", mfloat16.m, mfloat16.e, data[0], data[1]);
} else {
/* Print Nan and +/-INFINITY with %f. */
printf("%f: 0x%02x%02x\n", (double)cli_args.x_float, data[0], data[1]);
}
return 0;
}