1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2020-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_BFLOAT16_H
25*c217d954SCole Faust #define ARM_COMPUTE_BFLOAT16_H
26*c217d954SCole Faust
27*c217d954SCole Faust #include <cstdint>
28*c217d954SCole Faust #include <cstring>
29*c217d954SCole Faust
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace
33*c217d954SCole Faust {
34*c217d954SCole Faust /** Convert float to bfloat16
35*c217d954SCole Faust *
36*c217d954SCole Faust * @param[in] v Floating-point value to convert to bfloat
37*c217d954SCole Faust *
38*c217d954SCole Faust * @return Converted value
39*c217d954SCole Faust */
float_to_bf16(const float v)40*c217d954SCole Faust inline uint16_t float_to_bf16(const float v)
41*c217d954SCole Faust {
42*c217d954SCole Faust const uint32_t *fromptr = reinterpret_cast<const uint32_t *>(&v);
43*c217d954SCole Faust #if defined(ARM_COMPUTE_ENABLE_BF16)
44*c217d954SCole Faust uint16_t res;
45*c217d954SCole Faust
46*c217d954SCole Faust __asm __volatile(
47*c217d954SCole Faust "ldr s0, [%[fromptr]]\n"
48*c217d954SCole Faust ".inst 0x1e634000\n" // BFCVT h0, s0
49*c217d954SCole Faust "str h0, [%[toptr]]\n"
50*c217d954SCole Faust :
51*c217d954SCole Faust : [fromptr] "r"(fromptr), [toptr] "r"(&res)
52*c217d954SCole Faust : "v0", "memory");
53*c217d954SCole Faust #else /* defined(ARM_COMPUTE_ENABLE_BF16) */
54*c217d954SCole Faust uint16_t res = (*fromptr >> 16);
55*c217d954SCole Faust const uint16_t error = (*fromptr & 0x0000ffff);
56*c217d954SCole Faust uint16_t bf_l = res & 0x0001;
57*c217d954SCole Faust if((error > 0x8000) || ((error == 0x8000) && (bf_l != 0)))
58*c217d954SCole Faust {
59*c217d954SCole Faust res += 1;
60*c217d954SCole Faust }
61*c217d954SCole Faust #endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
62*c217d954SCole Faust return res;
63*c217d954SCole Faust }
64*c217d954SCole Faust
65*c217d954SCole Faust /** Convert bfloat16 to float
66*c217d954SCole Faust *
67*c217d954SCole Faust * @param[in] v Bfloat16 value to convert to float
68*c217d954SCole Faust *
69*c217d954SCole Faust * @return Converted value
70*c217d954SCole Faust */
bf16_to_float(const uint16_t & v)71*c217d954SCole Faust inline float bf16_to_float(const uint16_t &v)
72*c217d954SCole Faust {
73*c217d954SCole Faust const uint32_t lv = (v << 16);
74*c217d954SCole Faust float fp;
75*c217d954SCole Faust memcpy(&fp, &lv, sizeof(lv));
76*c217d954SCole Faust return fp;
77*c217d954SCole Faust }
78*c217d954SCole Faust }
79*c217d954SCole Faust
80*c217d954SCole Faust /** Brain floating point representation class */
81*c217d954SCole Faust class bfloat16 final
82*c217d954SCole Faust {
83*c217d954SCole Faust public:
84*c217d954SCole Faust /** Default Constructor */
bfloat16()85*c217d954SCole Faust bfloat16()
86*c217d954SCole Faust : value(0)
87*c217d954SCole Faust {
88*c217d954SCole Faust }
89*c217d954SCole Faust /** Constructor
90*c217d954SCole Faust *
91*c217d954SCole Faust * @param[in] v Floating-point value
92*c217d954SCole Faust */
bfloat16(float v)93*c217d954SCole Faust bfloat16(float v)
94*c217d954SCole Faust : value(float_to_bf16(v))
95*c217d954SCole Faust {
96*c217d954SCole Faust }
97*c217d954SCole Faust /** Assignment operator
98*c217d954SCole Faust *
99*c217d954SCole Faust * @param[in] v Floating point value to assign
100*c217d954SCole Faust *
101*c217d954SCole Faust * @return The updated object
102*c217d954SCole Faust */
103*c217d954SCole Faust bfloat16 &operator=(float v)
104*c217d954SCole Faust {
105*c217d954SCole Faust value = float_to_bf16(v);
106*c217d954SCole Faust return *this;
107*c217d954SCole Faust }
108*c217d954SCole Faust /** Floating point conversion operator
109*c217d954SCole Faust *
110*c217d954SCole Faust * @return Floating point representation of the value
111*c217d954SCole Faust */
112*c217d954SCole Faust operator float() const
113*c217d954SCole Faust {
114*c217d954SCole Faust return bf16_to_float(value);
115*c217d954SCole Faust }
116*c217d954SCole Faust /** Lowest representative value
117*c217d954SCole Faust *
118*c217d954SCole Faust * @return Returns the lowest finite value representable by bfloat16
119*c217d954SCole Faust */
lowest()120*c217d954SCole Faust static bfloat16 lowest()
121*c217d954SCole Faust {
122*c217d954SCole Faust bfloat16 val;
123*c217d954SCole Faust val.value = 0xFF7F;
124*c217d954SCole Faust return val;
125*c217d954SCole Faust }
126*c217d954SCole Faust /** Largest representative value
127*c217d954SCole Faust *
128*c217d954SCole Faust * @return Returns the largest finite value representable by bfloat16
129*c217d954SCole Faust */
max()130*c217d954SCole Faust static bfloat16 max()
131*c217d954SCole Faust {
132*c217d954SCole Faust bfloat16 val;
133*c217d954SCole Faust val.value = 0x7F7F;
134*c217d954SCole Faust return val;
135*c217d954SCole Faust }
136*c217d954SCole Faust
137*c217d954SCole Faust private:
138*c217d954SCole Faust uint16_t value;
139*c217d954SCole Faust };
140*c217d954SCole Faust } // namespace arm_compute
141*c217d954SCole Faust #endif /* ARM_COMPUTE_BFLOAT16_H */
142