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