]> git.sesse.net Git - movit/blob - fp16.h
Update a comment that wasn't really wrong, but less relevant in this context.
[movit] / fp16.h
1 #ifndef _MOVIT_FP16_H
2 #define _MOVIT_FP16_H 1
3
4 // Code for converting to and from fp16 (from fp64), without any particular
5 // machine support, with proper IEEE round-to-even behavior (and correct
6 // handling of NaNs and infinities). This is needed because some OpenGL
7 // drivers don't properly round off when asked to convert data themselves.
8 //
9 // These routines are not particularly fast.
10
11 namespace movit {
12
13 typedef unsigned int fp32_int_t;
14 typedef unsigned short fp16_int_t;
15
16 double fp16_to_fp64(fp16_int_t x);
17 fp16_int_t fp64_to_fp16(double x);
18
19 // These are not very useful by themselves, but are implemented using the same
20 // code as the fp16 ones (just with different constants), so they are useful
21 // for verifying against the FPU in unit tests.
22 double fp32_to_fp64(fp32_int_t x);
23 fp32_int_t fp64_to_fp32(double x);
24
25 // Overloads for use in templates.
26 static inline double to_fp64(double x) { return x; }
27 static inline double to_fp64(float x) { return x; }
28 static inline double to_fp64(fp16_int_t x) { return fp16_to_fp64(x); }
29
30 template<class T> inline T from_fp64(double x);
31 template<> inline double from_fp64<double>(double x) { return x; }
32 template<> inline float from_fp64<float>(double x) { return x; }
33 template<> inline fp16_int_t from_fp64<fp16_int_t>(double x) { return fp64_to_fp16(x); }
34
35 template<class From, class To>
36 inline To convert_float(From x) { return from_fp64<To>(to_fp64(x)); }
37
38 template<class Same>
39 inline Same convert_float(Same x) { return x; }
40
41 }  // namespace movit
42
43 #endif  // _MOVIT_FP16_H