1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2018-2019 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef _DOUBLE_H_
26 #define _DOUBLE_H_
27
28 #include "half_float.h"
29 #include "u_math.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*
36 * This API is no more than a wrapper to the counterpart softfloat.h
37 * calls. Still, softfloat.h conversion API is meant to be kept private. In
38 * other words, only use the API published here, instead of calling directly
39 * the softfloat.h one.
40 */
41
42 float _mesa_double_to_float(double val);
43 float _mesa_double_to_float_rtz(double val);
44
45 static inline float
_mesa_double_to_float_rtne(double val)46 _mesa_double_to_float_rtne(double val)
47 {
48 return _mesa_double_to_float(val);
49 }
50
51 /*
52 * We round down from double to half float by going through float in between,
53 * but this can give us inaccurate results in some cases.
54 * One such case is 0x40ee6a0000000001, which should round to 0x7b9b, but
55 * going through float first turns into 0x7b9a instead. This is because the
56 * first non-fitting bit is set, so we get a tie, but with the least
57 * significant bit of the original number set, the tie should break rounding
58 * up.
59 * The cast to float, however, turns into 0x47735000, which when going to half
60 * still ties, but now we lost the tie-up bit, and instead we round to the
61 * nearest even, which in this case is down.
62 *
63 * To fix this, we check if the original would have tied, and if the tie would
64 * have rounded up, and if both are true, set the least significant bit of the
65 * intermediate float to 1, so that a tie on the next cast rounds up as well.
66 * If the rounding already got rid of the tie, that set bit will just be
67 * truncated anyway and the end result doesn't change.
68 *
69 * Another failing case is 0x40effdffffffffff. This one doesn't have the tie
70 * from double to half, so it just rounds down to 0x7bff (65504.0), but going
71 * through float first, it turns into 0x477ff000, which does have the tie bit
72 * for half set, and when that one gets rounded it turns into 0x7c00
73 * (Infinity).
74 * The fix for that one is to make sure the intermediate float does not have
75 * the tie bit set if the original didn't have it.
76 */
77 static inline uint16_t
_mesa_double_to_float16_rtne(double val)78 _mesa_double_to_float16_rtne(double val)
79 {
80 int significand_bits16 = 10;
81 int significand_bits32 = 23;
82 int significand_bits64 = 52;
83 int f64_to_16_tie_bit = significand_bits64 - significand_bits16 - 1;
84 int f32_to_16_tie_bit = significand_bits32 - significand_bits16 - 1;
85 uint64_t f64_rounds_up_mask = ((1ULL << f64_to_16_tie_bit) - 1);
86
87 union di src;
88 union fi dst;
89
90 src.d = val;
91 dst.f = val;
92
93 bool f64_has_tie = (src.ui & (1ULL << f64_to_16_tie_bit)) != 0;
94 bool f64_rounds_up = (src.ui & f64_rounds_up_mask) != 0;
95
96 dst.ui |= (f64_has_tie && f64_rounds_up);
97 if (!f64_has_tie)
98 dst.ui &= ~(1U << f32_to_16_tie_bit);
99
100 return _mesa_float_to_float16_rtne(dst.f);
101 }
102
103 /*
104 * double -> float -> half with RTZ doesn't have as many complications as
105 * RTNE, but we do need to ensure that the double -> float cast also uses RTZ.
106 */
107 static inline uint16_t
_mesa_double_to_float16_rtz(double val)108 _mesa_double_to_float16_rtz(double val)
109 {
110 return _mesa_float_to_float16_rtz(_mesa_double_to_float_rtz(val));
111 }
112
113 #ifdef __cplusplus
114 } /* extern C */
115 #endif
116
117 #endif /* _DOUBLE_H_ */
118