Convert float to bfloat16 in a portable way that works on older hardware * * @param[in] v Floating-point value to convert to bfloat * * @return Converted value */
| 38 | * @return Converted value |
| 39 | */ |
| 40 | inline uint16_t portable_float_to_bf16(const float v) |
| 41 | { |
| 42 | const uint32_t *fromptr = reinterpret_cast<const uint32_t *>(&v); |
| 43 | uint16_t res = (*fromptr >> 16); |
| 44 | const uint16_t error = (*fromptr & 0x0000ffff); |
| 45 | uint16_t bf_l = res & 0x0001; |
| 46 | |
| 47 | if ((error > 0x8000) || ((error == 0x8000) && (bf_l != 0))) |
| 48 | { |
| 49 | res += 1; |
| 50 | } |
| 51 | return res; |
| 52 | } |
| 53 | |
| 54 | /** Convert float to bfloat16 |
| 55 | * |
no outgoing calls
no test coverage detected