• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 #include <functional>
21 #include <iosfwd>
22 #include <limits>
23 #include <type_traits>
24 
25 #ifndef LIKELY
26 #define LIKELY_DEFINED_LOCAL
27 #ifdef __cplusplus
28 #   define LIKELY( exp )    (__builtin_expect( !!(exp), true ))
29 #   define UNLIKELY( exp )  (__builtin_expect( !!(exp), false ))
30 #else
31 #   define LIKELY( exp )    (__builtin_expect( !!(exp), 1 ))
32 #   define UNLIKELY( exp )  (__builtin_expect( !!(exp), 0 ))
33 #endif
34 #endif
35 
36 #if __cplusplus >= 201402L
37 #define CONSTEXPR constexpr
38 #else
39 #define CONSTEXPR
40 #endif
41 
42 namespace android {
43 
44 /*
45  * half-float
46  *
47  *  1   5       10
48  * +-+------+------------+
49  * |s|eee.ee|mm.mmmm.mmmm|
50  * +-+------+------------+
51  *
52  * minimum (denormal) value: 2^-24 = 5.96e-8
53  * minimum (normal) value:   2^-14 = 6.10e-5
54  * maximum value:            2-2^-10 = 65504
55  *
56  * Integers between 0 and 2048 can be represented exactly
57  */
58 class half {
59     struct fp16 {
60         uint16_t bits;
fp16fp1661         explicit constexpr fp16() noexcept : bits(0) { }
fp16fp1662         explicit constexpr fp16(uint16_t b) noexcept : bits(b) { }
setSfp1663         void setS(unsigned int s) noexcept { bits = uint16_t((bits & 0x7FFF) | (s<<15)); }
setEfp1664         void setE(unsigned int s) noexcept { bits = uint16_t((bits & 0xE3FF) | (s<<10)); }
setMfp1665         void setM(unsigned int s) noexcept { bits = uint16_t((bits & 0xFC00) | (s<< 0)); }
getSfp1666         constexpr unsigned int getS() const noexcept { return  bits >> 15u; }
getEfp1667         constexpr unsigned int getE() const noexcept { return (bits >> 10u) & 0x1Fu; }
getMfp1668         constexpr unsigned int getM() const noexcept { return  bits         & 0x3FFu; }
69     };
70     struct fp32 {
71         union {
72             uint32_t bits;
73             float fp;
74         };
fp32fp3275         explicit constexpr fp32() noexcept : bits(0) { }
fp32fp3276         explicit constexpr fp32(float f) noexcept : fp(f) { }
setSfp3277         void setS(unsigned int s) noexcept { bits = uint32_t((bits & 0x7FFFFFFF) | (s<<31)); }
setEfp3278         void setE(unsigned int s) noexcept { bits = uint32_t((bits & 0x807FFFFF) | (s<<23)); }
setMfp3279         void setM(unsigned int s) noexcept { bits = uint32_t((bits & 0xFF800000) | (s<< 0)); }
getSfp3280         constexpr unsigned int getS() const noexcept { return  bits >> 31u; }
getEfp3281         constexpr unsigned int getE() const noexcept { return (bits >> 23u) & 0xFFu; }
getMfp3282         constexpr unsigned int getM() const noexcept { return  bits         & 0x7FFFFFu; }
83     };
84 
85 public:
half()86     CONSTEXPR half() noexcept { }
half(float v)87     CONSTEXPR half(float v) noexcept : mBits(ftoh(v)) { }
88     CONSTEXPR operator float() const noexcept { return htof(mBits); }
89 
getBits()90     uint16_t getBits() const noexcept { return mBits.bits; }
getExponent()91     unsigned int getExponent() const noexcept { return mBits.getE(); }
getMantissa()92     unsigned int getMantissa() const noexcept { return mBits.getM(); }
93 
94 private:
95     friend class std::numeric_limits<half>;
96     friend CONSTEXPR half operator"" _hf(long double v);
97 
98     enum Binary { binary };
half(Binary,uint16_t bits)99     explicit constexpr half(Binary, uint16_t bits) noexcept : mBits(bits) { }
100     static CONSTEXPR fp16 ftoh(float v) noexcept;
101     static CONSTEXPR float htof(fp16 v) noexcept;
102     fp16 mBits;
103 };
104 
ftoh(float v)105 inline CONSTEXPR half::fp16 half::ftoh(float v) noexcept {
106     fp16 out;
107     fp32 in(v);
108     if (UNLIKELY(in.getE() == 0xFF)) { // inf or nan
109         out.setE(0x1F);
110         out.setM(in.getM() ? 0x200 : 0);
111     } else {
112         int e = static_cast<int>(in.getE()) - 127 + 15;
113         if (e >= 0x1F) {
114             // overflow
115             out.setE(0x31); // +/- inf
116         } else if (e <= 0) {
117             // underflow
118             // flush to +/- 0
119         } else {
120             unsigned int m = in.getM();
121             out.setE(uint16_t(e));
122             out.setM(m >> 13);
123             if (m & 0x1000) {
124                 // rounding
125                 out.bits++;
126             }
127         }
128     }
129     out.setS(in.getS());
130     return out;
131 }
132 
htof(half::fp16 in)133 inline CONSTEXPR float half::htof(half::fp16 in) noexcept {
134     fp32 out;
135     if (UNLIKELY(in.getE() == 0x1F)) { // inf or nan
136         out.setE(0xFF);
137         out.setM(in.getM() ? 0x400000 : 0);
138     } else {
139         if (in.getE() == 0) {
140             if (in.getM()) {
141                 // TODO: denormal half float, treat as zero for now
142                 // (it's stupid because they can be represented as regular float)
143             }
144         } else {
145             int e = static_cast<int>(in.getE()) - 15 + 127;
146             unsigned int m = in.getM();
147             out.setE(uint32_t(e));
148             out.setM(m << 13);
149         }
150     }
151     out.setS(in.getS());
152     return out.fp;
153 }
154 
155 inline CONSTEXPR android::half operator"" _hf(long double v) {
156     return android::half(android::half::binary, android::half::ftoh(static_cast<float>(v)).bits);
157 }
158 
159 } // namespace android
160 
161 namespace std {
162 
163 template<> struct is_floating_point<android::half> : public std::true_type {};
164 
165 template<>
166 class numeric_limits<android::half> {
167 public:
168     typedef android::half type;
169 
170     static constexpr const bool is_specialized = true;
171     static constexpr const bool is_signed = true;
172     static constexpr const bool is_integer = false;
173     static constexpr const bool is_exact = false;
174     static constexpr const bool has_infinity = true;
175     static constexpr const bool has_quiet_NaN = true;
176     static constexpr const bool has_signaling_NaN = false;
177     static constexpr const float_denorm_style has_denorm = denorm_absent;
178     static constexpr const bool has_denorm_loss = true;
179     static constexpr const bool is_iec559 = false;
180     static constexpr const bool is_bounded = true;
181     static constexpr const bool is_modulo = false;
182     static constexpr const bool traps = false;
183     static constexpr const bool tinyness_before = false;
184     static constexpr const float_round_style round_style = round_indeterminate;
185 
186     static constexpr const int digits = 11;
187     static constexpr const int digits10 = 3;
188     static constexpr const int max_digits10 = 5;
189     static constexpr const int radix = 2;
190     static constexpr const int min_exponent = -13;
191     static constexpr const int min_exponent10 = -4;
192     static constexpr const int max_exponent = 16;
193     static constexpr const int max_exponent10 = 4;
194 
195     inline static constexpr type round_error() noexcept { return android::half(android::half::binary, 0x3800); }
196     inline static constexpr type min() noexcept { return android::half(android::half::binary, 0x0400); }
197     inline static constexpr type max() noexcept { return android::half(android::half::binary, 0x7bff); }
198     inline static constexpr type lowest() noexcept { return android::half(android::half::binary, 0xfbff); }
199     inline static constexpr type epsilon() noexcept { return android::half(android::half::binary, 0x1400); }
200     inline static constexpr type infinity() noexcept { return android::half(android::half::binary, 0x7c00); }
201     inline static constexpr type quiet_NaN() noexcept { return android::half(android::half::binary, 0x7fff); }
202     inline static constexpr type denorm_min() noexcept { return android::half(android::half::binary, 0x0001); }
203     inline static constexpr type signaling_NaN() noexcept { return android::half(android::half::binary, 0x7dff); }
204 };
205 
206 template<> struct hash<android::half> {
207     size_t operator()(const android::half& half) {
208         return std::hash<float>{}(half);
209     }
210 };
211 
212 } // namespace std
213 
214 #ifdef LIKELY_DEFINED_LOCAL
215 #undef LIKELY_DEFINED_LOCAL
216 #undef LIKELY
217 #undef UNLIKELY
218 #endif // LIKELY_DEFINED_LOCAL
219 
220 #undef CONSTEXPR
221