Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
intmath.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <script/intmath.h>
6
7#include <cstdint>
8#include <limits>
9
10#if defined(HAVE_CONFIG_H)
11#include <config/bitcoin-config.h>
12#endif
13
15 if (a > 0 && b > std::numeric_limits<int64_t>::max() - a) {
16 // integer overflow
17 return true;
18 }
19 if (a < 0 && b <= std::numeric_limits<int64_t>::min() - a) {
20 // integer underflow
21 return true;
22 }
23 result = a + b;
24 return result == std::numeric_limits<int64_t>::min();
25}
26
28#if HAVE_DECL___BUILTIN_SADDLL_OVERFLOW
29 if (__builtin_saddll_overflow(a, b, (long long int *)&result)) {
30 return true;
31 }
32 return result == std::numeric_limits<int64_t>::min();
33#else
34 return AddInt63OverflowEmulated(a, b, result);
35#endif
36}
37
39 if (a < 0 && b > std::numeric_limits<int64_t>::max() + a) {
40 // integer overflow
41 return true;
42 }
43 if (a > 0 && b <= std::numeric_limits<int64_t>::min() + a) {
44 // integer underflow
45 return true;
46 }
47 result = a - b;
48 return result == std::numeric_limits<int64_t>::min();
49}
50
52#if HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW
53 if (__builtin_ssubll_overflow(a, b, (long long int *)&result)) {
54 return true;
55 }
56 return result == std::numeric_limits<int64_t>::min();
57#else
58 return SubInt63OverflowEmulated(a, b, result);
59#endif
60}
bool AddInt63Overflow(int64_t a, int64_t b, int64_t &result)
Computes a + b and stores it in result.
Definition intmath.cpp:27
bool SubInt63OverflowEmulated(int64_t a, int64_t b, int64_t &result)
Computes a - b and stores it in result.
Definition intmath.cpp:38
bool AddInt63OverflowEmulated(int64_t a, int64_t b, int64_t &result)
Computes a + b and stores it in result.
Definition intmath.cpp:14
bool SubInt63Overflow(int64_t a, int64_t b, int64_t &result)
Computes a - b and stores it in result.
Definition intmath.cpp:51
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85