summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/common/third_party/base/anglebase/numerics/safe_math.h
blob: 3af4db63f7624096c90fc570dae5caaf66498328 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ANGLEBASE_NUMERICS_SAFE_MATH_H_
#define ANGLEBASE_NUMERICS_SAFE_MATH_H_

#include <stddef.h>

#include <limits>
#include <type_traits>

#include "anglebase/logging.h"
#include "anglebase/numerics/safe_math_impl.h"

namespace angle
{

namespace base
{

namespace internal
{

// CheckedNumeric implements all the logic and operators for detecting integer
// boundary conditions such as overflow, underflow, and invalid conversions.
// The CheckedNumeric type implicitly converts from floating point and integer
// data types, and contains overloads for basic arithmetic operations (i.e.: +,
// -, *, /, %).
//
// The following methods convert from CheckedNumeric to standard numeric values:
// IsValid() - Returns true if the underlying numeric value is valid (i.e. has
//             has not wrapped and is not the result of an invalid conversion).
// ValueOrDie() - Returns the underlying value. If the state is not valid this
//                call will crash on a CHECK.
// ValueOrDefault() - Returns the current value, or the supplied default if the
//                    state is not valid.
// ValueFloating() - Returns the underlying floating point value (valid only
//                   only for floating point CheckedNumeric types).
//
// Bitwise operations are explicitly not supported, because correct
// handling of some cases (e.g. sign manipulation) is ambiguous. Comparison
// operations are explicitly not supported because they could result in a crash
// on a CHECK condition. You should use patterns like the following for these
// operations:
// Bitwise operation:
//     CheckedNumeric<int> checked_int = untrusted_input_value;
//     int x = checked_int.ValueOrDefault(0) | kFlagValues;
// Comparison:
//   CheckedNumeric<size_t> checked_size = untrusted_input_value;
//   checked_size += HEADER LENGTH;
//   if (checked_size.IsValid() && checked_size.ValueOrDie() < buffer_size)
//     Do stuff...
template <typename T>
class CheckedNumeric
{
    static_assert(std::is_arithmetic<T>::value, "CheckedNumeric<T>: T must be a numeric type.");

  public:
    typedef T type;

    CheckedNumeric() {}

    // Copy constructor.
    template <typename Src>
    CheckedNumeric(const CheckedNumeric<Src> &rhs) : state_(rhs.ValueUnsafe(), rhs.validity())
    {
    }

    template <typename Src>
    CheckedNumeric(Src value, RangeConstraint validity) : state_(value, validity)
    {
    }

    // This is not an explicit constructor because we implicitly upgrade regular
    // numerics to CheckedNumerics to make them easier to use.
    template <typename Src>
    CheckedNumeric(Src value)  // NOLINT(runtime/explicit)
        : state_(value)
    {
        static_assert(std::numeric_limits<Src>::is_specialized, "Argument must be numeric.");
    }

    // This is not an explicit constructor because we want a seamless conversion
    // from StrictNumeric types.
    template <typename Src>
    CheckedNumeric(StrictNumeric<Src> value)  // NOLINT(runtime/explicit)
        : state_(static_cast<Src>(value))
    {
    }

    // IsValid() is the public API to test if a CheckedNumeric is currently valid.
    bool IsValid() const { return validity() == RANGE_VALID; }

    // ValueOrDie() The primary accessor for the underlying value. If the current
    // state is not valid it will CHECK and crash.
    T ValueOrDie() const
    {
        CHECK(IsValid());
        return state_.value();
    }

    // ValueOrDefault(T default_value) A convenience method that returns the
    // current value if the state is valid, and the supplied default_value for
    // any other state.
    T ValueOrDefault(T default_value) const { return IsValid() ? state_.value() : default_value; }

    // ValueFloating() - Since floating point values include their validity state,
    // we provide an easy method for extracting them directly, without a risk of
    // crashing on a CHECK.
    T ValueFloating() const
    {
        static_assert(std::numeric_limits<T>::is_iec559, "Argument must be float.");
        return CheckedNumeric<T>::cast(*this).ValueUnsafe();
    }

    // validity() - DO NOT USE THIS IN EXTERNAL CODE - It is public right now for
    // tests and to avoid a big matrix of friend operator overloads. But the
    // values it returns are likely to change in the future.
    // Returns: current validity state (i.e. valid, overflow, underflow, nan).
    // TODO(jschuh): crbug.com/332611 Figure out and implement semantics for
    // saturation/wrapping so we can expose this state consistently and implement
    // saturated arithmetic.
    RangeConstraint validity() const { return state_.validity(); }

    // ValueUnsafe() - DO NOT USE THIS IN EXTERNAL CODE - It is public right now
    // for tests and to avoid a big matrix of friend operator overloads. But the
    // values it returns are likely to change in the future.
    // Returns: the raw numeric value, regardless of the current state.
    // TODO(jschuh): crbug.com/332611 Figure out and implement semantics for
    // saturation/wrapping so we can expose this state consistently and implement
    // saturated arithmetic.
    T ValueUnsafe() const { return state_.value(); }

    // Prototypes for the supported arithmetic operator overloads.
    template <typename Src>
    CheckedNumeric &operator+=(Src rhs);
    template <typename Src>
    CheckedNumeric &operator-=(Src rhs);
    template <typename Src>
    CheckedNumeric &operator*=(Src rhs);
    template <typename Src>
    CheckedNumeric &operator/=(Src rhs);
    template <typename Src>
    CheckedNumeric &operator%=(Src rhs);

    CheckedNumeric operator-() const
    {
        RangeConstraint validity;
        T value = CheckedNeg(state_.value(), &validity);
        // Negation is always valid for floating point.
        if (std::numeric_limits<T>::is_iec559)
            return CheckedNumeric<T>(value);

        validity = GetRangeConstraint(state_.validity() | validity);
        return CheckedNumeric<T>(value, validity);
    }

    CheckedNumeric Abs() const
    {
        RangeConstraint validity;
        T value = CheckedAbs(state_.value(), &validity);
        // Absolute value is always valid for floating point.
        if (std::numeric_limits<T>::is_iec559)
            return CheckedNumeric<T>(value);

        validity = GetRangeConstraint(state_.validity() | validity);
        return CheckedNumeric<T>(value, validity);
    }

    // This function is available only for integral types. It returns an unsigned
    // integer of the same width as the source type, containing the absolute value
    // of the source, and properly handling signed min.
    CheckedNumeric<typename UnsignedOrFloatForSize<T>::type> UnsignedAbs() const
    {
        return CheckedNumeric<typename UnsignedOrFloatForSize<T>::type>(
            CheckedUnsignedAbs(state_.value()), state_.validity());
    }

    CheckedNumeric &operator++()
    {
        *this += 1;
        return *this;
    }

    CheckedNumeric operator++(int)
    {
        CheckedNumeric value = *this;
        *this += 1;
        return value;
    }

    CheckedNumeric &operator--()
    {
        *this -= 1;
        return *this;
    }

    CheckedNumeric operator--(int)
    {
        CheckedNumeric value = *this;
        *this -= 1;
        return value;
    }

    // These static methods behave like a convenience cast operator targeting
    // the desired CheckedNumeric type. As an optimization, a reference is
    // returned when Src is the same type as T.
    template <typename Src>
    static CheckedNumeric<T> cast(
        Src u,
        typename std::enable_if<std::numeric_limits<Src>::is_specialized, int>::type = 0)
    {
        return u;
    }

    template <typename Src>
    static CheckedNumeric<T> cast(
        const CheckedNumeric<Src> &u,
        typename std::enable_if<!std::is_same<Src, T>::value, int>::type = 0)
    {
        return u;
    }

    static const CheckedNumeric<T> &cast(const CheckedNumeric<T> &u) { return u; }

  private:
    template <typename NumericType>
    struct UnderlyingType
    {
        using type = NumericType;
    };

    template <typename NumericType>
    struct UnderlyingType<CheckedNumeric<NumericType>>
    {
        using type = NumericType;
    };

    CheckedNumericState<T> state_;
};

// This is the boilerplate for the standard arithmetic operator overloads. A
// macro isn't the prettiest solution, but it beats rewriting these five times.
// Some details worth noting are:
//  * We apply the standard arithmetic promotions.
//  * We skip range checks for floating points.
//  * We skip range checks for destination integers with sufficient range.
// TODO(jschuh): extract these out into templates.
#define ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP)                                   \
    /* Binary arithmetic operator for CheckedNumerics of the same type. */                         \
    template <typename T>                                                                          \
    CheckedNumeric<typename ArithmeticPromotion<T>::type> operator OP(                             \
        const CheckedNumeric<T> &lhs, const CheckedNumeric<T> &rhs)                                \
    {                                                                                              \
        typedef typename ArithmeticPromotion<T>::type Promotion;                                   \
        /* Floating point always takes the fast path */                                            \
        if (std::numeric_limits<T>::is_iec559)                                                     \
            return CheckedNumeric<T>(lhs.ValueUnsafe() OP rhs.ValueUnsafe());                      \
        if (IsIntegerArithmeticSafe<Promotion, T, T>::value)                                       \
            return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs.ValueUnsafe(),               \
                                             GetRangeConstraint(rhs.validity() | lhs.validity())); \
        RangeConstraint validity = RANGE_VALID;                                                    \
        T result =                                                                                 \
            static_cast<T>(Checked##NAME(static_cast<Promotion>(lhs.ValueUnsafe()),                \
                                         static_cast<Promotion>(rhs.ValueUnsafe()), &validity));   \
        return CheckedNumeric<Promotion>(                                                          \
            result, GetRangeConstraint(validity | lhs.validity() | rhs.validity()));               \
    }                                                                                              \
    /* Assignment arithmetic operator implementation from CheckedNumeric. */                       \
    template <typename T>                                                                          \
    template <typename Src>                                                                        \
    CheckedNumeric<T> &CheckedNumeric<T>::operator COMPOUND_OP(Src rhs)                            \
    {                                                                                              \
        *this = CheckedNumeric<T>::cast(*this)                                                     \
            OP CheckedNumeric<typename UnderlyingType<Src>::type>::cast(rhs);                      \
        return *this;                                                                              \
    }                                                                                              \
    /* Binary arithmetic operator for CheckedNumeric of different type. */                         \
    template <typename T, typename Src>                                                            \
    CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP(                        \
        const CheckedNumeric<Src> &lhs, const CheckedNumeric<T> &rhs)                              \
    {                                                                                              \
        typedef typename ArithmeticPromotion<T, Src>::type Promotion;                              \
        if (IsIntegerArithmeticSafe<Promotion, T, Src>::value)                                     \
            return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs.ValueUnsafe(),               \
                                             GetRangeConstraint(rhs.validity() | lhs.validity())); \
        return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs);       \
    }                                                                                              \
    /* Binary arithmetic operator for left CheckedNumeric and right numeric. */                    \
    template <typename T, typename Src,                                                            \
              typename std::enable_if<std::is_arithmetic<Src>::value>::type * = nullptr>           \
    CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP(                        \
        const CheckedNumeric<T> &lhs, Src rhs)                                                     \
    {                                                                                              \
        typedef typename ArithmeticPromotion<T, Src>::type Promotion;                              \
        if (IsIntegerArithmeticSafe<Promotion, T, Src>::value)                                     \
            return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs, lhs.validity());            \
        return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs);       \
    }                                                                                              \
    /* Binary arithmetic operator for left numeric and right CheckedNumeric. */                    \
    template <typename T, typename Src,                                                            \
              typename std::enable_if<std::is_arithmetic<Src>::value>::type * = nullptr>           \
    CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP(                        \
        Src lhs, const CheckedNumeric<T> &rhs)                                                     \
    {                                                                                              \
        typedef typename ArithmeticPromotion<T, Src>::type Promotion;                              \
        if (IsIntegerArithmeticSafe<Promotion, T, Src>::value)                                     \
            return CheckedNumeric<Promotion>(lhs OP rhs.ValueUnsafe(), rhs.validity());            \
        return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs);       \
    }

ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=)
ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=)
ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=)
ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=)
ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=)

#undef ANGLEBASE_NUMERIC_ARITHMETIC_OPERATORS

}  // namespace internal

using internal::CheckedNumeric;

}  // namespace base

}  // namespace angle

#endif  // ANGLEBASE_NUMERICS_SAFE_MATH_H_