blob: 5ec9bf7b4026af025491782272735e6f416f730e [file] [log] [blame]
Romain Guy5d4bae72016-11-08 09:49:25 -08001/*
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
Mathias Agopian1d77b712017-02-17 15:46:13 -080017#pragma once
Romain Guy5d4bae72016-11-08 09:49:25 -080018
19#include <stdint.h>
Dan Stozaa3d8f052020-09-29 13:37:23 -070020#include <functional>
Romain Guy5d4bae72016-11-08 09:49:25 -080021#include <iosfwd>
22#include <limits>
23#include <type_traits>
24
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080025#ifndef LIKELY
Jaesoo Lee47df7e32017-03-24 13:52:12 +090026#define LIKELY_DEFINED_LOCAL
Romain Guy5d4bae72016-11-08 09:49:25 -080027#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
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080034#endif
Romain Guy5d4bae72016-11-08 09:49:25 -080035
Romain Guycaf2ca42016-11-10 11:45:58 -080036#if __cplusplus >= 201402L
37#define CONSTEXPR constexpr
38#else
39#define CONSTEXPR
40#endif
41
Romain Guy5d4bae72016-11-08 09:49:25 -080042namespace 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 */
58class half {
59 struct fp16 {
Yi Kong4fdbdd12017-05-11 11:51:08 -070060 uint16_t bits;
61 explicit constexpr fp16() noexcept : bits(0) { }
Romain Guycaf2ca42016-11-10 11:45:58 -080062 explicit constexpr fp16(uint16_t b) noexcept : bits(b) { }
Romain Guy5d4bae72016-11-08 09:49:25 -080063 void setS(unsigned int s) noexcept { bits = uint16_t((bits & 0x7FFF) | (s<<15)); }
64 void setE(unsigned int s) noexcept { bits = uint16_t((bits & 0xE3FF) | (s<<10)); }
65 void setM(unsigned int s) noexcept { bits = uint16_t((bits & 0xFC00) | (s<< 0)); }
Romain Guycaf2ca42016-11-10 11:45:58 -080066 constexpr unsigned int getS() const noexcept { return bits >> 15u; }
67 constexpr unsigned int getE() const noexcept { return (bits >> 10u) & 0x1Fu; }
68 constexpr unsigned int getM() const noexcept { return bits & 0x3FFu; }
Romain Guy5d4bae72016-11-08 09:49:25 -080069 };
70 struct fp32 {
71 union {
Yi Kong4fdbdd12017-05-11 11:51:08 -070072 uint32_t bits;
Romain Guy5d4bae72016-11-08 09:49:25 -080073 float fp;
74 };
Yi Kong4fdbdd12017-05-11 11:51:08 -070075 explicit constexpr fp32() noexcept : bits(0) { }
76 explicit constexpr fp32(float f) noexcept : fp(f) { }
Romain Guy5d4bae72016-11-08 09:49:25 -080077 void setS(unsigned int s) noexcept { bits = uint32_t((bits & 0x7FFFFFFF) | (s<<31)); }
78 void setE(unsigned int s) noexcept { bits = uint32_t((bits & 0x807FFFFF) | (s<<23)); }
79 void setM(unsigned int s) noexcept { bits = uint32_t((bits & 0xFF800000) | (s<< 0)); }
Romain Guycaf2ca42016-11-10 11:45:58 -080080 constexpr unsigned int getS() const noexcept { return bits >> 31u; }
81 constexpr unsigned int getE() const noexcept { return (bits >> 23u) & 0xFFu; }
82 constexpr unsigned int getM() const noexcept { return bits & 0x7FFFFFu; }
Romain Guy5d4bae72016-11-08 09:49:25 -080083 };
84
85public:
Krzysztof KosiƄski91a4df12020-05-21 00:18:54 -070086 CONSTEXPR half() noexcept { }
Romain Guycaf2ca42016-11-10 11:45:58 -080087 CONSTEXPR half(float v) noexcept : mBits(ftoh(v)) { }
88 CONSTEXPR operator float() const noexcept { return htof(mBits); }
Romain Guy5d4bae72016-11-08 09:49:25 -080089
90 uint16_t getBits() const noexcept { return mBits.bits; }
91 unsigned int getExponent() const noexcept { return mBits.getE(); }
92 unsigned int getMantissa() const noexcept { return mBits.getM(); }
93
94private:
95 friend class std::numeric_limits<half>;
Romain Guycaf2ca42016-11-10 11:45:58 -080096 friend CONSTEXPR half operator"" _hf(long double v);
Romain Guy5d4bae72016-11-08 09:49:25 -080097
98 enum Binary { binary };
99 explicit constexpr half(Binary, uint16_t bits) noexcept : mBits(bits) { }
Romain Guycaf2ca42016-11-10 11:45:58 -0800100 static CONSTEXPR fp16 ftoh(float v) noexcept;
101 static CONSTEXPR float htof(fp16 v) noexcept;
Romain Guy5d4bae72016-11-08 09:49:25 -0800102 fp16 mBits;
103};
104
Romain Guycaf2ca42016-11-10 11:45:58 -0800105inline CONSTEXPR half::fp16 half::ftoh(float v) noexcept {
Romain Guy5d4bae72016-11-08 09:49:25 -0800106 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 {
Romain Guycaf2ca42016-11-10 11:45:58 -0800112 int e = static_cast<int>(in.getE()) - 127 + 15;
Romain Guy5d4bae72016-11-08 09:49:25 -0800113 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
Romain Guycaf2ca42016-11-10 11:45:58 -0800133inline CONSTEXPR float half::htof(half::fp16 in) noexcept {
Romain Guy5d4bae72016-11-08 09:49:25 -0800134 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 {
Romain Guycaf2ca42016-11-10 11:45:58 -0800145 int e = static_cast<int>(in.getE()) - 15 + 127;
Romain Guy5d4bae72016-11-08 09:49:25 -0800146 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
Romain Guycaf2ca42016-11-10 11:45:58 -0800155inline CONSTEXPR android::half operator"" _hf(long double v) {
156 return android::half(android::half::binary, android::half::ftoh(static_cast<float>(v)).bits);
Romain Guy5d4bae72016-11-08 09:49:25 -0800157}
158
159} // namespace android
160
161namespace std {
162
163template<> struct is_floating_point<android::half> : public std::true_type {};
164
165template<>
166class numeric_limits<android::half> {
167public:
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
Dan Stozaa3d8f052020-09-29 13:37:23 -0700206template<> struct hash<android::half> {
207 size_t operator()(const android::half& half) {
208 return std::hash<float>{}(half);
209 }
210};
211
Romain Guy5d4bae72016-11-08 09:49:25 -0800212} // namespace std
213
Jaesoo Lee47df7e32017-03-24 13:52:12 +0900214#ifdef LIKELY_DEFINED_LOCAL
215#undef LIKELY_DEFINED_LOCAL
Romain Guy5d4bae72016-11-08 09:49:25 -0800216#undef LIKELY
217#undef UNLIKELY
Jaesoo Lee47df7e32017-03-24 13:52:12 +0900218#endif // LIKELY_DEFINED_LOCAL
219
Romain Guycaf2ca42016-11-10 11:45:58 -0800220#undef CONSTEXPR