aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/root/checkedint.c
blob: ee107393eda3afa28e9632c37df7a8e42d57e308 (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

/**********************************************
 * This module implements integral arithmetic primitives that check
 * for out-of-range results.
 * This is a translation to C++ of D's core.checkedint
 *
 * Integral arithmetic operators operate on fixed width types.
 * Results that are not representable in those fixed widths are silently
 * truncated to fit.
 * This module offers integral arithmetic primitives that produce the
 * same results, but set an 'overflow' flag when such truncation occurs.
 * The setting is sticky, meaning that numerous operations can be cascaded
 * and then the flag need only be checked at the end.
 * Whether the operation is signed or unsigned is indicated by an 's' or 'u'
 * suffix, respectively. While this could be achieved without such suffixes by
 * using overloading on the signedness of the types, the suffix makes it clear
 * which is happening without needing to examine the types.
 *
 * While the generic versions of these functions are computationally expensive
 * relative to the cost of the operation itself, compiler implementations are free
 * to recognize them and generate equivalent and faster code.
 *
 * References: $(LINK2 http://blog.regehr.org/archives/1139, Fast Integer Overflow Checks)
 * Copyright: Copyright (C) 2014-2020 by The D Language Foundation, All Rights Reserved
 * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
 * Authors:   Walter Bright
 * Source:    https://github.com/D-Programming-Language/dmd/blob/master/src/root/checkedint.c
 */

#include "dsystem.h"
#include "checkedint.h"


/*******************************
 * Add two signed integers, checking for overflow.
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

int adds(int x, int y, bool& overflow)
{
    int64_t r = (int64_t)x + (int64_t)y;
    if (r < INT32_MIN || r > INT32_MAX)
        overflow = true;
    return (int)r;
}

/// ditto
int64_t adds(int64_t x, int64_t y, bool& overflow)
{
    int64_t r = (uint64_t)x + (uint64_t)y;
    if ((x <  0 && y <  0 && r >= 0) ||
        (x >= 0 && y >= 0 && r <  0))
        overflow = true;
    return r;
}

/*******************************
 * Add two unsigned integers, checking for overflow (aka carry).
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

unsigned addu(unsigned x, unsigned y, bool& overflow)
{
    unsigned r = x + y;
    if (r < x || r < y)
        overflow = true;
    return r;
}

/// ditto
uint64_t addu(uint64_t x, uint64_t y, bool& overflow)
{
    uint64_t r = x + y;
    if (r < x || r < y)
        overflow = true;
    return r;
}

/*******************************
 * Subtract two signed integers, checking for overflow.
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

int subs(int x, int y, bool& overflow)
{
    int64_t r = (int64_t)x - (int64_t)y;
    if (r < INT32_MIN || r > INT32_MAX)
        overflow = true;
    return (int)r;
}

/// ditto
int64_t subs(int64_t x, int64_t y, bool& overflow)
{
    int64_t r = (uint64_t)x - (uint64_t)y;
    if ((x <  0 && y >= 0 && r >= 0) ||
        (x >= 0 && y <  0 && (r <  0 || y == INT64_MIN)))
        overflow = true;
    return r;
}

/*******************************
 * Subtract two unsigned integers, checking for overflow (aka borrow).
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

unsigned subu(unsigned x, unsigned y, bool& overflow)
{
    if (x < y)
        overflow = true;
    return x - y;
}

/// ditto
uint64_t subu(uint64_t x, uint64_t y, bool& overflow)
{
    if (x < y)
        overflow = true;
    return x - y;
}

/***********************************************
 * Negate an integer.
 *
 * Params:
 *      x = operand
 *      overflow = set if x cannot be negated, is not affected otherwise
 * Returns:
 *      the negation of x
 */

int negs(int x, bool& overflow)
{
    if (x == (int)INT32_MIN)
        overflow = true;
    return -x;
}

/// ditto
int64_t negs(int64_t x, bool& overflow)
{
    if (x == INT64_MIN)
        overflow = true;
    return -x;
}

/*******************************
 * Multiply two signed integers, checking for overflow.
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

int muls(int x, int y, bool& overflow)
{
    int64_t r = (int64_t)x * (int64_t)y;
    if (r < INT32_MIN || r > INT32_MAX)
        overflow = true;
    return (int)r;
}

/// ditto
int64_t muls(int64_t x, int64_t y, bool& overflow)
{
    int64_t r = (uint64_t)x * (uint64_t)y;
    int64_t not0or1 = ~(int64_t)1;
    if ((x & not0or1) && ((r == y) ? r : (r / x) != y))
        overflow = true;
    return r;
}

/*******************************
 * Multiply two unsigned integers, checking for overflow (aka carry).
 *
 * The overflow is sticky, meaning a sequence of operations can
 * be done and overflow need only be checked at the end.
 * Params:
 *      x = left operand
 *      y = right operand
 *      overflow = set if an overflow occurs, is not affected otherwise
 * Returns:
 *      the sum
 */

unsigned mulu(unsigned x, unsigned y, bool& overflow)
{
    uint64_t r = (uint64_t)x * (uint64_t)y;
    if (r > UINT32_MAX)
        overflow = true;
    return (unsigned)r;
}

/// ditto
uint64_t mulu(uint64_t x, uint64_t y, bool& overflow)
{
    uint64_t r = x * y;
    if (x && (r / x) != y)
        overflow = true;
    return r;
}