]> git.sesse.net Git - movit/blob - fp16.h
Add support for 10- and 12-bit planar Y'CbCr inputs.
[movit] / fp16.h
1 #ifndef _MOVIT_FP16_H
2 #define _MOVIT_FP16_H 1
3
4 #ifdef __F16C__
5 #include <immintrin.h>
6 #endif
7
8 // Code for converting to and from fp16 (from fp64), without any particular
9 // machine support, with proper IEEE round-to-even behavior (and correct
10 // handling of NaNs and infinities). This is needed because some OpenGL
11 // drivers don't properly round off when asked to convert data themselves.
12 //
13 // These routines are originally written by Fabian Giesen, and released by
14 // him into the public domain;
15 // see https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/.
16 // They are quite fast, and can be vectorized if need be; of course, using
17 // the f16c instructions (see below) will be faster still.
18
19 namespace movit {
20
21 // structs instead of ints, so that they are not implicitly convertible.
22 struct fp32_int_t {
23         unsigned int val;
24 };
25 struct fp16_int_t {
26         unsigned short val;
27 };
28
29 #ifdef __F16C__
30
31 // Use the f16c instructions from Haswell if available (and we know that they
32 // are at compile time).
33 static inline float fp16_to_fp32(fp16_int_t x)
34 {
35         return _cvtsh_ss(x.val);
36 }
37
38 static inline fp16_int_t fp32_to_fp16(float x)
39 {
40         fp16_int_t ret;
41         ret.val = _cvtss_sh(x, 0);
42         return ret;
43 }
44
45 #else
46
47 float fp16_to_fp32(fp16_int_t x);
48 fp16_int_t fp32_to_fp16(float x);
49
50 #endif
51
52 // Overloads for use in templates.
53 static inline float to_fp32(double x) { return x; }
54 static inline float to_fp32(float x) { return x; }
55 static inline float to_fp32(fp16_int_t x) { return fp16_to_fp32(x); }
56
57 template<class T> inline T from_fp32(float x);
58 template<> inline double from_fp32<double>(float x) { return x; }
59 template<> inline float from_fp32<float>(float x) { return x; }
60 template<> inline fp16_int_t from_fp32<fp16_int_t>(float x) { return fp32_to_fp16(x); }
61
62 template<class From, class To>
63 inline To convert_float(From x) { return from_fp32<To>(to_fp32(x)); }
64
65 template<class Same>
66 inline Same convert_float(Same x) { return x; }
67
68 }  // namespace movit
69
70 #endif  // _MOVIT_FP16_H