blob: cbc1ef022da8f13619a70ffbc035fc8fb16a6c4d [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
17#ifndef UI_HALF_H
18#define UI_HALF_H
19
20#include <stdint.h>
21#include <iosfwd>
22#include <limits>
23#include <type_traits>
24
25#ifdef __cplusplus
26# define LIKELY( exp ) (__builtin_expect( !!(exp), true ))
27# define UNLIKELY( exp ) (__builtin_expect( !!(exp), false ))
28#else
29# define LIKELY( exp ) (__builtin_expect( !!(exp), 1 ))
30# define UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 ))
31#endif
32
33namespace android {
34
35/*
36 * half-float
37 *
38 * 1 5 10
39 * +-+------+------------+
40 * |s|eee.ee|mm.mmmm.mmmm|
41 * +-+------+------------+
42 *
43 * minimum (denormal) value: 2^-24 = 5.96e-8
44 * minimum (normal) value: 2^-14 = 6.10e-5
45 * maximum value: 2-2^-10 = 65504
46 *
47 * Integers between 0 and 2048 can be represented exactly
48 */
49class half {
50 struct fp16 {
51 uint16_t bits = 0;
52 fp16() noexcept = default;
53 explicit constexpr fp16(uint16_t bits) noexcept : bits(bits) { }
54 void setS(unsigned int s) noexcept { bits = uint16_t((bits & 0x7FFF) | (s<<15)); }
55 void setE(unsigned int s) noexcept { bits = uint16_t((bits & 0xE3FF) | (s<<10)); }
56 void setM(unsigned int s) noexcept { bits = uint16_t((bits & 0xFC00) | (s<< 0)); }
57 unsigned int getS() const noexcept { return bits >> 15u; }
58 unsigned int getE() const noexcept { return (bits >> 10u) & 0x1Fu; }
59 unsigned int getM() const noexcept { return bits & 0x3FFu; }
60 };
61 struct fp32 {
62 union {
63 uint32_t bits = 0;
64 float fp;
65 };
66 fp32() noexcept = default;
67 explicit constexpr fp32(float f) : fp(f) { }
68 void setS(unsigned int s) noexcept { bits = uint32_t((bits & 0x7FFFFFFF) | (s<<31)); }
69 void setE(unsigned int s) noexcept { bits = uint32_t((bits & 0x807FFFFF) | (s<<23)); }
70 void setM(unsigned int s) noexcept { bits = uint32_t((bits & 0xFF800000) | (s<< 0)); }
71 unsigned int getS() const noexcept { return bits >> 31u; }
72 unsigned int getE() const noexcept { return (bits >> 23u) & 0xFFu; }
73 unsigned int getM() const noexcept { return bits & 0x7FFFFFu; }
74 };
75
76public:
77 half(float v) noexcept : mBits(ftoh(v)) { }
78 operator float() const noexcept { return htof(mBits); }
79
80 uint16_t getBits() const noexcept { return mBits.bits; }
81 unsigned int getExponent() const noexcept { return mBits.getE(); }
82 unsigned int getMantissa() const noexcept { return mBits.getM(); }
83
84private:
85 friend class std::numeric_limits<half>;
86 friend half operator"" _hf(long double v);
87
88 enum Binary { binary };
89 explicit constexpr half(Binary, uint16_t bits) noexcept : mBits(bits) { }
90 static fp16 ftoh(float v) noexcept;
91 static float htof(fp16 v) noexcept;
92 fp16 mBits;
93};
94
95inline /* constexpr */ half::fp16 half::ftoh(float v) noexcept {
96 fp16 out;
97 fp32 in(v);
98 if (UNLIKELY(in.getE() == 0xFF)) { // inf or nan
99 out.setE(0x1F);
100 out.setM(in.getM() ? 0x200 : 0);
101 } else {
102 int e = in.getE() - 127 + 15;
103 if (e >= 0x1F) {
104 // overflow
105 out.setE(0x31); // +/- inf
106 } else if (e <= 0) {
107 // underflow
108 // flush to +/- 0
109 } else {
110 unsigned int m = in.getM();
111 out.setE(uint16_t(e));
112 out.setM(m >> 13);
113 if (m & 0x1000) {
114 // rounding
115 out.bits++;
116 }
117 }
118 }
119 out.setS(in.getS());
120 return out;
121}
122
123inline float half::htof(half::fp16 in) noexcept {
124 fp32 out;
125 if (UNLIKELY(in.getE() == 0x1F)) { // inf or nan
126 out.setE(0xFF);
127 out.setM(in.getM() ? 0x400000 : 0);
128 } else {
129 if (in.getE() == 0) {
130 if (in.getM()) {
131 // TODO: denormal half float, treat as zero for now
132 // (it's stupid because they can be represented as regular float)
133 }
134 } else {
135 int e = in.getE() - 15 + 127;
136 unsigned int m = in.getM();
137 out.setE(uint32_t(e));
138 out.setM(m << 13);
139 }
140 }
141 out.setS(in.getS());
142 return out.fp;
143}
144
145inline /* constexpr */ android::half operator"" _hf(long double v) {
146 return android::half(android::half::binary, android::half::ftoh(v).bits);
147}
148
149} // namespace android
150
151namespace std {
152
153template<> struct is_floating_point<android::half> : public std::true_type {};
154
155template<>
156class numeric_limits<android::half> {
157public:
158 typedef android::half type;
159
160 static constexpr const bool is_specialized = true;
161 static constexpr const bool is_signed = true;
162 static constexpr const bool is_integer = false;
163 static constexpr const bool is_exact = false;
164 static constexpr const bool has_infinity = true;
165 static constexpr const bool has_quiet_NaN = true;
166 static constexpr const bool has_signaling_NaN = false;
167 static constexpr const float_denorm_style has_denorm = denorm_absent;
168 static constexpr const bool has_denorm_loss = true;
169 static constexpr const bool is_iec559 = false;
170 static constexpr const bool is_bounded = true;
171 static constexpr const bool is_modulo = false;
172 static constexpr const bool traps = false;
173 static constexpr const bool tinyness_before = false;
174 static constexpr const float_round_style round_style = round_indeterminate;
175
176 static constexpr const int digits = 11;
177 static constexpr const int digits10 = 3;
178 static constexpr const int max_digits10 = 5;
179 static constexpr const int radix = 2;
180 static constexpr const int min_exponent = -13;
181 static constexpr const int min_exponent10 = -4;
182 static constexpr const int max_exponent = 16;
183 static constexpr const int max_exponent10 = 4;
184
185 inline static constexpr type round_error() noexcept { return android::half(android::half::binary, 0x3800); }
186 inline static constexpr type min() noexcept { return android::half(android::half::binary, 0x0400); }
187 inline static constexpr type max() noexcept { return android::half(android::half::binary, 0x7bff); }
188 inline static constexpr type lowest() noexcept { return android::half(android::half::binary, 0xfbff); }
189 inline static constexpr type epsilon() noexcept { return android::half(android::half::binary, 0x1400); }
190 inline static constexpr type infinity() noexcept { return android::half(android::half::binary, 0x7c00); }
191 inline static constexpr type quiet_NaN() noexcept { return android::half(android::half::binary, 0x7fff); }
192 inline static constexpr type denorm_min() noexcept { return android::half(android::half::binary, 0x0001); }
193 inline static constexpr type signaling_NaN() noexcept { return android::half(android::half::binary, 0x7dff); }
194};
195
196} // namespace std
197
198#undef LIKELY
199#undef UNLIKELY
200
201#endif // UI_HALF_H