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