5#include "gtest/gtest.h"
12TEST(SaturatedMath, ExplicitAddOfSignedInts) {
14 EXPECT_EQ(saturated::Add<int8_t>(0x79, 5), int8_t(0x7E));
15 EXPECT_EQ(saturated::Add<int8_t>(0x7A, 5), int8_t(0x7F));
16 EXPECT_EQ(saturated::Add<int8_t>(0x7B, 5), int8_t(0x7F));
19 EXPECT_EQ(saturated::Add<int8_t>(0x86, -5), int8_t(0x81));
20 EXPECT_EQ(saturated::Add<int8_t>(0x85, -5), int8_t(0x80));
21 EXPECT_EQ(saturated::Add<int8_t>(0x84, -5), int8_t(0x80));
24 EXPECT_EQ(saturated::Add<int16_t>(0x7FF9, 5), int16_t(0x7FFE));
25 EXPECT_EQ(saturated::Add<int16_t>(0x7FFA, 5), int16_t(0x7FFF));
26 EXPECT_EQ(saturated::Add<int16_t>(0x7FFB, 5), int16_t(0x7FFF));
29 EXPECT_EQ(saturated::Add<int16_t>(0x8006, -5), int16_t(0x8001));
30 EXPECT_EQ(saturated::Add<int16_t>(0x8005, -5), int16_t(0x8000));
31 EXPECT_EQ(saturated::Add<int16_t>(0x8004, -5), int16_t(0x8000));
34 EXPECT_EQ(saturated::Add<int32_t>(0x7FFFFFF9, 5), int32_t(0x7FFFFFFE));
35 EXPECT_EQ(saturated::Add<int32_t>(0x7FFFFFFA, 5), int32_t(0x7FFFFFFF));
36 EXPECT_EQ(saturated::Add<int32_t>(0x7FFFFFFB, 5), int32_t(0x7FFFFFFF));
39 EXPECT_EQ(saturated::Add<int32_t>(0x80000006, -5), int32_t(0x80000001));
40 EXPECT_EQ(saturated::Add<int32_t>(0x80000005, -5), int32_t(0x80000000));
41 EXPECT_EQ(saturated::Add<int32_t>(0x80000004, -5), int32_t(0x80000000));
44 EXPECT_EQ(saturated::Add<int64_t>(0x7FFFFFFFFFFFFFF9, 5),
45 int64_t(0x7FFFFFFFFFFFFFFE));
46 EXPECT_EQ(saturated::Add<int64_t>(0x7FFFFFFFFFFFFFFA, 5),
47 int64_t(0x7FFFFFFFFFFFFFFF));
48 EXPECT_EQ(saturated::Add<int64_t>(0x7FFFFFFFFFFFFFFB, 5),
49 int64_t(0x7FFFFFFFFFFFFFFF));
52 EXPECT_EQ(saturated::Add<int64_t>(0x8000000000000006, -5),
53 int64_t(0x8000000000000001));
54 EXPECT_EQ(saturated::Add<int64_t>(0x8000000000000005, -5),
55 int64_t(0x8000000000000000));
56 EXPECT_EQ(saturated::Add<int64_t>(0x8000000000000004, -5),
57 int64_t(0x8000000000000000));
61TEST(SaturatedMath, ImplicitAddOfSignedInts) {
65 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x7E));
67 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x7F));
69 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x7F));
74 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x81));
76 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x80));
78 EXPECT_EQ(saturated::Add(
a,
b), int8_t(0x80));
83 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x7FFE));
85 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x7FFF));
87 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x7FFF));
92 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x8001));
94 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x8000));
96 EXPECT_EQ(saturated::Add(
a,
b), int16_t(0x8000));
99 int32_t
a = 0x7FFFFFF9;
101 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x7FFFFFFE));
103 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x7FFFFFFF));
105 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x7FFFFFFF));
108 int32_t
a = 0x80000006;
110 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x80000001));
112 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x80000000));
114 EXPECT_EQ(saturated::Add(
a,
b), int32_t(0x80000000));
117 int64_t
a = 0x7FFFFFFFFFFFFFF9;
119 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x7FFFFFFFFFFFFFFE));
120 a = 0x7FFFFFFFFFFFFFFA;
121 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x7FFFFFFFFFFFFFFF));
122 a = 0x7FFFFFFFFFFFFFFB;
123 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x7FFFFFFFFFFFFFFF));
126 int64_t
a = 0x8000000000000006;
128 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x8000000000000001));
129 a = 0x8000000000000005;
130 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x8000000000000000));
131 a = 0x8000000000000004;
132 EXPECT_EQ(saturated::Add(
a,
b), int64_t(0x8000000000000000));
136TEST(SaturatedMath, ExplicitAddOfFloatingPoint) {
138 const float inf = std::numeric_limits<float>::infinity();
140 const float big =
max * 0.5f;
142 EXPECT_EQ(saturated::Add<float>(big, big),
max);
143 EXPECT_EQ(saturated::Add<float>(
max, big), inf);
144 EXPECT_EQ(saturated::Add<float>(big,
max), inf);
145 EXPECT_EQ(saturated::Add<float>(
max,
max), inf);
146 EXPECT_EQ(saturated::Add<float>(
max, inf), inf);
147 EXPECT_EQ(saturated::Add<float>(inf,
max), inf);
148 EXPECT_EQ(saturated::Add<float>(inf, inf), inf);
150 EXPECT_EQ(saturated::Add<float>(-big, -big), -
max);
151 EXPECT_EQ(saturated::Add<float>(-
max, -big), -inf);
152 EXPECT_EQ(saturated::Add<float>(-big, -
max), -inf);
153 EXPECT_EQ(saturated::Add<float>(-
max, -
max), -inf);
154 EXPECT_EQ(saturated::Add<float>(-
max, -inf), -inf);
155 EXPECT_EQ(saturated::Add<float>(-inf, -
max), -inf);
156 EXPECT_EQ(saturated::Add<float>(-inf, -inf), -inf);
158 EXPECT_EQ(saturated::Add<float>(big, -big), 0.0f);
159 EXPECT_EQ(saturated::Add<float>(
max, -big), big);
160 EXPECT_EQ(saturated::Add<float>(big, -
max), -big);
161 EXPECT_EQ(saturated::Add<float>(
max, -
max), 0.0f);
162 EXPECT_EQ(saturated::Add<float>(
max, -inf), -inf);
163 EXPECT_EQ(saturated::Add<float>(inf, -
max), inf);
164 EXPECT_TRUE(std::isnan(saturated::Add<float>(inf, -inf)));
166 EXPECT_EQ(saturated::Add<float>(-big, big), 0.0f);
167 EXPECT_EQ(saturated::Add<float>(-
max, big), -big);
168 EXPECT_EQ(saturated::Add<float>(-big,
max), big);
169 EXPECT_EQ(saturated::Add<float>(-
max,
max), 0.0f);
170 EXPECT_EQ(saturated::Add<float>(-
max, inf), inf);
171 EXPECT_EQ(saturated::Add<float>(-inf,
max), -inf);
172 EXPECT_TRUE(std::isnan(saturated::Add<float>(-inf, inf)));
175 const double inf = std::numeric_limits<double>::infinity();
177 const double big =
max * 0.5f;
179 EXPECT_EQ(saturated::Add<double>(big, big),
max);
180 EXPECT_EQ(saturated::Add<double>(
max, big), inf);
181 EXPECT_EQ(saturated::Add<double>(big,
max), inf);
182 EXPECT_EQ(saturated::Add<double>(
max,
max), inf);
183 EXPECT_EQ(saturated::Add<double>(
max, inf), inf);
184 EXPECT_EQ(saturated::Add<double>(inf,
max), inf);
185 EXPECT_EQ(saturated::Add<double>(inf, inf), inf);
187 EXPECT_EQ(saturated::Add<double>(-big, -big), -
max);
188 EXPECT_EQ(saturated::Add<double>(-
max, -big), -inf);
189 EXPECT_EQ(saturated::Add<double>(-big, -
max), -inf);
190 EXPECT_EQ(saturated::Add<double>(-
max, -
max), -inf);
191 EXPECT_EQ(saturated::Add<double>(-
max, -inf), -inf);
192 EXPECT_EQ(saturated::Add<double>(-inf, -
max), -inf);
193 EXPECT_EQ(saturated::Add<double>(-inf, -inf), -inf);
195 EXPECT_EQ(saturated::Add<double>(big, -big), 0.0f);
196 EXPECT_EQ(saturated::Add<double>(
max, -big), big);
197 EXPECT_EQ(saturated::Add<double>(big, -
max), -big);
198 EXPECT_EQ(saturated::Add<double>(
max, -
max), 0.0f);
199 EXPECT_EQ(saturated::Add<double>(
max, -inf), -inf);
200 EXPECT_EQ(saturated::Add<double>(inf, -
max), inf);
201 EXPECT_TRUE(std::isnan(saturated::Add<double>(inf, -inf)));
203 EXPECT_EQ(saturated::Add<double>(-big, big), 0.0f);
204 EXPECT_EQ(saturated::Add<double>(-
max, big), -big);
205 EXPECT_EQ(saturated::Add<double>(-big,
max), big);
206 EXPECT_EQ(saturated::Add<double>(-
max,
max), 0.0f);
207 EXPECT_EQ(saturated::Add<double>(-
max, inf), inf);
208 EXPECT_EQ(saturated::Add<double>(-inf,
max), -inf);
209 EXPECT_TRUE(std::isnan(saturated::Add<double>(-inf, inf)));
212 const Scalar inf = std::numeric_limits<Scalar>::infinity();
216 EXPECT_EQ(saturated::Add<Scalar>(big, big),
max);
217 EXPECT_EQ(saturated::Add<Scalar>(
max, big), inf);
218 EXPECT_EQ(saturated::Add<Scalar>(big,
max), inf);
219 EXPECT_EQ(saturated::Add<Scalar>(
max,
max), inf);
220 EXPECT_EQ(saturated::Add<Scalar>(
max, inf), inf);
221 EXPECT_EQ(saturated::Add<Scalar>(inf,
max), inf);
222 EXPECT_EQ(saturated::Add<Scalar>(inf, inf), inf);
224 EXPECT_EQ(saturated::Add<Scalar>(-big, -big), -
max);
225 EXPECT_EQ(saturated::Add<Scalar>(-
max, -big), -inf);
226 EXPECT_EQ(saturated::Add<Scalar>(-big, -
max), -inf);
227 EXPECT_EQ(saturated::Add<Scalar>(-
max, -
max), -inf);
228 EXPECT_EQ(saturated::Add<Scalar>(-
max, -inf), -inf);
229 EXPECT_EQ(saturated::Add<Scalar>(-inf, -
max), -inf);
230 EXPECT_EQ(saturated::Add<Scalar>(-inf, -inf), -inf);
232 EXPECT_EQ(saturated::Add<Scalar>(big, -big), 0.0f);
233 EXPECT_EQ(saturated::Add<Scalar>(
max, -big), big);
234 EXPECT_EQ(saturated::Add<Scalar>(big, -
max), -big);
235 EXPECT_EQ(saturated::Add<Scalar>(
max, -
max), 0.0f);
236 EXPECT_EQ(saturated::Add<Scalar>(
max, -inf), -inf);
237 EXPECT_EQ(saturated::Add<Scalar>(inf, -
max), inf);
238 EXPECT_TRUE(std::isnan(saturated::Add<Scalar>(inf, -inf)));
240 EXPECT_EQ(saturated::Add<Scalar>(-big, big), 0.0f);
241 EXPECT_EQ(saturated::Add<Scalar>(-
max, big), -big);
242 EXPECT_EQ(saturated::Add<Scalar>(-big,
max), big);
243 EXPECT_EQ(saturated::Add<Scalar>(-
max,
max), 0.0f);
244 EXPECT_EQ(saturated::Add<Scalar>(-
max, inf), inf);
245 EXPECT_EQ(saturated::Add<Scalar>(-inf,
max), -inf);
246 EXPECT_TRUE(std::isnan(saturated::Add<Scalar>(-inf, inf)));
250TEST(SaturatedMath, ImplicitAddOfFloatingPoint) {
252 const float inf = std::numeric_limits<float>::infinity();
254 const float big =
max * 0.5f;
256 EXPECT_EQ(saturated::Add(big, big),
max);
257 EXPECT_EQ(saturated::Add(
max, big), inf);
258 EXPECT_EQ(saturated::Add(big,
max), inf);
259 EXPECT_EQ(saturated::Add(
max,
max), inf);
260 EXPECT_EQ(saturated::Add(
max, inf), inf);
261 EXPECT_EQ(saturated::Add(inf,
max), inf);
262 EXPECT_EQ(saturated::Add(inf, inf), inf);
264 EXPECT_EQ(saturated::Add(-big, -big), -
max);
265 EXPECT_EQ(saturated::Add(-
max, -big), -inf);
266 EXPECT_EQ(saturated::Add(-big, -
max), -inf);
267 EXPECT_EQ(saturated::Add(-
max, -
max), -inf);
268 EXPECT_EQ(saturated::Add(-
max, -inf), -inf);
269 EXPECT_EQ(saturated::Add(-inf, -
max), -inf);
270 EXPECT_EQ(saturated::Add(-inf, -inf), -inf);
272 EXPECT_EQ(saturated::Add(big, -big), 0.0f);
273 EXPECT_EQ(saturated::Add(
max, -big), big);
274 EXPECT_EQ(saturated::Add(big, -
max), -big);
275 EXPECT_EQ(saturated::Add(
max, -
max), 0.0f);
276 EXPECT_EQ(saturated::Add(
max, -inf), -inf);
277 EXPECT_EQ(saturated::Add(inf, -
max), inf);
278 EXPECT_TRUE(std::isnan(saturated::Add(inf, -inf)));
280 EXPECT_EQ(saturated::Add(-big, big), 0.0f);
281 EXPECT_EQ(saturated::Add(-
max, big), -big);
282 EXPECT_EQ(saturated::Add(-big,
max), big);
283 EXPECT_EQ(saturated::Add(-
max,
max), 0.0f);
284 EXPECT_EQ(saturated::Add(-
max, inf), inf);
285 EXPECT_EQ(saturated::Add(-inf,
max), -inf);
286 EXPECT_TRUE(std::isnan(saturated::Add(-inf, inf)));
289 const double inf = std::numeric_limits<double>::infinity();
291 const double big =
max * 0.5f;
293 EXPECT_EQ(saturated::Add(big, big),
max);
294 EXPECT_EQ(saturated::Add(
max, big), inf);
295 EXPECT_EQ(saturated::Add(big,
max), inf);
296 EXPECT_EQ(saturated::Add(
max,
max), inf);
297 EXPECT_EQ(saturated::Add(
max, inf), inf);
298 EXPECT_EQ(saturated::Add(inf,
max), inf);
299 EXPECT_EQ(saturated::Add(inf, inf), inf);
301 EXPECT_EQ(saturated::Add(-big, -big), -
max);
302 EXPECT_EQ(saturated::Add(-
max, -big), -inf);
303 EXPECT_EQ(saturated::Add(-big, -
max), -inf);
304 EXPECT_EQ(saturated::Add(-
max, -
max), -inf);
305 EXPECT_EQ(saturated::Add(-
max, -inf), -inf);
306 EXPECT_EQ(saturated::Add(-inf, -
max), -inf);
307 EXPECT_EQ(saturated::Add(-inf, -inf), -inf);
309 EXPECT_EQ(saturated::Add(big, -big), 0.0f);
310 EXPECT_EQ(saturated::Add(
max, -big), big);
311 EXPECT_EQ(saturated::Add(big, -
max), -big);
312 EXPECT_EQ(saturated::Add(
max, -
max), 0.0f);
313 EXPECT_EQ(saturated::Add(
max, -inf), -inf);
314 EXPECT_EQ(saturated::Add(inf, -
max), inf);
315 EXPECT_TRUE(std::isnan(saturated::Add(inf, -inf)));
317 EXPECT_EQ(saturated::Add(-big, big), 0.0f);
318 EXPECT_EQ(saturated::Add(-
max, big), -big);
319 EXPECT_EQ(saturated::Add(-big,
max), big);
320 EXPECT_EQ(saturated::Add(-
max,
max), 0.0f);
321 EXPECT_EQ(saturated::Add(-
max, inf), inf);
322 EXPECT_EQ(saturated::Add(-inf,
max), -inf);
323 EXPECT_TRUE(std::isnan(saturated::Add(-inf, inf)));
326 const Scalar inf = std::numeric_limits<Scalar>::infinity();
330 EXPECT_EQ(saturated::Add(big, big),
max);
331 EXPECT_EQ(saturated::Add(
max, big), inf);
332 EXPECT_EQ(saturated::Add(big,
max), inf);
333 EXPECT_EQ(saturated::Add(
max,
max), inf);
334 EXPECT_EQ(saturated::Add(
max, inf), inf);
335 EXPECT_EQ(saturated::Add(inf,
max), inf);
336 EXPECT_EQ(saturated::Add(inf, inf), inf);
338 EXPECT_EQ(saturated::Add(-big, -big), -
max);
339 EXPECT_EQ(saturated::Add(-
max, -big), -inf);
340 EXPECT_EQ(saturated::Add(-big, -
max), -inf);
341 EXPECT_EQ(saturated::Add(-
max, -
max), -inf);
342 EXPECT_EQ(saturated::Add(-
max, -inf), -inf);
343 EXPECT_EQ(saturated::Add(-inf, -
max), -inf);
344 EXPECT_EQ(saturated::Add(-inf, -inf), -inf);
346 EXPECT_EQ(saturated::Add(big, -big), 0.0f);
347 EXPECT_EQ(saturated::Add(
max, -big), big);
348 EXPECT_EQ(saturated::Add(big, -
max), -big);
349 EXPECT_EQ(saturated::Add(
max, -
max), 0.0f);
350 EXPECT_EQ(saturated::Add(
max, -inf), -inf);
351 EXPECT_EQ(saturated::Add(inf, -
max), inf);
352 EXPECT_TRUE(std::isnan(saturated::Add(inf, -inf)));
354 EXPECT_EQ(saturated::Add(-big, big), 0.0f);
355 EXPECT_EQ(saturated::Add(-
max, big), -big);
356 EXPECT_EQ(saturated::Add(-big,
max), big);
357 EXPECT_EQ(saturated::Add(-
max,
max), 0.0f);
358 EXPECT_EQ(saturated::Add(-
max, inf), inf);
359 EXPECT_EQ(saturated::Add(-inf,
max), -inf);
360 EXPECT_TRUE(std::isnan(saturated::Add(-inf, inf)));
364TEST(SaturatedMath, ExplicitSubOfSignedInts) {
366 EXPECT_EQ(saturated::Sub<int8_t>(0x79, -5), int8_t(0x7E));
367 EXPECT_EQ(saturated::Sub<int8_t>(0x7A, -5), int8_t(0x7F));
368 EXPECT_EQ(saturated::Sub<int8_t>(0x7B, -5), int8_t(0x7F));
371 EXPECT_EQ(saturated::Sub<int8_t>(0x86, 5), int8_t(0x81));
372 EXPECT_EQ(saturated::Sub<int8_t>(0x85, 5), int8_t(0x80));
373 EXPECT_EQ(saturated::Sub<int8_t>(0x84, 5), int8_t(0x80));
376 EXPECT_EQ(saturated::Sub<int16_t>(0x7FF9, -5), int16_t(0x7FFE));
377 EXPECT_EQ(saturated::Sub<int16_t>(0x7FFA, -5), int16_t(0x7FFF));
378 EXPECT_EQ(saturated::Sub<int16_t>(0x7FFB, -5), int16_t(0x7FFF));
381 EXPECT_EQ(saturated::Sub<int16_t>(0x8006, 5), int16_t(0x8001));
382 EXPECT_EQ(saturated::Sub<int16_t>(0x8005, 5), int16_t(0x8000));
383 EXPECT_EQ(saturated::Sub<int16_t>(0x8004, 5), int16_t(0x8000));
386 EXPECT_EQ(saturated::Sub<int32_t>(0x7FFFFFF9, -5), int32_t(0x7FFFFFFE));
387 EXPECT_EQ(saturated::Sub<int32_t>(0x7FFFFFFA, -5), int32_t(0x7FFFFFFF));
388 EXPECT_EQ(saturated::Sub<int32_t>(0x7FFFFFFB, -5), int32_t(0x7FFFFFFF));
391 EXPECT_EQ(saturated::Sub<int32_t>(0x80000006, 5), int32_t(0x80000001));
392 EXPECT_EQ(saturated::Sub<int32_t>(0x80000005, 5), int32_t(0x80000000));
393 EXPECT_EQ(saturated::Sub<int32_t>(0x80000004, 5), int32_t(0x80000000));
396 EXPECT_EQ(saturated::Sub<int64_t>(0x7FFFFFFFFFFFFFF9, -5),
397 int64_t(0x7FFFFFFFFFFFFFFE));
398 EXPECT_EQ(saturated::Sub<int64_t>(0x7FFFFFFFFFFFFFFA, -5),
399 int64_t(0x7FFFFFFFFFFFFFFF));
400 EXPECT_EQ(saturated::Sub<int64_t>(0x7FFFFFFFFFFFFFFB, -5),
401 int64_t(0x7FFFFFFFFFFFFFFF));
404 EXPECT_EQ(saturated::Sub<int64_t>(0x8000000000000006, 5),
405 int64_t(0x8000000000000001));
406 EXPECT_EQ(saturated::Sub<int64_t>(0x8000000000000005, 5),
407 int64_t(0x8000000000000000));
408 EXPECT_EQ(saturated::Sub<int64_t>(0x8000000000000004, 5),
409 int64_t(0x8000000000000000));
413TEST(SaturatedMath, ImplicitSubOfSignedInts) {
417 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x7E));
419 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x7F));
421 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x7F));
426 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x81));
428 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x80));
430 EXPECT_EQ(saturated::Sub(
a,
b), int8_t(0x80));
435 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x7FFE));
437 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x7FFF));
439 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x7FFF));
444 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x8001));
446 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x8000));
448 EXPECT_EQ(saturated::Sub(
a,
b), int16_t(0x8000));
451 int32_t
a = 0x7FFFFFF9;
453 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x7FFFFFFE));
455 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x7FFFFFFF));
457 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x7FFFFFFF));
460 int32_t
a = 0x80000006;
462 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x80000001));
464 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x80000000));
466 EXPECT_EQ(saturated::Sub(
a,
b), int32_t(0x80000000));
469 int64_t
a = 0x7FFFFFFFFFFFFFF9;
471 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x7FFFFFFFFFFFFFFE));
472 a = 0x7FFFFFFFFFFFFFFA;
473 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x7FFFFFFFFFFFFFFF));
474 a = 0x7FFFFFFFFFFFFFFB;
475 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x7FFFFFFFFFFFFFFF));
478 int64_t
a = 0x8000000000000006;
480 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x8000000000000001));
481 a = 0x8000000000000005;
482 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x8000000000000000));
483 a = 0x8000000000000004;
484 EXPECT_EQ(saturated::Sub(
a,
b), int64_t(0x8000000000000000));
488TEST(SaturatedMath, ExplicitSubOfFloatingPoint) {
490 const float inf = std::numeric_limits<float>::infinity();
492 const float big =
max * 0.5f;
494 EXPECT_EQ(saturated::Sub<float>(big, big), 0.0f);
495 EXPECT_EQ(saturated::Sub<float>(
max, big), big);
496 EXPECT_EQ(saturated::Sub<float>(big,
max), -big);
497 EXPECT_EQ(saturated::Sub<float>(
max,
max), 0.0f);
498 EXPECT_EQ(saturated::Sub<float>(
max, inf), -inf);
499 EXPECT_EQ(saturated::Sub<float>(inf,
max), inf);
500 EXPECT_TRUE(std::isnan(saturated::Sub<float>(inf, inf)));
502 EXPECT_EQ(saturated::Sub<float>(-big, -big), 0.0f);
503 EXPECT_EQ(saturated::Sub<float>(-
max, -big), -big);
504 EXPECT_EQ(saturated::Sub<float>(-big, -
max), big);
505 EXPECT_EQ(saturated::Sub<float>(-
max, -
max), 0.0f);
506 EXPECT_EQ(saturated::Sub<float>(-
max, -inf), inf);
507 EXPECT_EQ(saturated::Sub<float>(-inf, -
max), -inf);
508 EXPECT_TRUE(std::isnan(saturated::Sub<float>(-inf, -inf)));
510 EXPECT_EQ(saturated::Sub<float>(big, -big),
max);
511 EXPECT_EQ(saturated::Sub<float>(
max, -big), inf);
512 EXPECT_EQ(saturated::Sub<float>(big, -
max), inf);
513 EXPECT_EQ(saturated::Sub<float>(
max, -
max), inf);
514 EXPECT_EQ(saturated::Sub<float>(
max, -inf), inf);
515 EXPECT_EQ(saturated::Sub<float>(inf, -
max), inf);
516 EXPECT_EQ(saturated::Sub<float>(inf, -inf), inf);
518 EXPECT_EQ(saturated::Sub<float>(-big, big), -
max);
519 EXPECT_EQ(saturated::Sub<float>(-
max, big), -inf);
520 EXPECT_EQ(saturated::Sub<float>(-big,
max), -inf);
521 EXPECT_EQ(saturated::Sub<float>(-
max,
max), -inf);
522 EXPECT_EQ(saturated::Sub<float>(-
max, inf), -inf);
523 EXPECT_EQ(saturated::Sub<float>(-inf,
max), -inf);
524 EXPECT_EQ(saturated::Sub<float>(-inf, inf), -inf);
527 const double inf = std::numeric_limits<double>::infinity();
529 const double big =
max * 0.5f;
531 EXPECT_EQ(saturated::Sub<double>(big, big), 0.0f);
532 EXPECT_EQ(saturated::Sub<double>(
max, big), big);
533 EXPECT_EQ(saturated::Sub<double>(big,
max), -big);
534 EXPECT_EQ(saturated::Sub<double>(
max,
max), 0.0f);
535 EXPECT_EQ(saturated::Sub<double>(
max, inf), -inf);
536 EXPECT_EQ(saturated::Sub<double>(inf,
max), inf);
537 EXPECT_TRUE(std::isnan(saturated::Sub<double>(inf, inf)));
539 EXPECT_EQ(saturated::Sub<double>(-big, -big), 0.0f);
540 EXPECT_EQ(saturated::Sub<double>(-
max, -big), -big);
541 EXPECT_EQ(saturated::Sub<double>(-big, -
max), big);
542 EXPECT_EQ(saturated::Sub<double>(-
max, -
max), 0.0f);
543 EXPECT_EQ(saturated::Sub<double>(-
max, -inf), inf);
544 EXPECT_EQ(saturated::Sub<double>(-inf, -
max), -inf);
545 EXPECT_TRUE(std::isnan(saturated::Sub<double>(-inf, -inf)));
547 EXPECT_EQ(saturated::Sub<double>(big, -big),
max);
548 EXPECT_EQ(saturated::Sub<double>(
max, -big), inf);
549 EXPECT_EQ(saturated::Sub<double>(big, -
max), inf);
550 EXPECT_EQ(saturated::Sub<double>(
max, -
max), inf);
551 EXPECT_EQ(saturated::Sub<double>(
max, -inf), inf);
552 EXPECT_EQ(saturated::Sub<double>(inf, -
max), inf);
553 EXPECT_EQ(saturated::Sub<double>(inf, -inf), inf);
555 EXPECT_EQ(saturated::Sub<double>(-big, big), -
max);
556 EXPECT_EQ(saturated::Sub<double>(-
max, big), -inf);
557 EXPECT_EQ(saturated::Sub<double>(-big,
max), -inf);
558 EXPECT_EQ(saturated::Sub<double>(-
max,
max), -inf);
559 EXPECT_EQ(saturated::Sub<double>(-
max, inf), -inf);
560 EXPECT_EQ(saturated::Sub<double>(-inf,
max), -inf);
561 EXPECT_EQ(saturated::Sub<double>(-inf, inf), -inf);
564 const Scalar inf = std::numeric_limits<Scalar>::infinity();
568 EXPECT_EQ(saturated::Sub<Scalar>(big, big), 0.0f);
569 EXPECT_EQ(saturated::Sub<Scalar>(
max, big), big);
570 EXPECT_EQ(saturated::Sub<Scalar>(big,
max), -big);
571 EXPECT_EQ(saturated::Sub<Scalar>(
max,
max), 0.0f);
572 EXPECT_EQ(saturated::Sub<Scalar>(
max, inf), -inf);
573 EXPECT_EQ(saturated::Sub<Scalar>(inf,
max), inf);
574 EXPECT_TRUE(std::isnan(saturated::Sub<Scalar>(inf, inf)));
576 EXPECT_EQ(saturated::Sub<Scalar>(-big, -big), 0.0f);
577 EXPECT_EQ(saturated::Sub<Scalar>(-
max, -big), -big);
578 EXPECT_EQ(saturated::Sub<Scalar>(-big, -
max), big);
579 EXPECT_EQ(saturated::Sub<Scalar>(-
max, -
max), 0.0f);
580 EXPECT_EQ(saturated::Sub<Scalar>(-
max, -inf), inf);
581 EXPECT_EQ(saturated::Sub<Scalar>(-inf, -
max), -inf);
582 EXPECT_TRUE(std::isnan(saturated::Sub<Scalar>(-inf, -inf)));
584 EXPECT_EQ(saturated::Sub<Scalar>(big, -big),
max);
585 EXPECT_EQ(saturated::Sub<Scalar>(
max, -big), inf);
586 EXPECT_EQ(saturated::Sub<Scalar>(big, -
max), inf);
587 EXPECT_EQ(saturated::Sub<Scalar>(
max, -
max), inf);
588 EXPECT_EQ(saturated::Sub<Scalar>(
max, -inf), inf);
589 EXPECT_EQ(saturated::Sub<Scalar>(inf, -
max), inf);
590 EXPECT_EQ(saturated::Sub<Scalar>(inf, -inf), inf);
592 EXPECT_EQ(saturated::Sub<Scalar>(-big, big), -
max);
593 EXPECT_EQ(saturated::Sub<Scalar>(-
max, big), -inf);
594 EXPECT_EQ(saturated::Sub<Scalar>(-big,
max), -inf);
595 EXPECT_EQ(saturated::Sub<Scalar>(-
max,
max), -inf);
596 EXPECT_EQ(saturated::Sub<Scalar>(-
max, inf), -inf);
597 EXPECT_EQ(saturated::Sub<Scalar>(-inf,
max), -inf);
598 EXPECT_EQ(saturated::Sub<Scalar>(-inf, inf), -inf);
602TEST(SaturatedMath, ImplicitSubOfFloatingPoint) {
604 const float inf = std::numeric_limits<float>::infinity();
606 const float big =
max * 0.5f;
608 EXPECT_EQ(saturated::Sub(big, big), 0.0f);
609 EXPECT_EQ(saturated::Sub(
max, big), big);
610 EXPECT_EQ(saturated::Sub(big,
max), -big);
611 EXPECT_EQ(saturated::Sub(
max,
max), 0.0f);
612 EXPECT_EQ(saturated::Sub(
max, inf), -inf);
613 EXPECT_EQ(saturated::Sub(inf,
max), inf);
616 EXPECT_EQ(saturated::Sub(-big, -big), 0.0f);
617 EXPECT_EQ(saturated::Sub(-
max, -big), -big);
618 EXPECT_EQ(saturated::Sub(-big, -
max), big);
619 EXPECT_EQ(saturated::Sub(-
max, -
max), 0.0f);
620 EXPECT_EQ(saturated::Sub(-
max, -inf), inf);
621 EXPECT_EQ(saturated::Sub(-inf, -
max), -inf);
622 EXPECT_TRUE(std::isnan(saturated::Sub(-inf, -inf)));
624 EXPECT_EQ(saturated::Sub(big, -big),
max);
625 EXPECT_EQ(saturated::Sub(
max, -big), inf);
626 EXPECT_EQ(saturated::Sub(big, -
max), inf);
627 EXPECT_EQ(saturated::Sub(
max, -
max), inf);
628 EXPECT_EQ(saturated::Sub(
max, -inf), inf);
629 EXPECT_EQ(saturated::Sub(inf, -
max), inf);
630 EXPECT_EQ(saturated::Sub(inf, -inf), inf);
632 EXPECT_EQ(saturated::Sub(-big, big), -
max);
633 EXPECT_EQ(saturated::Sub(-
max, big), -inf);
634 EXPECT_EQ(saturated::Sub(-big,
max), -inf);
635 EXPECT_EQ(saturated::Sub(-
max,
max), -inf);
636 EXPECT_EQ(saturated::Sub(-
max, inf), -inf);
637 EXPECT_EQ(saturated::Sub(-inf,
max), -inf);
638 EXPECT_EQ(saturated::Sub(-inf, inf), -inf);
641 const double inf = std::numeric_limits<double>::infinity();
643 const double big =
max * 0.5f;
645 EXPECT_EQ(saturated::Sub(big, big), 0.0f);
646 EXPECT_EQ(saturated::Sub(
max, big), big);
647 EXPECT_EQ(saturated::Sub(big,
max), -big);
648 EXPECT_EQ(saturated::Sub(
max,
max), 0.0f);
649 EXPECT_EQ(saturated::Sub(
max, inf), -inf);
650 EXPECT_EQ(saturated::Sub(inf,
max), inf);
653 EXPECT_EQ(saturated::Sub(-big, -big), 0.0f);
654 EXPECT_EQ(saturated::Sub(-
max, -big), -big);
655 EXPECT_EQ(saturated::Sub(-big, -
max), big);
656 EXPECT_EQ(saturated::Sub(-
max, -
max), 0.0f);
657 EXPECT_EQ(saturated::Sub(-
max, -inf), inf);
658 EXPECT_EQ(saturated::Sub(-inf, -
max), -inf);
659 EXPECT_TRUE(std::isnan(saturated::Sub(-inf, -inf)));
661 EXPECT_EQ(saturated::Sub(big, -big),
max);
662 EXPECT_EQ(saturated::Sub(
max, -big), inf);
663 EXPECT_EQ(saturated::Sub(big, -
max), inf);
664 EXPECT_EQ(saturated::Sub(
max, -
max), inf);
665 EXPECT_EQ(saturated::Sub(
max, -inf), inf);
666 EXPECT_EQ(saturated::Sub(inf, -
max), inf);
667 EXPECT_EQ(saturated::Sub(inf, -inf), inf);
669 EXPECT_EQ(saturated::Sub(-big, big), -
max);
670 EXPECT_EQ(saturated::Sub(-
max, big), -inf);
671 EXPECT_EQ(saturated::Sub(-big,
max), -inf);
672 EXPECT_EQ(saturated::Sub(-
max,
max), -inf);
673 EXPECT_EQ(saturated::Sub(-
max, inf), -inf);
674 EXPECT_EQ(saturated::Sub(-inf,
max), -inf);
675 EXPECT_EQ(saturated::Sub(-inf, inf), -inf);
678 const Scalar inf = std::numeric_limits<Scalar>::infinity();
682 EXPECT_EQ(saturated::Sub(big, big), 0.0f);
683 EXPECT_EQ(saturated::Sub(
max, big), big);
684 EXPECT_EQ(saturated::Sub(big,
max), -big);
685 EXPECT_EQ(saturated::Sub(
max,
max), 0.0f);
686 EXPECT_EQ(saturated::Sub(
max, inf), -inf);
687 EXPECT_EQ(saturated::Sub(inf,
max), inf);
690 EXPECT_EQ(saturated::Sub(-big, -big), 0.0f);
691 EXPECT_EQ(saturated::Sub(-
max, -big), -big);
692 EXPECT_EQ(saturated::Sub(-big, -
max), big);
693 EXPECT_EQ(saturated::Sub(-
max, -
max), 0.0f);
694 EXPECT_EQ(saturated::Sub(-
max, -inf), inf);
695 EXPECT_EQ(saturated::Sub(-inf, -
max), -inf);
696 EXPECT_TRUE(std::isnan(saturated::Sub(-inf, -inf)));
698 EXPECT_EQ(saturated::Sub(big, -big),
max);
699 EXPECT_EQ(saturated::Sub(
max, -big), inf);
700 EXPECT_EQ(saturated::Sub(big, -
max), inf);
701 EXPECT_EQ(saturated::Sub(
max, -
max), inf);
702 EXPECT_EQ(saturated::Sub(
max, -inf), inf);
703 EXPECT_EQ(saturated::Sub(inf, -
max), inf);
704 EXPECT_EQ(saturated::Sub(inf, -inf), inf);
706 EXPECT_EQ(saturated::Sub(-big, big), -
max);
707 EXPECT_EQ(saturated::Sub(-
max, big), -inf);
708 EXPECT_EQ(saturated::Sub(-big,
max), -inf);
709 EXPECT_EQ(saturated::Sub(-
max,
max), -inf);
710 EXPECT_EQ(saturated::Sub(-
max, inf), -inf);
711 EXPECT_EQ(saturated::Sub(-inf,
max), -inf);
712 EXPECT_EQ(saturated::Sub(-inf, inf), -inf);
716TEST(SaturatedMath, ExplicitAverageScalarOfSignedInts) {
724 EXPECT_EQ(saturated::AverageScalar<int8_t>(0x81, 0x7F), -0.0f);
725 EXPECT_EQ(saturated::AverageScalar<int8_t>(0x80, 0x7F), -0.5f);
726 EXPECT_EQ(saturated::AverageScalar<int8_t>(0x70, 0x75), 114.5f);
727 EXPECT_EQ(saturated::AverageScalar<int8_t>(0x85, 0x8A), -120.5f);
730 EXPECT_EQ(saturated::AverageScalar<int16_t>(0x8001, 0x7FFF), -0.0f);
731 EXPECT_EQ(saturated::AverageScalar<int16_t>(0x8000, 0x7FFF), -0.5f);
732 EXPECT_EQ(saturated::AverageScalar<int16_t>(0x7000, 0x7005), 28674.5f);
733 EXPECT_EQ(saturated::AverageScalar<int16_t>(0x8005, 0x800A), -32760.5f);
736 EXPECT_EQ(saturated::AverageScalar<int32_t>(0x80000001, 0x7FFFFFFF), -0.0f);
737 EXPECT_EQ(saturated::AverageScalar<int32_t>(0x80000000, 0x7FFFFFFF), -0.5f);
738 EXPECT_EQ(saturated::AverageScalar<int32_t>(0x70000000, 0x70000005),
740 EXPECT_EQ(saturated::AverageScalar<int32_t>(0x80000005, 0x8000000A),
744 EXPECT_EQ(saturated::AverageScalar<int64_t>(0x8000000000000001,
751 EXPECT_EQ(saturated::AverageScalar<int64_t>(0x8000000000000000,
754 EXPECT_NEAR(saturated::AverageScalar<int64_t>(0x7000000000000000,
756 8.07045053e+18, 1e18);
757 EXPECT_NEAR(saturated::AverageScalar<int64_t>(0x8000000000000005,
759 -9.223372e+18, 1e18);
763TEST(SaturatedMath, ImplicitAverageScalarOfSignedInts) {
773 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.0f);
775 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.5f);
778 EXPECT_EQ(saturated::AverageScalar(
a,
b), 114.5f);
781 EXPECT_EQ(saturated::AverageScalar(
a,
b), -120.5f);
786 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.0f);
788 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.5f);
791 EXPECT_EQ(saturated::AverageScalar(
a,
b), 28674.5f);
794 EXPECT_EQ(saturated::AverageScalar(
a,
b), -32760.5f);
797 int32_t
a = 0x80000001;
798 int32_t
b = 0x7FFFFFFF;
799 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.0f);
801 EXPECT_EQ(saturated::AverageScalar(
a,
b), -0.5f);
804 EXPECT_EQ(saturated::AverageScalar(
a,
b), 1879048195.5f);
807 EXPECT_EQ(saturated::AverageScalar(
a,
b), -2147483655.5f);
810 int64_t
a = 0x8000000000000001;
811 int64_t
b = 0x7FFFFFFFFFFFFFFF;
812 EXPECT_EQ(saturated::AverageScalar(
a,
b), 0.0f);
817 a = 0x8000000000000000;
818 EXPECT_EQ(saturated::AverageScalar<int64_t>(
a,
b), 0.0f);
819 a = 0x7000000000000000;
820 b = 0x7000000000000005;
821 EXPECT_NEAR(saturated::AverageScalar<int64_t>(
a,
b), 8.0704505e+18, 1e18);
822 a = 0x8000000000000005;
823 b = 0x800000000000000A;
824 EXPECT_NEAR(saturated::AverageScalar<int64_t>(
a,
b), -9.223372e+18, 1e18);
828TEST(SaturatedMath, ExplicitAverageScalarOfFloatingPoint) {
829 const Scalar s_inf = std::numeric_limits<Scalar>::infinity();
831 const Scalar s_big = s_max * 0.5f;
834 const float inf = std::numeric_limits<Scalar>::infinity();
836 const float big =
max * 0.5f;
838 EXPECT_EQ(saturated::AverageScalar<float>(big, big), s_big);
839 EXPECT_EQ(saturated::AverageScalar<float>(
max,
max), s_max);
840 EXPECT_EQ(saturated::AverageScalar<float>(big, -big), 0.0f);
841 EXPECT_EQ(saturated::AverageScalar<float>(
max, -
max), 0.0f);
842 EXPECT_EQ(saturated::AverageScalar<float>(-big, big), 0.0f);
843 EXPECT_EQ(saturated::AverageScalar<float>(-
max,
max), 0.0f);
844 EXPECT_EQ(saturated::AverageScalar<float>(-big, -big), -s_big);
845 EXPECT_EQ(saturated::AverageScalar<float>(-
max, -
max), -s_max);
847 EXPECT_EQ(saturated::AverageScalar<float>(inf, inf), s_inf);
848 EXPECT_EQ(saturated::AverageScalar<float>(-inf, -inf), -s_inf);
849 EXPECT_TRUE(std::isnan(saturated::AverageScalar<float>(-inf, inf)));
850 EXPECT_TRUE(std::isnan(saturated::AverageScalar<float>(inf, -inf)));
853 const double inf = std::numeric_limits<Scalar>::infinity();
855 const double big =
max * 0.5;
861 EXPECT_EQ(saturated::AverageScalar<double>(s_big, s_big), s_big);
862 EXPECT_EQ(saturated::AverageScalar<double>(s_max, s_max), s_max);
863 EXPECT_EQ(saturated::AverageScalar<double>(-s_big, -s_big), -s_big);
864 EXPECT_EQ(saturated::AverageScalar<double>(-s_max, -s_max), -s_max);
868 EXPECT_EQ(saturated::AverageScalar<double>(big, big), s_inf);
869 EXPECT_EQ(saturated::AverageScalar<double>(
max,
max), s_inf);
870 EXPECT_EQ(saturated::AverageScalar<double>(big, -big), 0.0f);
871 EXPECT_EQ(saturated::AverageScalar<double>(
max, -
max), 0.0f);
872 EXPECT_EQ(saturated::AverageScalar<double>(-big, big), 0.0f);
873 EXPECT_EQ(saturated::AverageScalar<double>(-
max,
max), 0.0f);
874 EXPECT_EQ(saturated::AverageScalar<double>(-big, -big), -s_inf);
875 EXPECT_EQ(saturated::AverageScalar<double>(-
max, -
max), -s_inf);
877 EXPECT_EQ(saturated::AverageScalar<double>(inf, inf), s_inf);
878 EXPECT_EQ(saturated::AverageScalar<double>(-inf, -inf), -s_inf);
879 EXPECT_TRUE(std::isnan(saturated::AverageScalar<double>(-inf, inf)));
880 EXPECT_TRUE(std::isnan(saturated::AverageScalar<double>(inf, -inf)));
883 const Scalar inf = std::numeric_limits<Scalar>::infinity();
887 EXPECT_EQ(saturated::AverageScalar<Scalar>(big, big), s_big);
888 EXPECT_EQ(saturated::AverageScalar<Scalar>(
max,
max), s_max);
889 EXPECT_EQ(saturated::AverageScalar<Scalar>(big, -big), 0.0f);
890 EXPECT_EQ(saturated::AverageScalar<Scalar>(
max, -
max), 0.0f);
891 EXPECT_EQ(saturated::AverageScalar<Scalar>(-big, big), 0.0f);
892 EXPECT_EQ(saturated::AverageScalar<Scalar>(-
max,
max), 0.0f);
893 EXPECT_EQ(saturated::AverageScalar<Scalar>(-big, -big), -s_big);
894 EXPECT_EQ(saturated::AverageScalar<Scalar>(-
max, -
max), -s_max);
896 EXPECT_EQ(saturated::AverageScalar<Scalar>(inf, inf), s_inf);
897 EXPECT_EQ(saturated::AverageScalar<Scalar>(-inf, -inf), -s_inf);
898 EXPECT_TRUE(std::isnan(saturated::AverageScalar<Scalar>(-inf, s_inf)));
899 EXPECT_TRUE(std::isnan(saturated::AverageScalar<Scalar>(inf, -s_inf)));
903TEST(SaturatedMath, ImplicitAverageScalarOfFloatingPoint) {
906 const Scalar s_inf = std::numeric_limits<Scalar>::infinity();
908 const Scalar s_big = s_max * 0.5f;
911 const float inf = std::numeric_limits<float>::infinity();
913 const float big =
max * 0.5f;
915 EXPECT_EQ(saturated::AverageScalar(big, big), s_big);
916 EXPECT_EQ(saturated::AverageScalar(
max,
max), s_max);
917 EXPECT_EQ(saturated::AverageScalar(big, -big), 0.0f);
918 EXPECT_EQ(saturated::AverageScalar(
max, -
max), 0.0f);
919 EXPECT_EQ(saturated::AverageScalar(-big, big), 0.0f);
920 EXPECT_EQ(saturated::AverageScalar(-
max,
max), 0.0f);
921 EXPECT_EQ(saturated::AverageScalar(-big, -big), -s_big);
922 EXPECT_EQ(saturated::AverageScalar(-
max, -
max), -s_max);
924 EXPECT_EQ(saturated::AverageScalar(inf, inf), s_inf);
925 EXPECT_EQ(saturated::AverageScalar(-inf, -inf), -s_inf);
926 EXPECT_TRUE(std::isnan(saturated::AverageScalar(-inf, inf)));
927 EXPECT_TRUE(std::isnan(saturated::AverageScalar(inf, -inf)));
930 const double inf = std::numeric_limits<double>::infinity();
932 const double big =
max * 0.5;
937 const double d_s_max = s_max;
938 const double d_s_big = s_big;
939 EXPECT_EQ(saturated::AverageScalar(d_s_big, d_s_big), s_big);
940 EXPECT_EQ(saturated::AverageScalar(d_s_max, d_s_max), s_max);
941 EXPECT_EQ(saturated::AverageScalar(-d_s_big, -d_s_big), -s_big);
942 EXPECT_EQ(saturated::AverageScalar(-d_s_max, -d_s_max), -s_max);
946 EXPECT_EQ(saturated::AverageScalar(big, big), s_inf);
947 EXPECT_EQ(saturated::AverageScalar(
max,
max), s_inf);
948 EXPECT_EQ(saturated::AverageScalar(big, -big), 0.0f);
949 EXPECT_EQ(saturated::AverageScalar(
max, -
max), 0.0f);
950 EXPECT_EQ(saturated::AverageScalar(-big, big), 0.0f);
951 EXPECT_EQ(saturated::AverageScalar(-
max,
max), 0.0f);
952 EXPECT_EQ(saturated::AverageScalar(-big, -big), -s_inf);
953 EXPECT_EQ(saturated::AverageScalar(-
max, -
max), -s_inf);
955 EXPECT_EQ(saturated::AverageScalar(inf, inf), s_inf);
956 EXPECT_EQ(saturated::AverageScalar(-inf, -inf), -s_inf);
957 EXPECT_TRUE(std::isnan(saturated::AverageScalar(-inf, inf)));
958 EXPECT_TRUE(std::isnan(saturated::AverageScalar(inf, -inf)));
961 const Scalar inf = std::numeric_limits<Scalar>::infinity();
965 EXPECT_EQ(saturated::AverageScalar(big, big), s_big);
966 EXPECT_EQ(saturated::AverageScalar(
max,
max), s_max);
967 EXPECT_EQ(saturated::AverageScalar(big, -big), 0.0f);
968 EXPECT_EQ(saturated::AverageScalar(
max, -
max), 0.0f);
969 EXPECT_EQ(saturated::AverageScalar(-big, big), 0.0f);
970 EXPECT_EQ(saturated::AverageScalar(-
max,
max), 0.0f);
971 EXPECT_EQ(saturated::AverageScalar(-big, -big), -s_big);
972 EXPECT_EQ(saturated::AverageScalar(-
max, -
max), -s_max);
974 EXPECT_EQ(saturated::AverageScalar(inf, inf), s_inf);
975 EXPECT_EQ(saturated::AverageScalar(-inf, -inf), -s_inf);
976 EXPECT_TRUE(std::isnan(saturated::AverageScalar(-inf, s_inf)));
977 EXPECT_TRUE(std::isnan(saturated::AverageScalar(inf, -s_inf)));
981TEST(SaturatedMath, CastingFiniteDoubleToFloatStaysFinite) {
986 const float result = saturated::Cast<double, float>(d_max);
991 const float result = saturated::Cast<double, float>(-d_max);
992 EXPECT_EQ(
result, -f_max);
996TEST(SaturatedMath, CastingInfiniteDoubleToFloatStaysInfinite) {
997 const double d_inf = std::numeric_limits<double>::infinity();
998 const float f_max = std::numeric_limits<float>::infinity();
1001 const float result = saturated::Cast<double, float>(d_inf);
1002 EXPECT_EQ(
result, f_max);
1006 const float result = saturated::Cast<double, float>(-d_inf);
1007 EXPECT_EQ(
result, -f_max);
1011TEST(SaturatedMath, CastingNaNDoubleToFloatStaysNaN) {
1012 const double d_nan = std::numeric_limits<double>::quiet_NaN();
1015 const float result = saturated::Cast<double, float>(d_nan);
1020 const float result = saturated::Cast<double, float>(-d_nan);
1025TEST(SaturatedMath, CastingLargeScalarToSignedIntProducesLimit) {
1027 const Scalar large = 1e20f;
1030 const auto result = saturated::Cast<Scalar, int8_t>(large);
1031 EXPECT_EQ(
result, int8_t(0x7F));
1034 const auto result = saturated::Cast<Scalar, int8_t>(-large);
1035 EXPECT_EQ(
result, int8_t(0x80));
1039 const auto result = saturated::Cast<Scalar, int16_t>(large);
1040 EXPECT_EQ(
result, int16_t(0x7FFF));
1043 const auto result = saturated::Cast<Scalar, int16_t>(-large);
1044 EXPECT_EQ(
result, int16_t(0x8000));
1048 const auto result = saturated::Cast<Scalar, int32_t>(large);
1049 EXPECT_EQ(
result, int32_t(0x7FFFFFFF));
1052 const auto result = saturated::Cast<Scalar, int32_t>(-large);
1053 EXPECT_EQ(
result, int32_t(0x80000000));
1057 const auto result = saturated::Cast<Scalar, int64_t>(large);
1058 EXPECT_EQ(
result, int64_t(0x7FFFFFFFFFFFFFFF));
1061 const auto result = saturated::Cast<Scalar, int64_t>(-large);
1062 EXPECT_EQ(
result, int64_t(0x8000000000000000));
1066TEST(SaturatedMath, CastingInfiniteScalarToSignedIntProducesLimit) {
1068 const Scalar inf = std::numeric_limits<Scalar>::infinity();
1071 const auto result = saturated::Cast<Scalar, int8_t>(inf);
1072 EXPECT_EQ(
result, int8_t(0x7F));
1075 const auto result = saturated::Cast<Scalar, int8_t>(-inf);
1076 EXPECT_EQ(
result, int8_t(0x80));
1080 const auto result = saturated::Cast<Scalar, int16_t>(inf);
1081 EXPECT_EQ(
result, int16_t(0x7FFF));
1084 const auto result = saturated::Cast<Scalar, int16_t>(-inf);
1085 EXPECT_EQ(
result, int16_t(0x8000));
1089 const auto result = saturated::Cast<Scalar, int32_t>(inf);
1090 EXPECT_EQ(
result, int32_t(0x7FFFFFFF));
1093 const auto result = saturated::Cast<Scalar, int32_t>(-inf);
1094 EXPECT_EQ(
result, int32_t(0x80000000));
1098 const auto result = saturated::Cast<Scalar, int64_t>(inf);
1099 EXPECT_EQ(
result, int64_t(0x7FFFFFFFFFFFFFFF));
1102 const auto result = saturated::Cast<Scalar, int64_t>(-inf);
1103 EXPECT_EQ(
result, int64_t(0x8000000000000000));
1107TEST(SaturatedMath, CastingNaNScalarToSignedIntProducesZero) {
1109 const Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
1112 const auto result = saturated::Cast<Scalar, int8_t>(nan);
1113 EXPECT_EQ(
result, int8_t(0));
1117 const auto result = saturated::Cast<Scalar, int16_t>(nan);
1118 EXPECT_EQ(
result, int16_t(0));
1122 const auto result = saturated::Cast<Scalar, int32_t>(nan);
1123 EXPECT_EQ(
result, int32_t(0));
1127 const auto result = saturated::Cast<Scalar, int64_t>(nan);
1128 EXPECT_EQ(
result, int64_t(0));
static float max(float r, float g, float b)
TEST(AiksCanvasTest, EmptyCullRect)
#define EXPECT_TRUE(handle)