File: overflowing-math.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 3,492 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019 the V8 project authors. All rights reserved.
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_
#define THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_

#include <stdint.h>

#include <cmath>
#include <type_traits>

namespace fdlibm {

// Helpers for performing overflowing arithmetic operations without relying
// on C++ undefined behavior.
#define ASSERT_SIGNED_INTEGER_TYPE(Type)                                      \
  static_assert(std::is_integral<Type>::value && std::is_signed<Type>::value, \
                "use this for signed integer types");
#define OP_WITH_WRAPAROUND(Name, OP)                                      \
  template <typename signed_type>                                         \
  inline signed_type Name##WithWraparound(signed_type a, signed_type b) { \
    ASSERT_SIGNED_INTEGER_TYPE(signed_type);                              \
    using unsigned_type = typename std::make_unsigned<signed_type>::type; \
    unsigned_type a_unsigned = static_cast<unsigned_type>(a);             \
    unsigned_type b_unsigned = static_cast<unsigned_type>(b);             \
    unsigned_type result = a_unsigned OP b_unsigned;                      \
    return static_cast<signed_type>(result);                              \
  }

OP_WITH_WRAPAROUND(Add, +)
OP_WITH_WRAPAROUND(Sub, -)
OP_WITH_WRAPAROUND(Mul, *)

// 16-bit integers are special due to C++'s implicit conversion rules.
// See https://bugs.llvm.org/show_bug.cgi?id=25580.
template <>
inline int16_t MulWithWraparound(int16_t a, int16_t b) {
  uint32_t a_unsigned = static_cast<uint32_t>(a);
  uint32_t b_unsigned = static_cast<uint32_t>(b);
  uint32_t result = a_unsigned * b_unsigned;
  return static_cast<int16_t>(static_cast<uint16_t>(result));
}

#undef OP_WITH_WRAPAROUND

template <typename signed_type>
inline signed_type NegateWithWraparound(signed_type a) {
  ASSERT_SIGNED_INTEGER_TYPE(signed_type);
  if (a == std::numeric_limits<signed_type>::min()) return a;
  return -a;
}

template <typename signed_type>
inline signed_type ShlWithWraparound(signed_type a, signed_type b) {
  ASSERT_SIGNED_INTEGER_TYPE(signed_type);
  using unsigned_type = typename std::make_unsigned<signed_type>::type;
  const unsigned_type kMask = (sizeof(a) * 8) - 1;
  return static_cast<signed_type>(static_cast<unsigned_type>(a) << (b & kMask));
}

#undef ASSERT_SIGNED_INTEGER_TYPE

// Returns the quotient x/y, avoiding C++ undefined behavior if y == 0.
template <typename T>
inline T Divide(T x, T y) {
  if (y != 0) return x / y;
  if (x == 0 || x != x) return std::numeric_limits<T>::quiet_NaN();
  if ((x >= 0) == (std::signbit(y) == 0)) {
    return std::numeric_limits<T>::infinity();
  }
  return -std::numeric_limits<T>::infinity();
}

inline float Recip(float a) { return Divide(1.0f, a); }

inline float RecipSqrt(float a) {
  if (a != 0) return 1.0f / std::sqrt(a);
  if (std::signbit(a) == 0) return std::numeric_limits<float>::infinity();
  return -std::numeric_limits<float>::infinity();
}

template <typename T>
inline T RoundingAverageUnsigned(T a, T b) {
  static_assert(std::is_unsigned<T>::value, "Only for unsiged types");
  static_assert(sizeof(T) < sizeof(uint64_t), "Must be smaller than uint64_t");
  return (static_cast<uint64_t>(a) + static_cast<uint64_t>(b) + 1) >> 1;
}

}  // namespace fdlibm

#endif  // THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_