Skip to content

Commit cf81f9d

Browse files
committed
Add function to populate optstruct from format name
1 parent d370138 commit cf81f9d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/cpfloat_definitions.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define _CHOPFAST_DEFINITIONS_
2525

2626
// #define _CRT_RAND_S
27+
#include <stdbool.h>
2728
#include <stdlib.h>
2829
#include <stdint.h>
2930

@@ -384,6 +385,21 @@ typedef struct {
384385
cpfloat_randseed_t *randseed;
385386
} optstruct;
386387

388+
/**
389+
@brief Populate fields of @ref optstruct from `format` field.
390+
391+
@details This function populates the fields of @p fpopts based on the
392+
string in the @p fpopts->format.
393+
394+
@param[in] fpopts Parameters describing the target format, the rounding mode,
395+
and the probability of soft errors striking the rounded values.
396+
397+
@return The function returns @b 0 if the string in @p fpopts->format is a
398+
valid format name, @b 1 if the string is empty or @p NULL, @b 2 if the string
399+
is "custom" or "c", and @b -1 if the string is not a valid format name.<p/>
400+
*/
401+
int cpfloat_populate_optstruct_from_format(optstruct *fpopts);
402+
387403
/**
388404
@brief Allocate @ref optstruct struct to store parameters of target format.
389405

src/cpfloat_template.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,63 @@ static inline BITTYPE PRNG_ADVANCE_BIT(BITSEEDTYPE *seed, size_t delta) {
101101
* Prototypes are in cpfloat_definitions.
102102
*/
103103

104+
int cpfloat_populate_optstruct_from_format(optstruct *fpopts) {
105+
if (fpopts == NULL)
106+
return -1;
107+
const char *fmt = fpopts->format;
108+
bool format_is_valid = true;
109+
bool is_subn_rnd_default = false;
110+
bool is_inf_no_default = false;
111+
if (fmt == NULL || fmt[0] == '\0')
112+
return 1;
113+
else if (strcmp(fmt, "custom") == 0 || strcmp(fmt, "c") == 0) {
114+
return 2;
115+
} else if (strcmp(fmt, "q43") == 0 || strcmp(fmt, "e4m3") == 0 || strcmp(fmt, "E4M3") == 0) {
116+
fpopts->precision = 4;
117+
fpopts->emin = -6;
118+
fpopts->emax = 8;
119+
is_inf_no_default = true;
120+
} else if (strcmp(fmt, "q52") == 0 || strcmp(fmt, "e5m2") == 0 || strcmp(fmt, "E5M2") == 0) {
121+
fpopts->precision = 3;
122+
fpopts->emin = -14;
123+
fpopts->emax = 15;
124+
} else if (strcmp(fmt, "b") == 0 || strcmp(fmt, "bf16") == 0 || strcmp(fmt, "bfloat16") == 0) {
125+
fpopts->precision = 8;
126+
fpopts->emin = -126;
127+
fpopts->emax = 127;
128+
is_subn_rnd_default = true;
129+
} else if (strcmp(fmt, "h") == 0 || strcmp(fmt, "fp16") == 0 || strcmp(fmt, "binary16") == 0 || strcmp(fmt, "half") == 0) {
130+
fpopts->precision = 11;
131+
fpopts->emin = -14;
132+
fpopts->emax = 15;
133+
} else if (strcmp(fmt, "t") == 0 || strcmp(fmt, "tf32") == 0 || strcmp(fmt, "TensorFloat-32") == 0) {
134+
fpopts->precision = 11;
135+
fpopts->emin = -126;
136+
fpopts->emax = 127;
137+
} else if (strcmp(fmt, "s") == 0 || strcmp(fmt, "fp32") == 0 || strcmp(fmt, "binary32") == 0 || strcmp(fmt, "single") == 0) {
138+
fpopts->precision = 24;
139+
fpopts->emin = -126;
140+
fpopts->emax = 127;
141+
} else if (strcmp(fmt, "d") == 0 || strcmp(fmt, "fp64") == 0 || strcmp(fmt, "binary64") == 0 || strcmp(fmt, "double") == 0) {
142+
fpopts->precision = 53;
143+
fpopts->emin = -1022;
144+
fpopts->emax = 1023;
145+
} else {
146+
format_is_valid = false;
147+
}
148+
fpopts->subnormal = is_subn_rnd_default ? CPFLOAT_SUBN_RND : CPFLOAT_SUBN_USE;
149+
fpopts->explim = CPFLOAT_EXPRANGE_TARG;
150+
fpopts->infinity = is_inf_no_default ? CPFLOAT_INF_NO : CPFLOAT_INF_USE;
151+
fpopts->round = CPFLOAT_RND_NE;
152+
fpopts->saturation = CPFLOAT_SAT_NO;
153+
fpopts->flip = CPFLOAT_SOFTERR_NO;
154+
fpopts->p = 0.0;
155+
if (format_is_valid)
156+
return 0;
157+
else
158+
return -1;
159+
}
160+
104161
optstruct *init_optstruct() {
105162
optstruct *fpopts = malloc(sizeof(*fpopts));
106163
fpopts->bitseed = NULL;

0 commit comments

Comments
 (0)