1
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2017-2018 Esteban Tovagliari, The appleseedhq Organization
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28
29 // appleseed.foundation headers.
30 #include "foundation/math/fp.h"
31 #include "foundation/math/half.h"
32 #include "foundation/platform/types.h"
33 #include "foundation/utility/test.h"
34
35 // OpenEXR headers.
36 #include "foundation/platform/_beginexrheaders.h"
37 #include "Imath/half.h"
38 #include "foundation/platform/_endexrheaders.h"
39
40 // Standard headers.
41 #include <cstddef>
42
43 using namespace foundation;
44
TEST_SUITE(Foundation_Math_Half)45 TEST_SUITE(Foundation_Math_Half)
46 {
47 TEST_CASE(HalfToFloat_FloatToHalf_Roundtrip)
48 {
49 for (size_t i = 0x0000; i <= 0xFFFF; ++i)
50 {
51 const Half expected_half = Half::from_bits(static_cast<uint16>(i));
52 const float expected_float = half_to_float(expected_half);
53
54 const Half actual_half = float_to_half(expected_float);
55 const float actual_float = half_to_float(actual_half);
56
57 if (FP<float>::is_qnan(expected_float))
58 EXPECT_TRUE(FP<float>::is_qnan(actual_float));
59 else if (FP<float>::is_snan(expected_float))
60 EXPECT_TRUE(FP<float>::is_snan(actual_float));
61 else EXPECT_EQ(expected_float, actual_float);
62 }
63 }
64
65 TEST_CASE(HalfToFloat_FloatToHalfAlt_Roundtrip)
66 {
67 for (size_t i = 0x0000; i <= 0xFFFF; ++i)
68 {
69 const Half expected_half = Half::from_bits(static_cast<uint16>(i));
70 const float expected_float = half_to_float(expected_half);
71
72 const Half actual_half = float_to_half_alt(expected_float);
73 const float actual_float = half_to_float(actual_half);
74
75 if (FP<float>::is_qnan(expected_float))
76 EXPECT_TRUE(FP<float>::is_qnan(actual_float));
77 else if (FP<float>::is_snan(expected_float))
78 EXPECT_TRUE(FP<float>::is_snan(actual_float));
79 else EXPECT_EQ(expected_float, actual_float);
80 }
81 }
82
83 TEST_CASE(HalfToFloat_FastFloatToHalf_Roundtrip)
84 {
85 for (size_t i = 0x0000; i <= 0xFFFF; ++i)
86 {
87 const Half expected_half = Half::from_bits(static_cast<uint16>(i));
88 const float expected_float = half_to_float(expected_half);
89
90 const Half actual_half = fast_float_to_half(expected_float);
91 const float actual_float = half_to_float(actual_half);
92
93 if (!FP<float>::is_nan(expected_float) && !FP<float>::is_inf(expected_float))
94 EXPECT_EQ(expected_float, actual_float);
95 }
96 }
97
98 TEST_CASE(HalfToFloat_MatchesImath)
99 {
100 for (size_t i = 0x0000; i <= 0xFFFF; ++i)
101 {
102 // Construct an Imath's half then convert it to a float.
103 half imath_half;
104 imath_half.setBits(static_cast<uint16>(i));
105 const float imath_float = static_cast<float>(imath_half);
106
107 // Construct an appleseed's Half then convert it to a float using half_to_float().
108 const Half as_half = Half::from_bits(static_cast<uint16>(i));
109 const float as_float = half_to_float(as_half);
110
111 if (FP<float>::is_qnan(imath_float))
112 EXPECT_TRUE(FP<float>::is_qnan(as_float));
113 else if (FP<float>::is_snan(imath_float))
114 EXPECT_TRUE(FP<float>::is_snan(as_float));
115 else EXPECT_EQ(imath_float, as_float);
116 }
117 }
118
119 TEST_CASE(FloatToHalf_MatchesImath)
120 {
121 for (size_t i = 0x0000; i <= 0xFFFF; ++i)
122 {
123 // Generate a float via Imath's half.
124 half x;
125 x.setBits(static_cast<uint16>(i));
126 const float f = x;
127
128 const half imath_half(f);
129 const float imath_float = static_cast<float>(imath_half);
130
131 const Half as_half = float_to_half(f);
132 const float as_float = half_to_float(as_half);
133
134 if (FP<float>::is_qnan(imath_float))
135 EXPECT_TRUE(FP<float>::is_qnan(as_float));
136 else if (FP<float>::is_snan(imath_float))
137 EXPECT_TRUE(FP<float>::is_snan(as_float));
138 else EXPECT_EQ(imath_float, as_float);
139 }
140 }
141 }
142