10 #include <type_traits>
16 #if !defined(NDEBUG) || defined(ARMNN_NUMERIC_CAST_TESTABLE)
17 #define ENABLE_NUMERIC_CAST_CHECKS 1
19 #define ENABLE_NUMERIC_CAST_CHECKS 0
22 #if defined(ARMNN_NUMERIC_CAST_TESTABLE)
23 # define ARMNN_NUMERIC_CAST_CHECK(cond, msg) ConditionalThrow<std::bad_cast>(cond)
25 #define ARMNN_NUMERIC_CAST_CHECK(cond, msg) ARMNN_ASSERT_MSG(cond, msg)
30 template<
typename Dest,
typename Source>
31 typename std::enable_if_t<
32 std::is_unsigned<Source>::value &&
33 std::is_unsigned<Dest>::value,
37 #if ENABLE_NUMERIC_CAST_CHECKS
38 if (source > std::numeric_limits<Dest>::max())
41 "narrower unsigned type. Overflow detected.");
43 #endif // ENABLE_NUMERIC_CAST_CHECKS
45 return static_cast<Dest
>(source);
51 template<
typename Dest,
typename Source>
52 typename std::enable_if_t<
53 std::is_signed<Source>::value &&
54 std::is_integral<Source>::value &&
55 std::is_signed<Dest>::value &&
56 std::is_integral<Dest>::value,
60 #if ENABLE_NUMERIC_CAST_CHECKS
61 if (source > std::numeric_limits<Dest>::max())
64 "Overflow detected.");
67 if (source < std::numeric_limits<Dest>::lowest())
70 "Underflow detected.");
72 #endif // ENABLE_NUMERIC_CAST_CHECKS
74 return static_cast<Dest
>(source);
78 template<
typename Dest,
typename Source>
79 typename std::enable_if_t<
80 std::is_floating_point<Source>::value &&
81 std::is_floating_point<Dest>::value,
85 #if ENABLE_NUMERIC_CAST_CHECKS
86 if (source > std::numeric_limits<Dest>::max())
89 "Overflow detected.");
92 if (source < std::numeric_limits<Dest>::lowest())
95 "Underflow detected.");
97 #endif // ENABLE_NUMERIC_CAST_CHECKS
99 return static_cast<Dest
>(source);
103 template<
typename Dest,
typename Source>
104 typename std::enable_if_t<
105 std::is_floating_point<Source>::value &&
106 std::is_signed<Dest>::value &&
107 std::is_integral<Dest>::value,
111 #if ENABLE_NUMERIC_CAST_CHECKS
112 if (source >
static_cast<Source
>(std::numeric_limits<Dest>::max()))
115 "Overflow detected.");
118 if (source <
static_cast<Source
>(std::numeric_limits<Dest>::lowest()))
121 "Underflow detected.");
123 #endif // ENABLE_NUMERIC_CAST_CHECKS
125 return static_cast<Dest
>(source);
129 template<
typename Dest,
typename Source>
130 typename std::enable_if_t<
131 std::is_signed<Source>::value &&
132 std::is_integral<Source>::value &&
133 std::is_floating_point<Dest>::value,
137 #if ENABLE_NUMERIC_CAST_CHECKS
138 Dest sourceConverted =
static_cast<Dest
>(source);
140 if (sourceConverted > std::numeric_limits<Dest>::max())
143 "Overflow detected.");
146 if (sourceConverted < std::numeric_limits<Dest>::lowest())
149 "Underflow detected.");
151 #endif // ENABLE_NUMERIC_CAST_CHECKS
153 return static_cast<Dest
>(source);
159 template<
typename Dest,
typename Source>
160 typename std::enable_if_t<
161 std::is_signed<Dest>::value &&
162 std::is_integral<Dest>::value &&
163 std::is_unsigned<Source>::value,
167 #if ENABLE_NUMERIC_CAST_CHECKS
168 if (sValue >
static_cast< typename std::make_unsigned<Dest>::type
>(std::numeric_limits<Dest>::max()))
171 "Overflow detected.");
173 #endif // ENABLE_NUMERIC_CAST_CHECKS
175 return static_cast<Dest
>(sValue);
179 template<
typename Dest,
typename Source>
180 typename std::enable_if_t<
181 std::is_floating_point<Dest>::value &&
182 std::is_unsigned<Source>::value,
186 #if ENABLE_NUMERIC_CAST_CHECKS
187 if (
static_cast<Dest
>(sValue) > std::numeric_limits<Dest>::max())
190 "Overflow detected.");
192 #endif // ENABLE_NUMERIC_CAST_CHECKS
194 return static_cast<Dest
>(sValue);
200 template<
typename Dest,
typename Source>
201 typename std::enable_if_t<
202 std::is_unsigned<Dest>::value &&
203 std::is_signed<Source>::value &&
204 std::is_integral<Source>::value,
208 #if ENABLE_NUMERIC_CAST_CHECKS
212 "Underflow detected.");
215 if (
static_cast< typename std::make_unsigned<Source>::type
>(sValue) > std::numeric_limits<Dest>::max())
218 "Overflow detected.");
220 #endif // ENABLE_NUMERIC_CAST_CHECKS
221 return static_cast<Dest
>(sValue);
225 template<
typename Dest,
typename Source>
226 typename std::enable_if_t<
227 std::is_unsigned<Dest>::value &&
228 std::is_floating_point<Source>::value,
232 #if ENABLE_NUMERIC_CAST_CHECKS
236 "Underflow detected.");
239 if (sValue >
static_cast<Source
>(std::numeric_limits<Dest>::max()))
242 "Overflow detected.");
244 #endif // ENABLE_NUMERIC_CAST_CHECKS
245 return static_cast<Dest
>(sValue);
248 #undef ENABLE_NUMERIC_CAST_CHECKS