]> git.sesse.net Git - movit/blob - fp16.h
Handle sampler2D uniforms specially.
[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 not particularly fast.
14
15 namespace movit {
16
17 typedef unsigned int fp32_int_t;
18 typedef unsigned short fp16_int_t;
19
20 #ifdef __F16C__
21
22 // Use the f16c instructions from Haswell if available (and we know that they
23 // are at compile time).
24 static inline double fp16_to_fp64(fp16_int_t x)
25 {
26         return _cvtsh_ss(x);
27 }
28
29 static inline fp16_int_t fp64_to_fp16(double x)
30 {
31         // NOTE: Strictly speaking, there are some select values where this isn't correct,
32         // since we first round to fp32 and then to fp16.
33         return _cvtss_sh(x, 0);
34 }
35
36 #else
37
38 double fp16_to_fp64(fp16_int_t x);
39 fp16_int_t fp64_to_fp16(double x);
40
41 #endif
42
43 // These are not very useful by themselves, but are implemented using the same
44 // code as the fp16 ones (just with different constants), so they are useful
45 // for verifying against the FPU in unit tests.
46 double fp32_to_fp64(fp32_int_t x);
47 fp32_int_t fp64_to_fp32(double x);
48
49 // Overloads for use in templates.
50 static inline double to_fp64(double x) { return x; }
51 static inline double to_fp64(float x) { return x; }
52 static inline double to_fp64(fp16_int_t x) { return fp16_to_fp64(x); }
53
54 template<class T> inline T from_fp64(double x);
55 template<> inline double from_fp64<double>(double x) { return x; }
56 template<> inline float from_fp64<float>(double x) { return x; }
57 template<> inline fp16_int_t from_fp64<fp16_int_t>(double x) { return fp64_to_fp16(x); }
58
59 template<class From, class To>
60 inline To convert_float(From x) { return from_fp64<To>(to_fp64(x)); }
61
62 template<class Same>
63 inline Same convert_float(Same x) { return x; }
64
65 }  // namespace movit
66
67 #endif  // _MOVIT_FP16_H