File: Half.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (77 lines) | stat: -rw-r--r-- 2,179 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "Half.h"

namespace nCine
{
	inline namespace Primitives
	{
		union FloatBits {
			std::uint32_t u;
			float f;
		};

		// half_to_float_fast4() from https://gist.github.com/rygorous/2144712
		float Half::UnpackHalf(const std::uint16_t value)
		{
			constexpr const FloatBits Magic { 113 << 23 };
			// Exponent mask after shift
			constexpr const std::uint32_t ShiftedExp = 0x7c00 << 13;

			const std::uint16_t h { value };
			FloatBits o;

			o.u = (h & 0x7fff) << 13;						// Exponent/mantissa bits
			const std::uint32_t exp = ShiftedExp & o.u;		// Just the exponent
			o.u += (127 - 15) << 23;						// Exponent adjust

			// Handle exponent special cases
			if (exp == ShiftedExp) {						// Inf/NaN?
				o.u += (128 - 16) << 23;					// Extra exp adjust
			} else if (exp == 0) {							// Zero/Denormal
				o.u += 1 << 23;								// Extra exp adjust
				o.f -= Magic.f;								// Renormalize
			}

			o.u |= (h & 0x8000) << 16;						// Sign bit
			return o.f;
		}

		// float_to_half_fast3() from https://gist.github.com/rygorous/2156668
		std::uint16_t Half::PackHalf(const float value)
		{
			constexpr const FloatBits FloatInfinity { 255 << 23 };
			constexpr const FloatBits HalfInfinity { 31 << 23 };
			constexpr const FloatBits Magic { 15 << 23 };
			constexpr const std::uint32_t SignMask = 0x80000000u;
			constexpr const std::uint32_t RoundMask = ~0xfffu;

			FloatBits f;
			f.f = value;
			std::uint16_t h;

			const std::uint32_t sign = f.u & SignMask;
			f.u ^= sign;

			// Note: all the integer compares in this function can be safely compiled
			// into signed compares since all operands are below 0x80000000. Important
			// if you want fast straight SSE2 code (since there's no unsigned PCMPGTD).

			// Inf or NaN (all exponent bits set): NaN->qNaN and Inf->Inf
			if (f.u >= FloatInfinity.u) {
				h = (f.u > FloatInfinity.u) ? 0x7e00 : 0x7c00;
			} else { // (De)normalized number or zero
				f.u &= RoundMask;
				f.f *= Magic.f;
				f.u -= RoundMask;

				// Clamp to signed infinity if overflowed
				if (f.u > HalfInfinity.u) f.u = HalfInfinity.u;

				// Take the bits!
				h = f.u >> 13;
			}

			h |= sign >> 16;
			return h;
		}
	}
}