-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite.go
441 lines (330 loc) · 10.5 KB
/
composite.go
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package safe
import (
"github.com/akramarenkov/safe/internal/is"
"github.com/akramarenkov/intspec"
"golang.org/x/exp/constraints"
)
// Calculates the value of the expression first + second - subtrahend and detects
// whether an overflow has occurred or not.
//
// In case of overflow, an error is returned.
func AddSub[Type constraints.Integer](first, second, subtrahend Type) (Type, error) {
if interim, err := Add(first, second); err == nil {
return Sub(interim, subtrahend)
}
if interim, err := Sub(first, subtrahend); err == nil {
if sum, err := Add(interim, second); err == nil {
return sum, nil
}
}
if interim, err := Sub(second, subtrahend); err == nil {
if sum, err := Add(interim, first); err == nil {
return sum, nil
}
}
interim, err := Sub(subtrahend, first)
if err != nil {
return 0, err
}
return Sub(second, interim)
}
// Calculates the quotient of dividing the sum of two integers by divisor and
// detects whether an overflow has occurred or not.
//
// In case of overflow or divisor equal to zero, an error is returned.
func AddDiv[Type constraints.Integer](first, second, divisor Type) (Type, error) {
if sum, err := Add(first, second); err == nil {
return Div(sum, divisor)
}
if divisor == 0 {
return 0, ErrDivisionByZero
}
// If overflow occurs during addition, the addition arguments have the same signs
minimum, maximum := intspec.Range[Type]()
overflowed := first + second
// The idea is to complement one of the arguments being added to the maximum
// (if the arguments are positive) or minimum (if the arguments are negative)
// values for a given type so that the division result is never zero. Then divide
// separately the maximum/minimum value and the excess that arose during overflow,
// add the quotients from these divisions and add the quotient from dividing the
// sum of the remainders from the two previous divisions
// Since the arguments of addition have the same signs, it is sufficient to check
// either of them
if first > 0 {
// If the divisor is equal to the minimum (negative) value for a given type,
// then the approach of complementing one of the arguments to the maximum
// (positive) value stops working because the minimum value is greater than
// the maximum in absolute value and the division results in zero, and the
// remainder is equal to the dividend
if is.Min(divisor) {
// If the divisor is equal to the minimum (negative) value for the given
// type, then when dividing the sum of two positive arguments (in case of
// overflow during addition), the result is always -1
return -Type(1), nil
}
excess := -(minimum - overflowed) + 1
qm := maximum / divisor
rm := maximum % divisor
qe := excess / divisor
re := excess % divisor
interim, err := Add(qm, qe)
if err != nil {
return 0, err
}
quotient := interim + (rm+re)/divisor
return quotient, nil
}
excess := -(maximum - overflowed + 1)
qm, err := Div(minimum, divisor)
if err != nil {
return 0, err
}
rm := minimum % divisor
qe := excess / divisor
re := excess % divisor
interim, err := Add(qm, qe)
if err != nil {
return 0, err
}
quotient := interim + (rm+re)/divisor
return quotient, nil
}
// Calculates the remainder of dividing the sum of two integers by divisor.
//
// In case of divisor equal to zero, an error is returned.
func AddDivRem[Type constraints.Integer](first, second, divisor Type) (Type, error) {
if divisor == 0 {
return 0, ErrDivisionByZero
}
if sum, err := Add(first, second); err == nil {
return sum % divisor, nil
}
minimum, maximum := intspec.Range[Type]()
overflowed := first + second
if first > 0 {
if is.Min(divisor) {
return overflowed - divisor, nil
}
excess := -(minimum - overflowed) + 1
rm := maximum % divisor
re := excess % divisor
remainder := (rm + re) % divisor
return remainder, nil
}
excess := -(maximum - overflowed + 1)
rm := minimum % divisor
re := excess % divisor
remainder := (rm + re) % divisor
return remainder, nil
}
// Calculates the quotient of dividing the sum of two unsigned integers by divisor and
// detects whether an overflow has occurred or not.
//
// Not faster than the [AddDiv] function.
//
// In case of overflow or divisor equal to zero, an error is returned.
func AddDivU[Type constraints.Unsigned](first, second, divisor Type) (Type, error) {
if divisor == 0 {
return 0, ErrDivisionByZero
}
if sum, err := AddU(first, second); err == nil {
return sum / divisor, nil
}
overflowed := first + second
excess := overflowed + 1
// For unsigned types, the maximum value for the type in case of overflow can be
// calculated
//
// complement := second - excess
// maximum := first + complement
maximum := ^Type(0)
qm := maximum / divisor
rm := maximum % divisor
qe := excess / divisor
re := excess % divisor
interim, err := AddU(qm, qe)
if err != nil {
return 0, err
}
quotient := interim + (rm+re)/divisor
return quotient, nil
}
// Calculates the quotient of dividing the difference of two integers by divisor and
// detects whether an overflow has occurred or not.
//
// In case of overflow or divisor equal to zero, an error is returned.
func SubDiv[Type constraints.Integer](minuend, subtrahend, divisor Type) (Type, error) {
if diff, err := Sub(minuend, subtrahend); err == nil {
return Div(diff, divisor)
}
if divisor == 0 {
return 0, ErrDivisionByZero
}
// If an overflow occurs during subtraction and the subtraction arguments are
// signed, then they have different signs
// When subtracting unsigned arguments, overflow cannot be compensated by
// division because the result of the subtraction and subsequent division must be
// negative. Except for one case when the divisor is greater than the difference and
// the division result is zero
if !is.Signed[Type]() {
excess := subtrahend - minuend
if excess/divisor == 0 {
return 0, nil
}
return 0, ErrOverflow
}
minimum, maximum := intspec.Range[Type]()
overflowed := minuend - subtrahend
if subtrahend > 0 {
excess := -(maximum - overflowed + 1)
qm, err := Div(minimum, divisor)
if err != nil {
return 0, err
}
rm := minimum % divisor
qe := excess / divisor
re := excess % divisor
interim, err := Add(qm, qe)
if err != nil {
return 0, err
}
quotient := interim + (rm+re)/divisor
return quotient, nil
}
if is.Min(divisor) {
return -Type(1), nil
}
// Overflow is possible in these calculations, but only in one case: if
// minuend is equal to the maximum value for the given type, and subtrahend is
// equal to the minimum value for the given type. In this case, the result of
// the calculation is equal to the minimum value for the given type. This value
// will be used in the division, and the result of the division will be inverted
excess := -(minimum - overflowed) + 1
qm := maximum / divisor
rm := maximum % divisor
qe := excess / divisor
re := excess % divisor
// Excess is overflowed, sign inversion performed
if excess < 0 {
negated, err := Negate(qe)
if err != nil {
return 0, err
}
qe = negated
re = -re
}
interim, err := Add(qm, qe)
if err != nil {
return 0, err
}
quotient := interim + (rm+re)/divisor
return quotient, nil
}
// Calculates the remainder of dividing the difference of two integers by divisor and
// detects whether an overflow has occurred or not.
//
// In case of overflow or divisor equal to zero, an error is returned.
func SubDivRem[Type constraints.Integer](minuend, subtrahend, divisor Type) (Type, error) {
if divisor == 0 {
return 0, ErrDivisionByZero
}
if diff, err := Sub(minuend, subtrahend); err == nil {
return diff % divisor, nil
}
if !is.Signed[Type]() {
excess := subtrahend - minuend
if excess%divisor == 0 {
return 0, nil
}
return 0, ErrOverflow
}
minimum, maximum := intspec.Range[Type]()
overflowed := minuend - subtrahend
if subtrahend > 0 {
excess := -(maximum - overflowed + 1)
rm := minimum % divisor
re := excess % divisor
remainder := (rm + re) % divisor
return remainder, nil
}
if is.Min(divisor) {
return overflowed - divisor, nil
}
excess := -(minimum - overflowed) + 1
rm := maximum % divisor
re := excess % divisor
if excess < 0 {
re = -re
}
remainder := (rm + re) % divisor
return remainder, nil
}
// Calculates the quotient of dividing the difference of two unsigned integers by
// divisor and detects whether an overflow has occurred or not.
//
// Faster than the [SubDiv] function about 30%.
//
// In case of overflow or divisor equal to zero, an error is returned.
func SubDivU[Type constraints.Unsigned](minuend, subtrahend, divisor Type) (Type, error) {
if divisor == 0 {
return 0, ErrDivisionByZero
}
if diff, err := SubU(minuend, subtrahend); err == nil {
return diff / divisor, nil
}
excess := subtrahend - minuend
if excess/divisor == 0 {
return 0, nil
}
return 0, ErrOverflow
}
// Calculates the quotient of dividing of the expression first + second - subtrahend by
// divisor and detects whether an overflow has occurred or not.
//
// In case of overflow or divisor equal to zero, an error is returned.
func AddSubDiv[Type constraints.Integer](first, second, subtrahend, divisor Type) (Type, error) {
if interim, err := AddSub(first, second, subtrahend); err == nil {
return Div(interim, divisor)
}
if interim, err := Add(first, second); err == nil {
return SubDiv(interim, subtrahend, divisor)
}
if interim, err := Sub(subtrahend, second); err == nil {
return SubDiv(first, interim, divisor)
}
qm, err := SubDiv(first, subtrahend, divisor)
if err != nil {
return 0, err
}
rm, _ := SubDivRem(first, subtrahend, divisor)
qe, err := Div(second, divisor)
if err != nil {
return 0, err
}
re := second % divisor
remainder, _ := AddDiv(rm, re, divisor)
return Add3(qm, qe, remainder)
}
// Calculates the quotient of dividing of the expression minuend + 1 - subtrahend by
// divisor and detects whether an overflow has occurred or not.
//
// Faster than the [AddSubDiv] function about 15%.
//
// In case of overflow or divisor equal to zero, an error is returned.
func AddOneSubDiv[Type constraints.Integer](minuend, subtrahend, divisor Type) (Type, error) {
if interim, err := Add(minuend, 1); err == nil {
return SubDiv(interim, subtrahend, divisor)
}
if interim, err := Sub(subtrahend, 1); err == nil {
return SubDiv(minuend, interim, divisor)
}
qm, err := SubDiv(minuend, subtrahend, divisor)
if err != nil {
return 0, err
}
rm, _ := SubDivRem(minuend, subtrahend, divisor)
qe := 1 / divisor
re := 1 % divisor
remainder, _ := AddDiv(rm, re, divisor)
return Add3(qm, qe, remainder)
}