blob: a862990d4aecff3adc52b014a2e65f6199d0cd6b [file] [log] [blame]
jvanverth93679922014-11-26 13:15:59 -08001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkHalf_DEFINED
9#define SkHalf_DEFINED
10
mtkleinfff055c2016-02-11 06:30:03 -080011#include "SkNx.h"
jvanverth93679922014-11-26 13:15:59 -080012#include "SkTypes.h"
13
14// 16-bit floating point value
15// format is 1 bit sign, 5 bits exponent, 10 bits mantissa
16// only used for storage
17typedef uint16_t SkHalf;
18
Mike Kleind0ce1482017-04-19 17:19:30 -040019static constexpr uint16_t SK_HalfMin = 0x0400; // 2^-14 (minimum positive normal value)
msarett6bdbf442016-07-19 09:07:55 -070020static constexpr uint16_t SK_HalfMax = 0x7bff; // 65504
21static constexpr uint16_t SK_HalfEpsilon = 0x1400; // 2^-10
22static constexpr uint16_t SK_Half1 = 0x3C00; // 1
jvanverth28f9c602014-12-05 13:06:35 -080023
jvanverth93679922014-11-26 13:15:59 -080024// convert between half and single precision floating point
25float SkHalfToFloat(SkHalf h);
Derek Sollenberger2fbf1bc2017-09-20 15:51:08 -040026SkHalf SK_API SkFloatToHalf(float f);
jvanverth93679922014-11-26 13:15:59 -080027
mtklein58e389b2016-07-15 07:00:11 -070028// Convert between half and single precision floating point,
mtkleina2d2f382016-08-23 08:58:12 -070029// assuming inputs and outputs are both finite, and may
30// flush values which would be denormal half floats to zero.
mtklein8ae991e2016-08-22 13:20:18 -070031static inline Sk4f SkHalfToFloat_finite_ftz(uint64_t);
32static inline Sk4h SkFloatToHalf_finite_ftz(const Sk4f&);
mtkleinfff055c2016-02-11 06:30:03 -080033
34// ~~~~~~~~~~~ impl ~~~~~~~~~~~~~~ //
35
36// Like the serial versions in SkHalf.cpp, these are based on
37// https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
38
mtkleinbe8c19e2016-02-19 09:40:24 -080039// GCC 4.9 lacks the intrinsics to use ARMv8 f16<->f32 instructions, so we use inline assembly.
40
Mike Klein2ee0f422017-06-12 09:28:09 -040041static inline Sk4f SkHalfToFloat_finite_ftz(uint64_t rgba) {
42 Sk4h hs = Sk4h::Load(&rgba);
mtkleinbe8c19e2016-02-19 09:40:24 -080043#if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64)
44 float32x4_t fs;
mtkleinf660b7c2016-07-26 08:01:19 -070045 asm ("fcvtl %[fs].4s, %[hs].4h \n" // vcvt_f32_f16(...)
mtkleinbe8c19e2016-02-19 09:40:24 -080046 : [fs] "=w" (fs) // =w: write-only NEON register
mtkleinf660b7c2016-07-26 08:01:19 -070047 : [hs] "w" (hs.fVec)); // w: read-only NEON register
mtkleinbe8c19e2016-02-19 09:40:24 -080048 return fs;
mtkleinfff055c2016-02-11 06:30:03 -080049#else
mtklein8ae991e2016-08-22 13:20:18 -070050 Sk4i bits = SkNx_cast<int>(hs), // Expand to 32 bit.
51 sign = bits & 0x00008000, // Save the sign bit for later...
52 positive = bits ^ sign, // ...but strip it off for now.
53 is_norm = 0x03ff < positive; // Exponent > 0?
mtklein58e389b2016-07-15 07:00:11 -070054
55 // For normal half floats, extend the mantissa by 13 zero bits,
56 // then adjust the exponent from 15 bias to 127 bias.
57 Sk4i norm = (positive << 13) + ((127 - 15) << 23);
58
mtklein8ae991e2016-08-22 13:20:18 -070059 Sk4i merged = (sign << 16) | (norm & is_norm);
mtklein58e389b2016-07-15 07:00:11 -070060 return Sk4f::Load(&merged);
mtkleinfff055c2016-02-11 06:30:03 -080061#endif
62}
63
mtklein8ae991e2016-08-22 13:20:18 -070064static inline Sk4h SkFloatToHalf_finite_ftz(const Sk4f& fs) {
mtkleinbe8c19e2016-02-19 09:40:24 -080065#if !defined(SKNX_NO_SIMD) && defined(SK_CPU_ARM64)
66 float32x4_t vec = fs.fVec;
67 asm ("fcvtn %[vec].4h, %[vec].4s \n" // vcvt_f16_f32(vec)
msarett6bdbf442016-07-19 09:07:55 -070068 : [vec] "+w" (vec)); // +w: read-write NEON register
69 return vreinterpret_u16_f32(vget_low_f32(vec));
mtkleinfff055c2016-02-11 06:30:03 -080070#else
mtklein8ae991e2016-08-22 13:20:18 -070071 Sk4i bits = Sk4i::Load(&fs),
72 sign = bits & 0x80000000, // Save the sign bit for later...
73 positive = bits ^ sign, // ...but strip it off for now.
74 will_be_norm = 0x387fdfff < positive; // greater than largest denorm half?
mtklein58e389b2016-07-15 07:00:11 -070075
76 // For normal half floats, adjust the exponent from 127 bias to 15 bias,
77 // then drop the bottom 13 mantissa bits.
78 Sk4i norm = (positive - ((127 - 15) << 23)) >> 13;
79
mtklein8ae991e2016-08-22 13:20:18 -070080 Sk4i merged = (sign >> 16) | (will_be_norm & norm);
msarett6bdbf442016-07-19 09:07:55 -070081 return SkNx_cast<uint16_t>(merged);
mtkleinfff055c2016-02-11 06:30:03 -080082#endif
83}
84
jvanverth93679922014-11-26 13:15:59 -080085#endif