| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkHalf_DEFINED | 8 #ifndef SkHalf_DEFINED |
| 9 #define SkHalf_DEFINED | 9 #define SkHalf_DEFINED |
| 10 | 10 |
| 11 #include "SkNx.h" | 11 #include "SkNx.h" |
| 12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
| 13 | 13 |
| 14 // 16-bit floating point value | 14 // 16-bit floating point value |
| 15 // format is 1 bit sign, 5 bits exponent, 10 bits mantissa | 15 // format is 1 bit sign, 5 bits exponent, 10 bits mantissa |
| 16 // only used for storage | 16 // only used for storage |
| 17 typedef uint16_t SkHalf; | 17 typedef uint16_t SkHalf; |
| 18 | 18 |
| 19 #define SK_HalfMin 0x0400 // 2^-24 (minimum positive normal value) | 19 static constexpr uint16_t SK_HalfMin = 0x0400; // 2^-24 (minimum positive n
ormal value) |
| 20 #define SK_HalfMax 0x7bff // 65504 | 20 static constexpr uint16_t SK_HalfMax = 0x7bff; // 65504 |
| 21 #define SK_HalfEpsilon 0x1400 // 2^-10 | 21 static constexpr uint16_t SK_HalfEpsilon = 0x1400; // 2^-10 |
| 22 static constexpr uint16_t SK_Half1 = 0x3C00; // 1 |
| 22 | 23 |
| 23 // convert between half and single precision floating point | 24 // convert between half and single precision floating point |
| 24 float SkHalfToFloat(SkHalf h); | 25 float SkHalfToFloat(SkHalf h); |
| 25 SkHalf SkFloatToHalf(float f); | 26 SkHalf SkFloatToHalf(float f); |
| 26 | 27 |
| 27 // Convert between half and single precision floating point, | 28 // Convert between half and single precision floating point, |
| 28 // assuming inputs and outputs are both finite. | 29 // assuming inputs and outputs are both finite. |
| 29 static inline Sk4f SkHalfToFloat_finite(uint64_t); | 30 static inline Sk4f SkHalfToFloat_finite(uint64_t); |
| 30 static inline uint64_t SkFloatToHalf_finite(const Sk4f&); | 31 static inline Sk4h SkFloatToHalf_finite(const Sk4f&); |
| 31 | 32 |
| 32 // ~~~~~~~~~~~ impl ~~~~~~~~~~~~~~ // | 33 // ~~~~~~~~~~~ impl ~~~~~~~~~~~~~~ // |
| 33 | 34 |
| 34 // Like the serial versions in SkHalf.cpp, these are based on | 35 // Like the serial versions in SkHalf.cpp, these are based on |
| 35 // https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/ | 36 // https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/ |
| 36 | 37 |
| 37 // GCC 4.9 lacks the intrinsics to use ARMv8 f16<->f32 instructions, so we use i
nline assembly. | 38 // GCC 4.9 lacks the intrinsics to use ARMv8 f16<->f32 instructions, so we use i
nline assembly. |
| 38 | 39 |
| 39 static inline Sk4f SkHalfToFloat_finite(uint64_t hs) { | 40 static inline Sk4f SkHalfToFloat_finite(uint64_t hs) { |
| 40 #if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64) | 41 #if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 58 // denorm value V*2^-14 into a normalized float K + V*2^-14. Then subtract
off K. | 59 // denorm value V*2^-14 into a normalized float K + V*2^-14. Then subtract
off K. |
| 59 const Sk4i K = ((127-15) + (23-10) + 1) << 23; | 60 const Sk4i K = ((127-15) + (23-10) + 1) << 23; |
| 60 Sk4i mask_K = positive | K; | 61 Sk4i mask_K = positive | K; |
| 61 Sk4f denorm = Sk4f::Load(&mask_K) - Sk4f::Load(&K); | 62 Sk4f denorm = Sk4f::Load(&mask_K) - Sk4f::Load(&K); |
| 62 | 63 |
| 63 Sk4i merged = (sign << 16) | is_denorm.thenElse(Sk4i::Load(&denorm), norm); | 64 Sk4i merged = (sign << 16) | is_denorm.thenElse(Sk4i::Load(&denorm), norm); |
| 64 return Sk4f::Load(&merged); | 65 return Sk4f::Load(&merged); |
| 65 #endif | 66 #endif |
| 66 } | 67 } |
| 67 | 68 |
| 68 static inline uint64_t SkFloatToHalf_finite(const Sk4f& fs) { | 69 static inline Sk4h SkFloatToHalf_finite(const Sk4f& fs) { |
| 69 uint64_t r; | |
| 70 #if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64) | 70 #if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64) |
| 71 float32x4_t vec = fs.fVec; | 71 float32x4_t vec = fs.fVec; |
| 72 asm ("fcvtn %[vec].4h, %[vec].4s \n" // vcvt_f16_f32(vec) | 72 asm ("fcvtn %[vec].4h, %[vec].4s \n" // vcvt_f16_f32(vec) |
| 73 "fmov %[r], %d[vec] \n" // vst1_f16(&r, ...) | 73 : [vec] "+w" (vec)); // +w: read-write NEON register |
| 74 : [r] "=r" (r) // =r: write-only 64-bit general reg
ister | 74 return vreinterpret_u16_f32(vget_low_f32(vec)); |
| 75 , [vec] "+w" (vec)); // +w: read-write NEON register | |
| 76 #else | 75 #else |
| 77 Sk4i bits = Sk4i::Load(&fs), | 76 Sk4i bits = Sk4i::Load(&fs), |
| 78 sign = bits & 0x80000000, // Save the sign bit f
or later... | 77 sign = bits & 0x80000000, // Save the sign bit f
or later... |
| 79 positive = bits ^ sign, // ...but strip it off
for now. | 78 positive = bits ^ sign, // ...but strip it off
for now. |
| 80 will_be_denorm = positive < ((127-15+1) << 23); // positve < smallest
normal half? | 79 will_be_denorm = positive < ((127-15+1) << 23); // positve < smallest
normal half? |
| 81 | 80 |
| 82 // For normal half floats, adjust the exponent from 127 bias to 15 bias, | 81 // For normal half floats, adjust the exponent from 127 bias to 15 bias, |
| 83 // then drop the bottom 13 mantissa bits. | 82 // then drop the bottom 13 mantissa bits. |
| 84 Sk4i norm = (positive - ((127 - 15) << 23)) >> 13; | 83 Sk4i norm = (positive - ((127 - 15) << 23)) >> 13; |
| 85 | 84 |
| 86 // This mechanically inverts the denorm half -> normal float conversion abov
e. | 85 // This mechanically inverts the denorm half -> normal float conversion abov
e. |
| 87 // Knowning that and reading its explanation will leave you feeling more con
fident | 86 // Knowning that and reading its explanation will leave you feeling more con
fident |
| 88 // than reading my best attempt at explaining this directly. | 87 // than reading my best attempt at explaining this directly. |
| 89 const Sk4i K = ((127-15) + (23-10) + 1) << 23; | 88 const Sk4i K = ((127-15) + (23-10) + 1) << 23; |
| 90 Sk4f plus_K = Sk4f::Load(&positive) + Sk4f::Load(&K); | 89 Sk4f plus_K = Sk4f::Load(&positive) + Sk4f::Load(&K); |
| 91 Sk4i denorm = Sk4i::Load(&plus_K) ^ K; | 90 Sk4i denorm = Sk4i::Load(&plus_K) ^ K; |
| 92 | 91 |
| 93 Sk4i merged = (sign >> 16) | will_be_denorm.thenElse(denorm, norm); | 92 Sk4i merged = (sign >> 16) | will_be_denorm.thenElse(denorm, norm); |
| 94 SkNx_cast<uint16_t>(merged).store(&r); | 93 return SkNx_cast<uint16_t>(merged); |
| 95 #endif | 94 #endif |
| 96 return r; | |
| 97 } | 95 } |
| 98 | 96 |
| 99 #endif | 97 #endif |
| OLD | NEW |