| 1 |
#ifndef FP8_HELPER_UNCLUDED |
| 2 |
#define FP8_HELPER_UNCLUDED |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <inttypes.h> |
| 6 |
|
| 7 |
typedef union |
| 8 |
{ |
| 9 |
_Float16 f16; |
| 10 |
unsigned short u16; |
| 11 |
} Float16Union; |
| 12 |
|
| 13 |
typedef union |
| 14 |
{ |
| 15 |
float f; |
| 16 |
uint32_t u; |
| 17 |
} Floatuint32Union; |
| 18 |
|
| 19 |
static unsigned char |
| 20 |
convert_fp16_to_hf8 (_Float16 x, unsigned char b, int s) |
| 21 |
{ |
| 22 |
Float16Union ux = { .f16 = x }; |
| 23 |
const unsigned short fp16_bias = 15, hf8_bias = 7; |
| 24 |
unsigned short sign = (ux.u16 & 0x8000) >> 8; |
| 25 |
unsigned short e_fp16 = (ux.u16 & 0x7c00) >> 10; |
| 26 |
unsigned short m_fp16 = ux.u16 & 0x03ff; |
| 27 |
|
| 28 |
/* If bias */ |
| 29 |
unsigned short x_bias = b ? ux.u16 + (b >> 1) : ux.u16; |
| 30 |
unsigned short e = (x_bias & 0x7c00) >> 10; |
| 31 |
unsigned short m = (x_bias & 0x03ff) >> 7; |
| 32 |
|
| 33 |
if (e_fp16 == 0x1f) |
| 34 |
{ |
| 35 |
/* Special value: NaN or Infinity. */ |
| 36 |
return (0xf << 3) | 0x7 | sign; |
| 37 |
} |
| 38 |
else if ((e_fp16 > (fp16_bias - hf8_bias + 15)) |
| 39 |
|| ((e_fp16 == (fp16_bias - hf8_bias + 15)) |
| 40 |
&& (m_fp16 > 0x0300))) |
| 41 |
{ |
| 42 |
/* Overflow: Return Max or NaN. */ |
| 43 |
return (0xf << 3) | (s ? 0x6 : 0x7) | sign; |
| 44 |
} |
| 45 |
else if (e_fp16 < fp16_bias - hf8_bias - 3) |
| 46 |
{ |
| 47 |
/* Value too small: Return zero. */ |
| 48 |
return sign; |
| 49 |
} |
| 50 |
else if (e_fp16 <= fp16_bias - hf8_bias) |
| 51 |
{ |
| 52 |
/* Denormalized value: Adjust mantissa. */ |
| 53 |
m = ((m_fp16 | 0x0400) >> ((fp16_bias - hf8_bias) + 1 - e_fp16)) |
| 54 |
| (((m_fp16 & 0x007f) + 0x007f) >> 7); |
| 55 |
return sign; |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
/* Normal value: Adjust exponent and mantissa. */ |
| 60 |
e -= (fp16_bias - hf8_bias); |
| 61 |
return (e << 3) | m | sign; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static unsigned char |
| 66 |
convert_fp16_to_bf8 (_Float16 x, unsigned char b, int s) |
| 67 |
{ |
| 68 |
Float16Union ux = { .f16 = x }; |
| 69 |
unsigned short temp; |
| 70 |
unsigned short fp8_res = 0; |
| 71 |
|
| 72 |
if (__builtin_isinf (x) || __builtin_isnan (x)) |
| 73 |
{ |
| 74 |
/* Special value: NaN or Infinity. */ |
| 75 |
fp8_res = (ux.u16 >> 8) & 0xFF; |
| 76 |
if (__builtin_isnan (x)) |
| 77 |
fp8_res |= 0x02; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
unsigned short rounding_bias = b ? b & 0xFF |
| 82 |
: ((ux.u16 >> 8) & 0x1) + 0x7F; |
| 83 |
temp = ux.u16 + rounding_bias; |
| 84 |
fp8_res = (temp >> 8) & 0xFF; |
| 85 |
if (((temp >> 8) & 0x7F) == 0x7C && s) |
| 86 |
fp8_res = (fp8_res & 0x80) | 0x7B; |
| 87 |
} |
| 88 |
return fp8_res; |
| 89 |
} |
| 90 |
|
| 91 |
static unsigned char |
| 92 |
convert_fp16_to_fp8 (_Float16 x, unsigned char b, int y, int s) |
| 93 |
{ |
| 94 |
return y ? convert_fp16_to_bf8 (x, b, s) |
| 95 |
: convert_fp16_to_hf8 (x, b, s); |
| 96 |
} |
| 97 |
|
| 98 |
static _Float16 |
| 99 |
convert_bf8_to_fp16(unsigned char x) |
| 100 |
{ |
| 101 |
Float16Union u = {.u16 = (x << 8) & 0xff00}; |
| 102 |
return u.f16; |
| 103 |
} |
| 104 |
|
| 105 |
static _Float16 |
| 106 |
convert_hf8_to_fp16(unsigned char x) |
| 107 |
{ |
| 108 |
unsigned char hf8_bias; |
| 109 |
Float16Union res; |
| 110 |
unsigned short fp_16bias, s, e, m, e_norm, lz_cnt; |
| 111 |
|
| 112 |
fp_16bias = 15; |
| 113 |
hf8_bias = 7; |
| 114 |
s = (x & 0x80) << 8; |
| 115 |
e = (x & 0x78) >> 3; |
| 116 |
m = x & 0x07; |
| 117 |
e_norm = e + fp_16bias - hf8_bias; |
| 118 |
|
| 119 |
/* convert denormal hf8 number into a normal fp16 number */ |
| 120 |
if ((e == 0) && (m !=0)) |
| 121 |
{ |
| 122 |
lz_cnt = 2; |
| 123 |
lz_cnt = (m > 0x1) ? 1 : lz_cnt; |
| 124 |
lz_cnt = (m > 0x3) ? 0 : lz_cnt; |
| 125 |
e_norm -= lz_cnt; |
| 126 |
m = (m << (lz_cnt + 1)) & 0x07; |
| 127 |
} |
| 128 |
else if ((e == 0) && (m == 0)) |
| 129 |
e_norm = 0; |
| 130 |
else if ((e == 0xf) && (m == 0x7)) |
| 131 |
{ |
| 132 |
e_norm = 0x1f; |
| 133 |
m = 0x4; |
| 134 |
} |
| 135 |
|
| 136 |
res.u16 = 0; |
| 137 |
res.u16 |= e_norm << 10; |
| 138 |
res.u16 |= m << 7; |
| 139 |
res.u16 |= s; |
| 140 |
|
| 141 |
return res.f16; |
| 142 |
} |
| 143 |
|
| 144 |
static float |
| 145 |
convert_bf8_to_fp32 (unsigned char x) |
| 146 |
{ |
| 147 |
Float16Union u = {.u16 = (x << 8) & 0xff00}; |
| 148 |
return (float)(u.f16); |
| 149 |
} |
| 150 |
|
| 151 |
static float |
| 152 |
convert_hf8_to_fp32 (unsigned char x) |
| 153 |
{ |
| 154 |
Floatuint32Union res = {.f = 0.0f}; |
| 155 |
unsigned int s = (x & 0x80) << 24, e = (x & 0x78) >> 3, m = x & 0x07; |
| 156 |
unsigned int e_norm = e + 120; |
| 157 |
|
| 158 |
if (!e && m) |
| 159 |
{ |
| 160 |
unsigned int lz_cnt = 2 - (m > 1) - (m > 3); |
| 161 |
e_norm -= lz_cnt; |
| 162 |
m = (m << (lz_cnt + 1) & 0x7); |
| 163 |
} |
| 164 |
else if (!e) |
| 165 |
e_norm = 0; |
| 166 |
else if (e == 0xf && m == 0x7) |
| 167 |
e_norm = 255, m = 4; |
| 168 |
|
| 169 |
res.u |= (e_norm << 23) | (m << 20) | s; |
| 170 |
|
| 171 |
return res.f; |
| 172 |
} |
| 173 |
|
| 174 |
static float |
| 175 |
convert_fp8_to_fp32 (unsigned char x, int y) |
| 176 |
{ |
| 177 |
return y ? convert_bf8_to_fp32 (x) |
| 178 |
: convert_hf8_to_fp32 (x); |
| 179 |
} |
| 180 |
|
| 181 |
static int |
| 182 |
fp8_isNan(unsigned char value, int bf8) |
| 183 |
{ |
| 184 |
unsigned char e = bf8 ? (value >> 2) & 0b11111 : (value >> 3) & 0b1111; |
| 185 |
unsigned char m = bf8 ? value & 0b11 : value & 0b111; |
| 186 |
|
| 187 |
return bf8 ? (e == 0b11111 && m != 0) : (e == 0b1111 && m == 0b111); |
| 188 |
} |
| 189 |
|
| 190 |
static int |
| 191 |
bf8_isInf(unsigned char value) |
| 192 |
{ |
| 193 |
unsigned char sign = value >> 7; |
| 194 |
unsigned char e = (value >> 2) & 0b11111; |
| 195 |
unsigned char m = value & 0b11; |
| 196 |
|
| 197 |
if (e == 0b11111 && !m) |
| 198 |
return sign ? -1 : 1; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
#ifdef __x86_64__ |
| 203 |
/* type = 0 for hf8 and type = 1 for bf8 */ |
| 204 |
/* value is 2^16*x for bf8 and 2^9*x for hf8 */ |
| 205 |
/* +-3 means +-inf, |
| 206 |
+-2 means +-Nan, |
| 207 |
and we will use -2 only, |
| 208 |
+-1 means positive and negative no rmal numbers, |
| 209 |
0 means undefined/not initialised */ |
| 210 |
static int64_t |
| 211 |
shift_fp8_to_int64 (unsigned char x, int bf8, int *valueState) |
| 212 |
{ |
| 213 |
*valueState = (x >> 7) ? -1 : 1; |
| 214 |
if (fp8_isNan (x, bf8)) |
| 215 |
*valueState = -2; |
| 216 |
if (bf8) |
| 217 |
if (bf8_isInf(x) == 1) |
| 218 |
*valueState = 3; |
| 219 |
else if (bf8_isInf(x) == -1) |
| 220 |
*valueState = -3; |
| 221 |
|
| 222 |
unsigned short sign = (x & 0x80) >> 7; |
| 223 |
unsigned short exp = bf8 ? (x & 0x7c) >> 2 : (x & 0x78) >> 3; |
| 224 |
unsigned short frac = bf8 ? (x & 0x03) : (x & 0x07); |
| 225 |
unsigned short mant = (exp == 0) ? frac : (bf8 ? (frac | 0x4) : (frac | 0x8)); |
| 226 |
unsigned short e_count = (exp == 0) ? 0 : exp - 1; |
| 227 |
int64_t magnitude = (int64_t)mant << (int64_t)e_count; |
| 228 |
|
| 229 |
return sign ? -magnitude : magnitude; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/* type = 0 for hf8 and type = 1 for bf8 */ |
| 234 |
static float |
| 235 |
shift_int128_to_fp32 (__int128_t in, int type1, int type2) |
| 236 |
{ |
| 237 |
if (in == 0) |
| 238 |
return 0; |
| 239 |
|
| 240 |
unsigned short sign = (in >> 127) & 1; |
| 241 |
unsigned short Jbit_position = 126; |
| 242 |
unsigned short fac = (type1 == type2) ? ( (type1) ? 32 : 18) : 25; |
| 243 |
|
| 244 |
__int128_t magnitude = sign ? -in : in; |
| 245 |
|
| 246 |
while (((magnitude >> 126) & 1) == 0) |
| 247 |
{ |
| 248 |
Jbit_position --; |
| 249 |
magnitude <<= 1; |
| 250 |
} |
| 251 |
|
| 252 |
__int128_t sticky = (magnitude & (((__int128_t)1 << 102) - 1)) != 0; |
| 253 |
__int128_t Gbit = (magnitude >> 102) & 1; |
| 254 |
__int128_t Lbit = (magnitude >> 103) & 1; |
| 255 |
__int128_t RndAddl = Gbit & ( Lbit | sticky); |
| 256 |
__int128_t mant = (magnitude >> 103) & (((__int128_t)1 << 25) - 1); |
| 257 |
|
| 258 |
__int128_t Rndmant = mant + RndAddl; |
| 259 |
__int128_t Ovf = Rndmant >> 24; |
| 260 |
__int128_t exp = 127 + Jbit_position - fac + Ovf; |
| 261 |
__int128_t frac = Rndmant & 0x7FFFFF; |
| 262 |
|
| 263 |
Floatuint32Union res; |
| 264 |
res.u = sign << 31; |
| 265 |
res.u |= exp << 23; |
| 266 |
res.u |= frac; |
| 267 |
|
| 268 |
return res.f; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
#endif |