-
Notifications
You must be signed in to change notification settings - Fork 0
/
converting.c
309 lines (263 loc) · 7.45 KB
/
converting.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "converting.h"
#include "fileLoading.h" /* for verbosePrintf */
#define SDC_MIN(x, y) ((x) < (y) ? (x) : (y))
#define SDC_MAX(x, y) ((x) > (y) ? (x) : (y))
#define SDC_CLAMP(min, x, max) (SDC_MIN(SDC_MAX(x, min), max))
#define SDC_INT_TO_I64(type, in, out, len) \
do \
{ \
size_t iti_i; \
\
for (iti_i = 0; iti_i < len; iti_i++) \
{ \
((int64_t *) out)[iti_i] \
= (int64_t) (((type *) in)[iti_i]); \
} \
} while (0)
#define SDC_DOWNCONVERT(in_type, in, out_type, out, max, min, len) \
do \
{ \
size_t dc_i; \
\
for (dc_i = 0; dc_i < len; dc_i++) \
{ \
if (((in_type *) in)[dc_i] < min) \
{ \
((out_type *) out)[dc_i] = min; \
} \
else if (((in_type *) in)[dc_i] > max) \
{ \
((out_type *) out)[dc_i] = max; \
} \
else \
{ \
((out_type *) out)[dc_i] \
= (out_type) (((in_type *) in)[dc_i]); \
} \
} \
} while (0)
enum dataType float_out = FLOAT_32;
enum
{
INCOMING = 0,
OUTGOING
};
struct
{
size_t type[2][NUM_DATA_TYPE];
} conversion_info = {0};
/* IEEE double-precision float */
/* 1 sign bit
* 11 exponent bits
* 52 fraction bits */
/* IEEE single-precision floating point */
/* 1 sign bit
* 8 exponent bits
* 23 fraction bits */
/* IEEE half-precision float */
/* 1 sign bit
* 5 exponent bits
* 10 fraction bits */
/* Brain float*/
/* 1 sign bit
* 8 exponent bits
* 7 fraction bits */
void dumpTypeInfo(void)
{
size_t i;
verbosePrintf("\nData type conversion information\n");
for (i = 0; i < UNSIGNED_8; i++)
{
verbosePrintf("%s:\t%-4lu -> %lu\n",
dtype_strs[i],
conversion_info.type[INCOMING][i],
conversion_info.type[OUTGOING][i]);
}
}
/* Helper function to get around strict aliasing */
static uint32_t asU32(const float in)
{
uint32_t out = 0;
memcpy(&out, &in, sizeof(float));
return out;
}
static float asF32(const uint32_t in)
{
float out = 0;
memcpy(&out, &in, sizeof(float));
return out;
}
/* Adapted from Maratyszcza's FP16 header library */
static uint16_t fltToHlf(const float in)
{
const float scale_to_inf = asF32(0x77800000);
const float scale_to_zero = asF32(0x08800000);
const uint32_t u32_in = asU32(in);
const uint32_t shl1 = u32_in << 1;
const uint32_t sign = u32_in & 0x80000000;
uint32_t bias = SDC_MAX((shl1 & 0xFF000000), 0x71000000);
float base = ((((in < 0.f) ? -in : in) * scale_to_inf) * scale_to_zero)
+ asF32((bias >> 1) + 0x07800000);
uint32_t nonsign = ((asU32(base) >> 13) & 0x00007C00)
+ (asU32(base) & 0x00000FFF);
return (uint16_t) ((sign >> 16) | ((shl1 > 0xFF000000)
? 0x7E00 : nonsign));
}
static uint16_t dblToBft(const double in)
{
const float flt = ((float) in) * 1.001957f;
return (uint16_t) (asU32(flt) >> 16);
}
static void doubleToHalf(const double *in, uint16_t *out, const size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
out[i] = fltToHlf((float) in[i]);
}
}
static void signed64ToHalf(const int64_t *in, uint16_t *out, const size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
out[i] = fltToHlf((float) in[i]);
}
}
static void doubleToBrain(const double *in, uint16_t *out, const size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
out[i] = dblToBft(in[i]);
}
}
static void signed64ToBrain(const int64_t *in, uint16_t *out, const size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
out[i] = dblToBft((double) in[i]);
}
}
/* All float values can be represented accurately as a double so this rather
* trivial */
static void floatToDouble(const float *in, double *out, const size_t len)
{
size_t i;
for (i = 0; i < len; i++)
{
out[i] = (double) in[i];
}
return;
}
char* downConvertDTypes(char *in, const size_t len,
const enum dataType in_type, enum dataType *out_type)
{
/* Both int64_t and double should be eight bytes long */
char *tmp_arr = calloc(len, 8);
char *out_arr = NULL;
conversion_info.type[INCOMING][in_type]++;
switch (in_type)
{
case FLOAT_64:
memcpy(tmp_arr, in, len * sizeof(double));
break;
case FLOAT_32:
/* SDC_FLT_TO_F64(float, in, tmp_arr, len); */
floatToDouble((float *) in, (double *) tmp_arr, len);
break;
/* If the input is either F16 or BF16 already there isn't
* much reason to touch them at the moment */
case FLOAT_16:
*out_type = FLOAT_16;
out_arr = malloc(sizeof(uint16_t) * len);
memcpy(out_arr, in, len * sizeof(uint16_t));
conversion_info.type[OUTGOING][*out_type]++;
free(tmp_arr);
return out_arr;
case BFLOAT_16:
*out_type = BFLOAT_16;
out_arr = malloc(sizeof(uint16_t) * len);
memcpy(out_arr, in, len * sizeof(uint16_t));
conversion_info.type[OUTGOING][*out_type]++;
free(tmp_arr);
return out_arr;
case SIGNED_64:
memcpy(tmp_arr, in, len * sizeof(int64_t));
break;
case SIGNED_32:
SDC_INT_TO_I64(int32_t, in, tmp_arr, len);
break;
case SIGNED_16:
SDC_INT_TO_I64(int16_t, in, tmp_arr, len);
break;
case SIGNED_8:
SDC_INT_TO_I64(int8_t, in, tmp_arr, len);
break;
case UNSIGNED_8:
SDC_INT_TO_I64(uint8_t, in, tmp_arr, len);
break;
case BOOLEAN: /* fallthrough */
default:
fprintf(stderr, "Unsupported dtype\n");
free(tmp_arr);
return NULL;
}
*out_type = float_out;
conversion_info.type[OUTGOING][*out_type]++;
out_arr = malloc(dtype_info[*out_type].size * len);
/* down-convert to target float */
switch (*out_type)
{
case FLOAT_32:
if (SDC_DTYPE_IS_FLOAT(in_type) == SDC_TRUE)
{
SDC_DOWNCONVERT(double, tmp_arr,
float, out_arr,
FLT_MAX, -FLT_MAX, len);
}
else
{
SDC_DOWNCONVERT(int64_t, tmp_arr,
float, out_arr,
FLT_MAX, -FLT_MAX, len);
}
break;
case FLOAT_16:
if (SDC_DTYPE_IS_FLOAT(in_type) == SDC_TRUE)
{
doubleToHalf((double *) tmp_arr,
(uint16_t *) out_arr, len);
}
else
{
signed64ToHalf((int64_t *) tmp_arr,
(uint16_t *) out_arr, len);
}
break;
case BFLOAT_16:
if (SDC_DTYPE_IS_FLOAT(in_type) == SDC_TRUE)
{
doubleToBrain((double *) tmp_arr,
(uint16_t *) out_arr, len);
}
else
{
signed64ToBrain((int64_t *) tmp_arr,
(uint16_t *) out_arr, len);
}
break;
default:
fprintf(stderr, "Bad output type: %s\n",
dtype_info[*out_type].name);
free(tmp_arr);
free(out_arr);
return NULL;
}
free(tmp_arr);
return out_arr;
}