V8 Project
safe_math_impl.h
Go to the documentation of this file.
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Slightly adapted for inclusion in V8.
6 // Copyright 2014 the V8 project authors. All rights reserved.
7 
8 #ifndef V8_BASE_SAFE_MATH_IMPL_H_
9 #define V8_BASE_SAFE_MATH_IMPL_H_
10 
11 #include <stdint.h>
12 
13 #include <cmath>
14 #include <cstdlib>
15 #include <limits>
16 
17 #include "src/base/macros.h"
19 
20 namespace v8 {
21 namespace base {
22 namespace internal {
23 
24 
25 // From Chromium's base/template_util.h:
26 
27 template<class T, T v>
29  static const T value = v;
30  typedef T value_type;
32 };
33 
34 template <class T, T v> const T integral_constant<T, v>::value;
35 
38 
39 template <class T, class U> struct is_same : public false_type {};
40 template <class T> struct is_same<T, T> : true_type {};
41 
42 template<bool B, class T = void>
43 struct enable_if {};
44 
45 template<class T>
46 struct enable_if<true, T> { typedef T type; };
47 
48 // </template_util.h>
49 
50 
51 // Everything from here up to the floating point operations is portable C++,
52 // but it may not be fast. This code could be split based on
53 // platform/architecture and replaced with potentially faster implementations.
54 
55 // Integer promotion templates used by the portable checked integer arithmetic.
56 template <size_t Size, bool IsSigned>
58 template <>
60  typedef int8_t type;
61 };
62 template <>
64  typedef uint8_t type;
65 };
66 template <>
68  typedef int16_t type;
69 };
70 template <>
72  typedef uint16_t type;
73 };
74 template <>
76  typedef int32_t type;
77 };
78 template <>
80  typedef uint32_t type;
81 };
82 template <>
84  typedef int64_t type;
85 };
86 template <>
88  typedef uint64_t type;
89 };
90 
91 // WARNING: We have no IntegerForSizeAndSign<16, *>. If we ever add one to
92 // support 128-bit math, then the ArithmeticPromotion template below will need
93 // to be updated (or more likely replaced with a decltype expression).
94 
95 template <typename Integer>
97  typedef typename enable_if<
98  std::numeric_limits<Integer>::is_integer,
99  typename IntegerForSizeAndSign<sizeof(Integer), false>::type>::type type;
100 };
101 
102 template <typename Integer>
104  typedef typename enable_if<
105  std::numeric_limits<Integer>::is_integer,
106  typename IntegerForSizeAndSign<sizeof(Integer), true>::type>::type type;
107 };
108 
109 template <typename Integer>
111  typedef typename enable_if<
112  std::numeric_limits<Integer>::is_integer,
113  typename IntegerForSizeAndSign<
114  sizeof(Integer) * 2,
115  std::numeric_limits<Integer>::is_signed>::type>::type type;
116 };
117 
118 template <typename Integer>
121  size_t>::type value = 8 * sizeof(Integer) - 1;
122 };
123 
124 // Helper templates for integer manipulations.
125 
126 template <typename T>
127 bool HasSignBit(T x) {
128  // Cast to unsigned since right shift on signed is undefined.
129  return !!(static_cast<typename UnsignedIntegerForSize<T>::type>(x) >>
131 }
132 
133 // This wrapper undoes the standard integer promotions.
134 template <typename T>
136  return ~x;
137 }
138 
139 // Here are the actual portable checked integer math implementations.
140 // TODO(jschuh): Break this code out from the enable_if pattern and find a clean
141 // way to coalesce things into the CheckedNumericState specializations below.
142 
143 template <typename T>
144 typename enable_if<std::numeric_limits<T>::is_integer, T>::type
146  // Since the value of x+y is undefined if we have a signed type, we compute
147  // it using the unsigned type of the same size.
148  typedef typename UnsignedIntegerForSize<T>::type UnsignedDst;
149  UnsignedDst ux = static_cast<UnsignedDst>(x);
150  UnsignedDst uy = static_cast<UnsignedDst>(y);
151  UnsignedDst uresult = ux + uy;
152  // Addition is valid if the sign of (x + y) is equal to either that of x or
153  // that of y.
154  if (std::numeric_limits<T>::is_signed) {
155  if (HasSignBit(BinaryComplement((uresult ^ ux) & (uresult ^ uy))))
157  else // Direction of wrap is inverse of result sign.
159 
160  } else { // Unsigned is either valid or overflow.
162  }
163  return static_cast<T>(uresult);
164 }
165 
166 template <typename T>
167 typename enable_if<std::numeric_limits<T>::is_integer, T>::type
169  // Since the value of x+y is undefined if we have a signed type, we compute
170  // it using the unsigned type of the same size.
171  typedef typename UnsignedIntegerForSize<T>::type UnsignedDst;
172  UnsignedDst ux = static_cast<UnsignedDst>(x);
173  UnsignedDst uy = static_cast<UnsignedDst>(y);
174  UnsignedDst uresult = ux - uy;
175  // Subtraction is valid if either x and y have same sign, or (x-y) and x have
176  // the same sign.
177  if (std::numeric_limits<T>::is_signed) {
178  if (HasSignBit(BinaryComplement((uresult ^ ux) & (ux ^ uy))))
180  else // Direction of wrap is inverse of result sign.
182 
183  } else { // Unsigned is either valid or underflow.
185  }
186  return static_cast<T>(uresult);
187 }
188 
189 // Integer multiplication is a bit complicated. In the fast case we just
190 // we just promote to a twice wider type, and range check the result. In the
191 // slow case we need to manually check that the result won't be truncated by
192 // checking with division against the appropriate bound.
193 template <typename T>
194 typename enable_if<
195  std::numeric_limits<T>::is_integer && sizeof(T) * 2 <= sizeof(uintmax_t),
196  T>::type
198  typedef typename TwiceWiderInteger<T>::type IntermediateType;
199  IntermediateType tmp =
200  static_cast<IntermediateType>(x) * static_cast<IntermediateType>(y);
201  *validity = DstRangeRelationToSrcRange<T>(tmp);
202  return static_cast<T>(tmp);
203 }
204 
205 template <typename T>
206 typename enable_if<std::numeric_limits<T>::is_integer &&
207  std::numeric_limits<T>::is_signed &&
208  (sizeof(T) * 2 > sizeof(uintmax_t)),
209  T>::type
211  // if either side is zero then the result will be zero.
212  if (!(x || y)) {
213  return RANGE_VALID;
214 
215  } else if (x > 0) {
216  if (y > 0)
217  *validity =
218  x <= std::numeric_limits<T>::max() / y ? RANGE_VALID : RANGE_OVERFLOW;
219  else
221  : RANGE_UNDERFLOW;
222 
223  } else {
224  if (y > 0)
226  : RANGE_UNDERFLOW;
227  else
229  y >= std::numeric_limits<T>::max() / x ? RANGE_VALID : RANGE_OVERFLOW;
230  }
231 
232  return x * y;
233 }
234 
235 template <typename T>
236 typename enable_if<std::numeric_limits<T>::is_integer &&
237  !std::numeric_limits<T>::is_signed &&
238  (sizeof(T) * 2 > sizeof(uintmax_t)),
239  T>::type
241  *validity = (y == 0 || x <= std::numeric_limits<T>::max() / y)
242  ? RANGE_VALID
243  : RANGE_OVERFLOW;
244  return x * y;
245 }
246 
247 // Division just requires a check for an invalid negation on signed min/-1.
248 template <typename T>
250  T x,
251  T y,
253  typename enable_if<std::numeric_limits<T>::is_integer, int>::type = 0) {
254  if (std::numeric_limits<T>::is_signed && x == std::numeric_limits<T>::min() &&
255  y == static_cast<T>(-1)) {
258  }
259 
261  return x / y;
262 }
263 
264 template <typename T>
265 typename enable_if<
266  std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
267  T>::type
269  *validity = y > 0 ? RANGE_VALID : RANGE_INVALID;
270  return x % y;
271 }
272 
273 template <typename T>
274 typename enable_if<
275  std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
276  T>::type
279  return x % y;
280 }
281 
282 template <typename T>
283 typename enable_if<
284  std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
285  T>::type
287  *validity =
289  // The negation of signed min is min, so catch that one.
290  return -value;
291 }
292 
293 template <typename T>
294 typename enable_if<
295  std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
296  T>::type
298  // The only legal unsigned negation is zero.
299  *validity = value ? RANGE_UNDERFLOW : RANGE_VALID;
300  return static_cast<T>(
301  -static_cast<typename SignedIntegerForSize<T>::type>(value));
302 }
303 
304 template <typename T>
305 typename enable_if<
306  std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
307  T>::type
309  *validity =
311  return std::abs(value);
312 }
313 
314 template <typename T>
315 typename enable_if<
316  std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
317  T>::type
319  // Absolute value of a positive is just its identiy.
321  return value;
322 }
323 
324 // These are the floating point stubs that the compiler needs to see. Only the
325 // negation operation is ever called.
326 #define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \
327  template <typename T> \
328  typename enable_if<std::numeric_limits<T>::is_iec559, T>::type \
329  Checked##NAME(T, T, RangeConstraint*) { \
330  UNREACHABLE(); \
331  return 0; \
332  }
333 
339 
340 #undef BASE_FLOAT_ARITHMETIC_STUBS
341 
342 template <typename T>
344  T value,
345  RangeConstraint*) {
346  return -value;
347 }
348 
349 template <typename T>
351  T value,
352  RangeConstraint*) {
353  return std::abs(value);
354 }
355 
356 // Floats carry around their validity state with them, but integers do not. So,
357 // we wrap the underlying value in a specialization in order to hide that detail
358 // and expose an interface via accessors.
363 };
364 
365 template <typename NumericType>
368  std::numeric_limits<NumericType>::is_integer
370  : (std::numeric_limits<NumericType>::is_iec559 ? NUMERIC_FLOATING
371  : NUMERIC_UNKNOWN);
372 };
373 
374 template <typename T, NumericRepresentation type =
377 
378 // Integrals require quite a bit of additional housekeeping to manage state.
379 template <typename T>
381  private:
384 
385  public:
386  template <typename Src, NumericRepresentation type>
387  friend class CheckedNumericState;
388 
389  CheckedNumericState() : value_(0), validity_(RANGE_VALID) {}
390 
391  template <typename Src>
393  : value_(value),
394  validity_(GetRangeConstraint(validity |
395  DstRangeRelationToSrcRange<T>(value))) {
396  // Argument must be numeric.
397  STATIC_ASSERT(std::numeric_limits<Src>::is_specialized);
398  }
399 
400  // Copy constructor.
401  template <typename Src>
403  : value_(static_cast<T>(rhs.value())),
404  validity_(GetRangeConstraint(
405  rhs.validity() | DstRangeRelationToSrcRange<T>(rhs.value()))) {}
406 
407  template <typename Src>
409  Src value,
410  typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type =
411  0)
412  : value_(static_cast<T>(value)),
413  validity_(DstRangeRelationToSrcRange<T>(value)) {}
414 
415  RangeConstraint validity() const { return validity_; }
416  T value() const { return value_; }
417 };
418 
419 // Floating points maintain their own validity, but need translation wrappers.
420 template <typename T>
422  private:
424 
425  public:
426  template <typename Src, NumericRepresentation type>
427  friend class CheckedNumericState;
428 
429  CheckedNumericState() : value_(0.0) {}
430 
431  template <typename Src>
433  Src value,
435  typename enable_if<std::numeric_limits<Src>::is_integer, int>::type = 0) {
436  switch (DstRangeRelationToSrcRange<T>(value)) {
437  case RANGE_VALID:
438  value_ = static_cast<T>(value);
439  break;
440 
441  case RANGE_UNDERFLOW:
442  value_ = -std::numeric_limits<T>::infinity();
443  break;
444 
445  case RANGE_OVERFLOW:
446  value_ = std::numeric_limits<T>::infinity();
447  break;
448 
449  case RANGE_INVALID:
450  value_ = std::numeric_limits<T>::quiet_NaN();
451  break;
452  }
453  }
454 
455  template <typename Src>
457  Src value,
458  typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type =
459  0)
460  : value_(static_cast<T>(value)) {}
461 
462  // Copy constructor.
463  template <typename Src>
465  : value_(static_cast<T>(rhs.value())) {}
466 
468  return GetRangeConstraint(value_ <= std::numeric_limits<T>::max(),
469  value_ >= -std::numeric_limits<T>::max());
470  }
471  T value() const { return value_; }
472 };
473 
474 // For integers less than 128-bit and floats 32-bit or larger, we can distil
475 // C/C++ arithmetic promotions down to two simple rules:
476 // 1. The type with the larger maximum exponent always takes precedence.
477 // 2. The resulting type must be promoted to at least an int.
478 // The following template specializations implement that promotion logic.
483 };
484 
485 template <typename Lhs,
486  typename Rhs = Lhs,
487  ArithmeticPromotionCategory Promotion =
494  : DEFAULT_PROMOTION) >
496 
497 template <typename Lhs, typename Rhs>
499  typedef Lhs type;
500 };
501 
502 template <typename Lhs, typename Rhs>
504  typedef Rhs type;
505 };
506 
507 template <typename Lhs, typename Rhs>
509  typedef int type;
510 };
511 
512 // We can statically check if operations on the provided types can wrap, so we
513 // can skip the checked operations if they're not needed. So, for an integer we
514 // care if the destination type preserves the sign and is twice the width of
515 // the source.
516 template <typename T, typename Lhs, typename Rhs>
518  static const bool value = !std::numeric_limits<T>::is_iec559 &&
521  sizeof(T) >= (2 * sizeof(Lhs)) &&
524  sizeof(T) >= (2 * sizeof(Rhs));
525 };
526 
527 } // namespace internal
528 } // namespace base
529 } // namespace v8
530 
531 #endif // V8_BASE_SAFE_MATH_IMPL_H_
A JavaScript value representing a signed integer.
Definition: v8.h:2176
CheckedNumericState(const CheckedNumericState< Src > &rhs)
CheckedNumericState(Src value, RangeConstraint validity, typename enable_if< std::numeric_limits< Src >::is_integer, int >::type=0)
CheckedNumericState(Src value, typename enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
CheckedNumericState(const CheckedNumericState< Src > &rhs)
CheckedNumericState(Src value, typename enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
CheckedNumericState(Src value, RangeConstraint validity)
enable harmony numeric enable harmony object literal extensions true
#define STATIC_ASSERT(test)
Definition: macros.h:311
unsigned short uint16_t
Definition: unicode.cc:23
signed short int16_t
Definition: unicode.cc:22
int int32_t
Definition: unicode.cc:24
enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedAbs(T value, RangeConstraint *validity)
integral_constant< bool, false > false_type
enable_if< std::numeric_limits< T >::is_integer, T >::type CheckedSub(T x, T y, RangeConstraint *validity)
enable_if< std::numeric_limits< T >::is_integer, T >::type CheckedAdd(T x, T y, RangeConstraint *validity)
RangeConstraint GetRangeConstraint(int integer_range_constraint)
RangeConstraint DstRangeRelationToSrcRange(Src value)
enable_if< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed &&(sizeof(T) *2 > sizeof(uintmax_t)), T >::type CheckedMul(T x, T y, RangeConstraint *validity)
integral_constant< bool, true > true_type
T CheckedDiv(T x, T y, RangeConstraint *validity, typename enable_if< std::numeric_limits< T >::is_integer, int >::type=0)
enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedNeg(T value, RangeConstraint *validity)
enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedMod(T x, T y, RangeConstraint *validity)
static int min(int a, int b)
Definition: liveedit.cc:273
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
#define BASE_FLOAT_ARITHMETIC_STUBS(NAME)
static const NumericRepresentation value
static const enable_if< std::numeric_limits< Integer >::is_integer, size_t >::type value
enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer), true >::type >::type type
enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer) *2, std::numeric_limits< Integer >::is_signed >::type >::type type
enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer), false >::type >::type type
integral_constant< T, v > type
#define T(name, string, precedence)
Definition: token.cc:25