Loading [MathJax]/extensions/tex2jax.js
libTriton version 1.0 build 1599
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
softfloat.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
9
10#include <cstring>
11
12namespace triton {
13 namespace sf {
14
15 uint16_t f32_to_f16(float value) {
16 uint32_t f;
17
18 static_assert(sizeof(float) == sizeof(uint32_t), "Unexpected float type size");
19 std::memcpy(&f, &value, sizeof(uint32_t));
20
21 uint16_t sign = ((f >> 16) & 0x8000);
22 int16_t exponent = ((f >> 23) & 0xff) - 127 + 15;
23 uint16_t mantissa = ((f >> 13) & 0x3ff);
24
25 if (exponent <= 0) {
26 if (exponent < -10) {
27 return sign;
28 }
29 mantissa = (mantissa | 0x400) >> (1 - exponent);
30 return sign | mantissa;
31 }
32
33 else if (exponent == 0xff - (127 - 15)) {
34 if (mantissa) {
35 return sign | 0x7fff;
36 } else {
37 return sign | 0x7c00;
38 }
39 }
40
41 else if (exponent > 30) {
42 return sign | 0x7c00;
43 }
44
45 return sign | (exponent << 10) | mantissa;
46 }
47
48 }; /* sf namespace */
49}; /* triton namespace */
auto f32_to_f16(float value) -> uint16_t
Cast 32-bit floating point value to 16-bit according to IEEE-754.
Definition softfloat.cpp:15
The Triton namespace.