@@ -126,22 +126,50 @@ namespace pythonic
126126 template <Integral T>
127127 constexpr bool would_add_overflow (T a, T b) noexcept
128128 {
129+ #ifdef _MSC_VER
130+ T res = a + b;
131+ if constexpr (std::is_signed_v<T>) {
132+ return ((a > 0 && b > 0 && res < 0 ) || (a < 0 && b < 0 && res > 0 ));
133+ } else {
134+ return res < a;
135+ }
136+ #else
129137 T res;
130138 return __builtin_add_overflow (a, b, &res);
139+ #endif
131140 }
132141
133142 template <Integral T>
134143 constexpr bool would_sub_overflow (T a, T b) noexcept
135144 {
145+ #ifdef _MSC_VER
146+ T res = a - b;
147+ if constexpr (std::is_signed_v<T>) {
148+ return ((a > 0 && b < 0 && res < 0 ) || (a < 0 && b > 0 && res > 0 ));
149+ } else {
150+ return res > a;
151+ }
152+ #else
136153 T res;
137154 return __builtin_sub_overflow (a, b, &res);
155+ #endif
138156 }
139157
140158 template <Integral T>
141159 constexpr bool would_mul_overflow (T a, T b) noexcept
142160 {
161+ #ifdef _MSC_VER
162+ T res = a * b;
163+ if constexpr (std::is_signed_v<T>) {
164+ bool same_sign = (a >= 0 ) == (b >= 0 );
165+ return (same_sign && res < 0 ) || (!same_sign && res > 0 );
166+ } else {
167+ return (a != 0 && b > std::numeric_limits<T>::max () / a);
168+ }
169+ #else
143170 T res;
144171 return __builtin_mul_overflow (a, b, &res);
172+ #endif
145173 }
146174
147175 // Floating point doesn't have traditional overflow, but we can check for infinity
@@ -175,9 +203,12 @@ namespace pythonic
175203#ifdef _MSC_VER
176204 T res = a + b;
177205 bool overflow;
178- if constexpr (std::is_signed_v<T>) {
206+ if constexpr (std::is_signed_v<T>)
207+ {
179208 overflow = ((a > 0 && b > 0 && res < 0 ) || (a < 0 && b < 0 && res > 0 ));
180- } else {
209+ }
210+ else
211+ {
181212 overflow = res < a;
182213 }
183214 if (overflow)
@@ -208,9 +239,12 @@ namespace pythonic
208239#ifdef _MSC_VER
209240 T res = a - b;
210241 bool overflow;
211- if constexpr (std::is_signed_v<T>) {
242+ if constexpr (std::is_signed_v<T>)
243+ {
212244 overflow = ((a > 0 && b < 0 && res < 0 ) || (a < 0 && b > 0 && res > 0 ));
213- } else {
245+ }
246+ else
247+ {
214248 overflow = res > a;
215249 }
216250 if (overflow)
@@ -241,10 +275,13 @@ namespace pythonic
241275#ifdef _MSC_VER
242276 T res = a * b;
243277 bool overflow;
244- if constexpr (std::is_signed_v<T>) {
278+ if constexpr (std::is_signed_v<T>)
279+ {
245280 bool same_sign = (a >= 0 ) == (b >= 0 );
246281 overflow = (same_sign && res < 0 ) || (!same_sign && res > 0 );
247- } else {
282+ }
283+ else
284+ {
248285 overflow = (a != 0 && b > std::numeric_limits<T>::max () / a);
249286 }
250287 if (overflow)
0 commit comments