| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/Casting.h" |
| 8 |
#include "mozilla/ThreadSafety.h" |
| 9 |
|
| 10 |
#include <stdint.h> |
| 11 |
#include <cstdint> |
| 12 |
#include <limits> |
| 13 |
#include <type_traits> |
| 14 |
#include <iostream> |
| 15 |
#include <tuple> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
using mozilla::AssertedCast; |
| 19 |
using mozilla::BitwiseCast; |
| 20 |
using mozilla::SaturatingCast; |
| 21 |
using mozilla::detail::IsInBounds; |
| 22 |
|
| 23 |
static const uint8_t floatMantissaBitsPlusOne = 24; |
| 24 |
static const uint8_t doubleMantissaBitsPlusOne = 53; |
| 25 |
|
| 26 |
template <typename Uint, typename Ulong, bool = (sizeof(Uint) == sizeof(Ulong))> |
| 27 |
struct UintUlongBitwiseCast; |
| 28 |
|
| 29 |
template <typename Uint, typename Ulong> |
| 30 |
struct UintUlongBitwiseCast<Uint, Ulong, true> { |
| 31 |
static void test() { |
| 32 |
MOZ_RELEASE_ASSERT(BitwiseCast<Ulong>(Uint(8675309)) == Ulong(8675309)); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
template <typename Uint, typename Ulong> |
| 37 |
struct UintUlongBitwiseCast<Uint, Ulong, false> { |
| 38 |
static void test() {} |
| 39 |
}; |
| 40 |
|
| 41 |
static void TestBitwiseCast() { |
| 42 |
MOZ_RELEASE_ASSERT(BitwiseCast<int>(int(8675309)) == int(8675309)); |
| 43 |
UintUlongBitwiseCast<unsigned int, unsigned long>::test(); |
| 44 |
} |
| 45 |
|
| 46 |
static void TestSameSize() { |
| 47 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(0)))); |
| 48 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MIN)))); |
| 49 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int16_t>(int16_t(INT16_MAX)))); |
| 50 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint16_t>(uint16_t(UINT16_MAX)))); |
| 51 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int16_t>(uint16_t(0)))); |
| 52 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int16_t>(uint16_t(-1)))); |
| 53 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(-1)))); |
| 54 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint16_t>(int16_t(INT16_MAX)))); |
| 55 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint16_t>(int16_t(INT16_MIN)))); |
| 56 |
MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint32_t>(int32_t(INT32_MAX)))); |
| 57 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint32_t>(int32_t(INT32_MIN)))); |
| 58 |
} |
| 59 |
|
| 60 |
static void TestToBiggerSize() { |
| 61 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(0)))); |
| 62 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MIN)))); |
| 63 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int32_t>(int16_t(INT16_MAX)))); |
| 64 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, uint32_t>(uint16_t(UINT16_MAX)))); |
| 65 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(0)))); |
| 66 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int32_t>(uint16_t(-1)))); |
| 67 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(-1)))); |
| 68 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, uint32_t>(int16_t(INT16_MAX)))); |
| 69 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint32_t>(int16_t(INT16_MIN)))); |
| 70 |
MOZ_RELEASE_ASSERT((IsInBounds<int32_t, uint64_t>(int32_t(INT32_MAX)))); |
| 71 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint64_t>(int32_t(INT32_MIN)))); |
| 72 |
} |
| 73 |
|
| 74 |
static void TestToSmallerSize() { |
| 75 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, int8_t>(int16_t(0)))); |
| 76 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MIN)))); |
| 77 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, int8_t>(int16_t(INT16_MAX)))); |
| 78 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, uint8_t>(uint16_t(UINT16_MAX)))); |
| 79 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, int8_t>(uint16_t(0)))); |
| 80 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint16_t, int8_t>(uint16_t(-1)))); |
| 81 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(-1)))); |
| 82 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MAX)))); |
| 83 |
MOZ_RELEASE_ASSERT((!IsInBounds<int16_t, uint8_t>(int16_t(INT16_MIN)))); |
| 84 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MAX)))); |
| 85 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, uint16_t>(int32_t(INT32_MIN)))); |
| 86 |
|
| 87 |
// Boundary cases |
| 88 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) - 1))); |
| 89 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN)))); |
| 90 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MIN) + 1))); |
| 91 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) - 1))); |
| 92 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX)))); |
| 93 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, int32_t>(int64_t(INT32_MAX) + 1))); |
| 94 |
|
| 95 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(-1)))); |
| 96 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(0)))); |
| 97 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(1)))); |
| 98 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) - 1))); |
| 99 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX)))); |
| 100 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, uint32_t>(int64_t(UINT32_MAX) + 1))); |
| 101 |
} |
| 102 |
|
| 103 |
template <typename In, typename Out> |
| 104 |
void checkBoundariesFloating(In aEpsilon = {}, Out aIntegerOffset = {}) { |
| 105 |
// Check the max value of the input float can't be represented as an integer. |
| 106 |
// This is true for all floating point and integer width. |
| 107 |
MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(std::numeric_limits<In>::max()))); |
| 108 |
// Check that the max value of the integer, as a float, minus an offset that |
| 109 |
// depends on the magnitude, can be represented as an integer. |
| 110 |
MOZ_RELEASE_ASSERT((IsInBounds<In, Out>( |
| 111 |
static_cast<In>(std::numeric_limits<Out>::max() - aIntegerOffset)))); |
| 112 |
// Check that the max value of the integer, plus a number that depends on the |
| 113 |
// magnitude of the number, can't be represented as this integer (because it |
| 114 |
// becomes too big). |
| 115 |
MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>( |
| 116 |
aEpsilon + static_cast<In>(std::numeric_limits<Out>::max())))); |
| 117 |
if constexpr (std::is_signed_v<In>) { |
| 118 |
// Same for negative numbers. |
| 119 |
MOZ_RELEASE_ASSERT( |
| 120 |
(!IsInBounds<In, Out>(std::numeric_limits<In>::lowest()))); |
| 121 |
MOZ_RELEASE_ASSERT((IsInBounds<In, Out>( |
| 122 |
static_cast<In>(std::numeric_limits<Out>::lowest())))); |
| 123 |
MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>( |
| 124 |
static_cast<In>(std::numeric_limits<Out>::lowest()) - aEpsilon))); |
| 125 |
} else { |
| 126 |
// Check for negative floats and unsigned integer types. |
| 127 |
MOZ_RELEASE_ASSERT((!IsInBounds<In, Out>(static_cast<In>(-1)))); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void TestFloatConversion() { |
| 132 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint64_t, float>(UINT64_MAX))); |
| 133 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint32_t, float>(UINT32_MAX))); |
| 134 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, float>(UINT16_MAX))); |
| 135 |
MOZ_RELEASE_ASSERT((IsInBounds<uint8_t, float>(UINT8_MAX))); |
| 136 |
|
| 137 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, float>(INT64_MAX))); |
| 138 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, float>(INT64_MIN))); |
| 139 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, float>(INT32_MAX))); |
| 140 |
MOZ_RELEASE_ASSERT((!IsInBounds<int32_t, float>(INT32_MIN))); |
| 141 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, float>(INT16_MAX))); |
| 142 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, float>(INT16_MIN))); |
| 143 |
MOZ_RELEASE_ASSERT((IsInBounds<int8_t, float>(INT8_MAX))); |
| 144 |
MOZ_RELEASE_ASSERT((IsInBounds<int8_t, float>(INT8_MIN))); |
| 145 |
|
| 146 |
MOZ_RELEASE_ASSERT((!IsInBounds<uint64_t, double>(UINT64_MAX))); |
| 147 |
MOZ_RELEASE_ASSERT((IsInBounds<uint32_t, double>(UINT32_MAX))); |
| 148 |
MOZ_RELEASE_ASSERT((IsInBounds<uint16_t, double>(UINT16_MAX))); |
| 149 |
MOZ_RELEASE_ASSERT((IsInBounds<uint8_t, double>(UINT8_MAX))); |
| 150 |
|
| 151 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, double>(INT64_MAX))); |
| 152 |
MOZ_RELEASE_ASSERT((!IsInBounds<int64_t, double>(INT64_MIN))); |
| 153 |
MOZ_RELEASE_ASSERT((IsInBounds<int32_t, double>(INT32_MAX))); |
| 154 |
MOZ_RELEASE_ASSERT((IsInBounds<int32_t, double>(INT32_MIN))); |
| 155 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, double>(INT16_MAX))); |
| 156 |
MOZ_RELEASE_ASSERT((IsInBounds<int16_t, double>(INT16_MIN))); |
| 157 |
MOZ_RELEASE_ASSERT((IsInBounds<int8_t, double>(INT8_MAX))); |
| 158 |
MOZ_RELEASE_ASSERT((IsInBounds<int8_t, double>(INT8_MIN))); |
| 159 |
|
| 160 |
// Floor check |
| 161 |
MOZ_RELEASE_ASSERT((IsInBounds<float, uint64_t>(4.3))); |
| 162 |
MOZ_RELEASE_ASSERT((AssertedCast<uint64_t>(4.3f) == 4u)); |
| 163 |
MOZ_RELEASE_ASSERT((IsInBounds<float, uint32_t>(4.3))); |
| 164 |
MOZ_RELEASE_ASSERT((AssertedCast<uint32_t>(4.3f) == 4u)); |
| 165 |
MOZ_RELEASE_ASSERT((IsInBounds<float, uint16_t>(4.3))); |
| 166 |
MOZ_RELEASE_ASSERT((AssertedCast<uint16_t>(4.3f) == 4u)); |
| 167 |
MOZ_RELEASE_ASSERT((IsInBounds<float, uint8_t>(4.3))); |
| 168 |
MOZ_RELEASE_ASSERT((AssertedCast<uint8_t>(4.3f) == 4u)); |
| 169 |
|
| 170 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int64_t>(4.3))); |
| 171 |
MOZ_RELEASE_ASSERT((AssertedCast<int64_t>(4.3f) == 4u)); |
| 172 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int32_t>(4.3))); |
| 173 |
MOZ_RELEASE_ASSERT((AssertedCast<int32_t>(4.3f) == 4u)); |
| 174 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int16_t>(4.3))); |
| 175 |
MOZ_RELEASE_ASSERT((AssertedCast<int16_t>(4.3f) == 4u)); |
| 176 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int8_t>(4.3))); |
| 177 |
MOZ_RELEASE_ASSERT((AssertedCast<int8_t>(4.3f) == 4u)); |
| 178 |
|
| 179 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int64_t>(-4.3))); |
| 180 |
MOZ_RELEASE_ASSERT((AssertedCast<int64_t>(-4.3f) == -4)); |
| 181 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int32_t>(-4.3))); |
| 182 |
MOZ_RELEASE_ASSERT((AssertedCast<int32_t>(-4.3f) == -4)); |
| 183 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int16_t>(-4.3))); |
| 184 |
MOZ_RELEASE_ASSERT((AssertedCast<int16_t>(-4.3f) == -4)); |
| 185 |
MOZ_RELEASE_ASSERT((IsInBounds<float, int8_t>(-4.3))); |
| 186 |
MOZ_RELEASE_ASSERT((AssertedCast<int8_t>(-4.3f) == -4)); |
| 187 |
|
| 188 |
// Bound check for float to unsigned integer conversion. The parameters are |
| 189 |
// espilons and offsets allowing to check boundaries, that depend on the |
| 190 |
// magnitude of the numbers. |
| 191 |
checkBoundariesFloating<double, uint64_t>(2049.); |
| 192 |
checkBoundariesFloating<double, uint32_t>(1.); |
| 193 |
checkBoundariesFloating<double, uint16_t>(1.); |
| 194 |
checkBoundariesFloating<double, uint8_t>(1.); |
| 195 |
// Large number because of the lack of precision of floats at this magnitude |
| 196 |
checkBoundariesFloating<float, uint64_t>(1.1e12f); |
| 197 |
checkBoundariesFloating<float, uint32_t>(1.f, 128u); |
| 198 |
checkBoundariesFloating<float, uint16_t>(1.f); |
| 199 |
checkBoundariesFloating<float, uint8_t>(1.f); |
| 200 |
|
| 201 |
checkBoundariesFloating<double, int64_t>(1025.); |
| 202 |
checkBoundariesFloating<double, int32_t>(1.); |
| 203 |
checkBoundariesFloating<double, int16_t>(1.); |
| 204 |
checkBoundariesFloating<double, int8_t>(1.); |
| 205 |
// Large number because of the lack of precision of floats at this magnitude |
| 206 |
checkBoundariesFloating<float, int64_t>(1.1e12f); |
| 207 |
checkBoundariesFloating<float, int32_t>(256.f, 64u); |
| 208 |
checkBoundariesFloating<float, int16_t>(1.f); |
| 209 |
checkBoundariesFloating<float, int8_t>(1.f); |
| 210 |
|
| 211 |
// Integer to floating point, boundary cases |
| 212 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, float>( |
| 213 |
int64_t(std::pow(2, floatMantissaBitsPlusOne)) + 1))); |
| 214 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>( |
| 215 |
int64_t(std::pow(2, floatMantissaBitsPlusOne))))); |
| 216 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>( |
| 217 |
int64_t(std::pow(2, floatMantissaBitsPlusOne)) - 1))); |
| 218 |
|
| 219 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, float>( |
| 220 |
int64_t(-std::pow(2, floatMantissaBitsPlusOne)) - 1))); |
| 221 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>( |
| 222 |
int64_t(-std::pow(2, floatMantissaBitsPlusOne))))); |
| 223 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, float>( |
| 224 |
int64_t(-std::pow(2, floatMantissaBitsPlusOne)) + 1))); |
| 225 |
|
| 226 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>( |
| 227 |
uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) + 1))); |
| 228 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>( |
| 229 |
uint64_t(std::pow(2, doubleMantissaBitsPlusOne))))); |
| 230 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>( |
| 231 |
uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) - 1))); |
| 232 |
|
| 233 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>( |
| 234 |
int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) - 1))); |
| 235 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>( |
| 236 |
int64_t(-std::pow(2, doubleMantissaBitsPlusOne))))); |
| 237 |
MOZ_RELEASE_ASSERT((IsInBounds<int64_t, double>( |
| 238 |
int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) + 1))); |
| 239 |
|
| 240 |
MOZ_RELEASE_ASSERT(!(IsInBounds<uint64_t, double>(UINT64_MAX))); |
| 241 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(INT64_MAX))); |
| 242 |
MOZ_RELEASE_ASSERT(!(IsInBounds<int64_t, double>(INT64_MIN))); |
| 243 |
|
| 244 |
MOZ_RELEASE_ASSERT( |
| 245 |
!(IsInBounds<double, float>(std::numeric_limits<double>::max()))); |
| 246 |
MOZ_RELEASE_ASSERT( |
| 247 |
!(IsInBounds<double, float>(-std::numeric_limits<double>::max()))); |
| 248 |
} |
| 249 |
|
| 250 |
#define ASSERT_EQ(a, b) \ |
| 251 |
if ((a) != (b)) { \ |
| 252 |
std::cerr << __FILE__ << ":" << __LINE__ << " Actual: " << +(a) << ", " \ |
| 253 |
<< "expected: " << +(b) << std::endl; \ |
| 254 |
MOZ_CRASH(); \ |
| 255 |
} |
| 256 |
|
| 257 |
#ifdef ENABLE_DEBUG_PRINT |
| 258 |
# define DEBUG_PRINT(in, out) \ |
| 259 |
std::cout << "\tIn: " << +in << ", " << "out: " << +out << std::endl; |
| 260 |
#else |
| 261 |
# define DEBUG_PRINT(in, out) |
| 262 |
#endif |
| 263 |
|
| 264 |
template <typename In, typename Out> |
| 265 |
void TestTypePairImpl() { |
| 266 |
std::cout << __PRETTY_FUNCTION__ << std::endl; |
| 267 |
std::cout << std::fixed; |
| 268 |
// Test casting infinities to integer works |
| 269 |
if constexpr (std::is_floating_point_v<In> && |
| 270 |
!std::is_floating_point_v<Out>) { |
| 271 |
Out v = SaturatingCast<Out>(std::numeric_limits<In>::infinity()); |
| 272 |
ASSERT_EQ(v, std::numeric_limits<Out>::max()); |
| 273 |
v = SaturatingCast<Out>(-std::numeric_limits<In>::infinity()); |
| 274 |
ASSERT_EQ(v, std::numeric_limits<Out>::lowest()); |
| 275 |
} |
| 276 |
// Saturation of a floating point value that is infinity is infinity |
| 277 |
if constexpr (std::is_floating_point_v<Out> && std::is_floating_point_v<In>) { |
| 278 |
In in = std::numeric_limits<In>::infinity(); |
| 279 |
Out v = SaturatingCast<Out>(in); |
| 280 |
DEBUG_PRINT(in, v); |
| 281 |
ASSERT_EQ(v, std::numeric_limits<Out>::infinity()); |
| 282 |
in = -std::numeric_limits<In>::infinity(); |
| 283 |
v = SaturatingCast<In>(in); |
| 284 |
DEBUG_PRINT(in, v); |
| 285 |
ASSERT_EQ(v, -std::numeric_limits<Out>::infinity()); |
| 286 |
return; |
| 287 |
} else { |
| 288 |
if constexpr (sizeof(In) > sizeof(Out) && std::is_integral_v<Out>) { |
| 289 |
// Test with a value just outside the range of the output type |
| 290 |
In in = static_cast<In>(std::numeric_limits<Out>::max()) + 1ull; |
| 291 |
Out v = SaturatingCast<Out>(in); |
| 292 |
DEBUG_PRINT(in, v); |
| 293 |
ASSERT_EQ(v, std::numeric_limits<Out>::max()); |
| 294 |
|
| 295 |
if (std::is_signed_v<In>) { |
| 296 |
// Test with a value just below the range of the output type |
| 297 |
Out lowest = std::numeric_limits<Out>::lowest(); |
| 298 |
in = static_cast<In>(lowest) - 1; |
| 299 |
v = SaturatingCast<Out>(in); |
| 300 |
DEBUG_PRINT(in, v); |
| 301 |
if constexpr (std::is_signed_v<In> && !std::is_signed_v<Out>) { |
| 302 |
ASSERT_EQ(v, 0); |
| 303 |
} else { |
| 304 |
ASSERT_EQ(v, std::numeric_limits<Out>::lowest()); |
| 305 |
} |
| 306 |
} |
| 307 |
} else if constexpr (std::is_integral_v<In> && std::is_integral_v<Out> && |
| 308 |
sizeof(In) == sizeof(Out) && !std::is_signed_v<In> && |
| 309 |
std::is_signed_v<Out>) { |
| 310 |
// Test that max uintXX_t saturates to max intXX_t |
| 311 |
In in = static_cast<In>(std::numeric_limits<Out>::max()) + 1; |
| 312 |
Out v = SaturatingCast<Out>(in); |
| 313 |
DEBUG_PRINT(in, v); |
| 314 |
ASSERT_EQ(v, std::numeric_limits<Out>::max()); |
| 315 |
} |
| 316 |
|
| 317 |
// SaturatingCast of zero is zero |
| 318 |
In in = static_cast<In>(0); |
| 319 |
Out v = SaturatingCast<Out>(in); |
| 320 |
DEBUG_PRINT(in, v); |
| 321 |
ASSERT_EQ(v, 0); |
| 322 |
|
| 323 |
if constexpr (sizeof(In) >= sizeof(Out) && std::is_signed_v<Out> && |
| 324 |
std::is_signed_v<In>) { |
| 325 |
// Test with a value within the range of the output type |
| 326 |
In in = static_cast<In>(std::numeric_limits<Out>::max() / 2); |
| 327 |
Out v = SaturatingCast<Out>(in); |
| 328 |
DEBUG_PRINT(in, v); |
| 329 |
ASSERT_EQ(v, in); |
| 330 |
|
| 331 |
// Test with a negative value within the range of the output type |
| 332 |
in = static_cast<In>(std::numeric_limits<Out>::lowest() / 2); |
| 333 |
v = SaturatingCast<Out>(in); |
| 334 |
DEBUG_PRINT(in, v); |
| 335 |
ASSERT_EQ(v, in); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
template <typename In, typename Out> |
| 341 |
void TestTypePair() { |
| 342 |
constexpr bool fromFloat = std::is_floating_point_v<In>; |
| 343 |
constexpr bool toFloat = std::is_floating_point_v<Out>; |
| 344 |
// Don't test casting to the same type |
| 345 |
if constexpr (!std::is_same_v<In, Out>) { |
| 346 |
if constexpr ((fromFloat && !toFloat) || (!fromFloat && !toFloat)) { |
| 347 |
TestTypePairImpl<In, Out>(); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
template <typename T, typename... Ts> |
| 353 |
void for_each_type_pair(std::tuple<T, Ts...>) { |
| 354 |
(TestTypePair<T, Ts>(), ...); |
| 355 |
(TestTypePair<Ts, T>(), ...); |
| 356 |
if constexpr (sizeof...(Ts) > 1) { |
| 357 |
for_each_type_pair(std::tuple<Ts...>{}); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
template <typename... Args> |
| 362 |
void TestSaturatingCastImpl() { |
| 363 |
for_each_type_pair(std::tuple<Args...>{}); |
| 364 |
} |
| 365 |
|
| 366 |
template <typename T, typename... Ts> |
| 367 |
void TestFirstToOthers() { |
| 368 |
(TestTypePair<T, Ts>(), ...); |
| 369 |
} |
| 370 |
|
| 371 |
void TestSaturatingCast() { |
| 372 |
// Each integer type against every other |
| 373 |
TestSaturatingCastImpl<short, int, long, int8_t, uint8_t, int16_t, uint16_t, |
| 374 |
int32_t, uint32_t, int64_t, uint64_t>(); |
| 375 |
|
| 376 |
// Floating point types to every integer type |
| 377 |
TestFirstToOthers<float, short, int, long, int8_t, uint8_t, int16_t, uint16_t, |
| 378 |
int32_t, uint32_t, int64_t, uint64_t>(); |
| 379 |
TestFirstToOthers<double, short, int, long, int8_t, uint8_t, int16_t, |
| 380 |
uint16_t, int32_t, uint32_t, int64_t, uint64_t>(); |
| 381 |
} |
| 382 |
|
| 383 |
int main() { |
| 384 |
TestBitwiseCast(); |
| 385 |
|
| 386 |
TestSameSize(); |
| 387 |
TestToBiggerSize(); |
| 388 |
TestToSmallerSize(); |
| 389 |
TestFloatConversion(); |
| 390 |
TestSaturatingCast(); |
| 391 |
|
| 392 |
return 0; |
| 393 |
} |