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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Assertions.h"
#include "mozilla/WrappingOperations.h"
#include <stdint.h>
using mozilla::WrappingAdd;
using mozilla::WrappingMultiply;
using mozilla::WrappingSubtract;
using mozilla::WrapToSigned;
// NOTE: In places below |-FOO_MAX - 1| is used instead of |-FOO_MIN| because
// in C++ numeric literals are full expressions -- the |-| in a negative
// number is technically separate. So with most compilers that limit
// |int| to the signed 32-bit range, something like |-2147483648| is
// operator-() applied to an *unsigned* expression. And MSVC, at least,
// warns when you do that. (The operation is well-defined, but it likely
// doesn't do what was intended.) So we do the usual workaround for this
// (see your local copy of <stdint.h> for a likely demo of this), writing
// it out by negating the max value and subtracting 1.
static_assert(WrapToSigned(uint8_t(17)) == 17,
"no wraparound should work, 8-bit");
static_assert(WrapToSigned(uint8_t(128)) == -128,
"works for 8-bit numbers, wraparound low end");
static_assert(WrapToSigned(uint8_t(128 + 7)) == -128 + 7,
"works for 8-bit numbers, wraparound mid");
static_assert(WrapToSigned(uint8_t(128 + 127)) == -128 + 127,
"works for 8-bit numbers, wraparound high end");
static_assert(WrapToSigned(uint16_t(12345)) == 12345,
"no wraparound should work, 16-bit");
static_assert(WrapToSigned(uint16_t(32768)) == -32768,
"works for 16-bit numbers, wraparound low end");
static_assert(WrapToSigned(uint16_t(32768 + 42)) == -32768 + 42,
"works for 16-bit numbers, wraparound mid");
static_assert(WrapToSigned(uint16_t(32768 + 32767)) == -32768 + 32767,
"works for 16-bit numbers, wraparound high end");
static_assert(WrapToSigned(uint32_t(8675309)) == 8675309,
"no wraparound should work, 32-bit");
static_assert(WrapToSigned(uint32_t(2147483648)) == -2147483647 - 1,
"works for 32-bit numbers, wraparound low end");
static_assert(WrapToSigned(uint32_t(2147483648 + 42)) == -2147483647 - 1 + 42,
"works for 32-bit numbers, wraparound mid");
static_assert(WrapToSigned(uint32_t(2147483648 + 2147483647)) ==
-2147483647 - 1 + 2147483647,
"works for 32-bit numbers, wraparound high end");
static_assert(WrapToSigned(uint64_t(4152739164)) == 4152739164,
"no wraparound should work, 64-bit");
static_assert(WrapToSigned(uint64_t(9223372036854775808ULL)) ==
-9223372036854775807LL - 1,
"works for 64-bit numbers, wraparound low end");
static_assert(WrapToSigned(uint64_t(9223372036854775808ULL + 8005552368LL)) ==
-9223372036854775807LL - 1 + 8005552368LL,
"works for 64-bit numbers, wraparound mid");
static_assert(WrapToSigned(uint64_t(9223372036854775808ULL +
9223372036854775807ULL)) ==
-9223372036854775807LL - 1 + 9223372036854775807LL,
"works for 64-bit numbers, wraparound high end");
template <typename T>
inline constexpr bool TestEqual(T aX, T aY) {
return aX == aY;
}
static void TestWrappingAdd8() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint8_t(0), uint8_t(128)), uint8_t(128)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint8_t(17), uint8_t(42)), uint8_t(59)),
"17 + 42 == 59");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint8_t(255), uint8_t(1)), uint8_t(0)),
"all bits plus one overflows to zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint8_t(128), uint8_t(127)), uint8_t(255)),
"high bit plus all lower bits is all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint8_t(128), uint8_t(193)), uint8_t(65)),
"128 + 193 is 256 + 65");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int8_t(0), int8_t(-128)), int8_t(-128)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int8_t(123), int8_t(8)), int8_t(-125)),
"overflow to negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int8_t(5), int8_t(-123)), int8_t(-118)),
"5 - 123 is -118");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int8_t(-85), int8_t(-73)), int8_t(98)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int8_t(-128), int8_t(127)), int8_t(-1)),
"high bit plus all lower bits is -1");
}
static void TestWrappingAdd16() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint16_t(0), uint16_t(32768)), uint16_t(32768)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint16_t(24389), uint16_t(2682)), uint16_t(27071)),
"24389 + 2682 == 27071");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint16_t(65535), uint16_t(1)), uint16_t(0)),
"all bits plus one overflows to zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint16_t(32768), uint16_t(32767)), uint16_t(65535)),
"high bit plus all lower bits is all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint16_t(32768), uint16_t(47582)), uint16_t(14814)),
"32768 + 47582 is 65536 + 14814");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int16_t(0), int16_t(-32768)), int16_t(-32768)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int16_t(32765), int16_t(8)), int16_t(-32763)),
"overflow to negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int16_t(5), int16_t(-28933)), int16_t(-28928)),
"5 - 28933 is -28928");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int16_t(-23892), int16_t(-12893)), int16_t(28751)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int16_t(-32768), int16_t(32767)), int16_t(-1)),
"high bit plus all lower bits is -1");
}
static void TestWrappingAdd32() {
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(uint32_t(0), uint32_t(2147483648)),
uint32_t(2147483648)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint32_t(1398742328), uint32_t(714192829)),
uint32_t(2112935157)),
"1398742328 + 714192829 == 2112935157");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint32_t(4294967295), uint32_t(1)), uint32_t(0)),
"all bits plus one overflows to zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint32_t(2147483648), uint32_t(2147483647)),
uint32_t(4294967295)),
"high bit plus all lower bits is all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint32_t(2147483648), uint32_t(3146492712)),
uint32_t(999009064)),
"2147483648 + 3146492712 is 4294967296 + 999009064");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int32_t(0), int32_t(-2147483647 - 1)),
int32_t(-2147483647 - 1)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(int32_t(2147483645), int32_t(8)),
int32_t(-2147483643)),
"overflow to negative");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(int32_t(257), int32_t(-23947248)),
int32_t(-23946991)),
"257 - 23947248 is -23946991");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int32_t(-2147483220), int32_t(-12893)),
int32_t(2147471183)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int32_t(-32768), int32_t(32767)), int32_t(-1)),
"high bit plus all lower bits is -1");
}
static void TestWrappingAdd64() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint64_t(0), uint64_t(9223372036854775808ULL)),
uint64_t(9223372036854775808ULL)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint64_t(70368744177664), uint64_t(3740873592)),
uint64_t(70372485051256)),
"70368744177664 + 3740873592 == 70372485051256");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(uint64_t(18446744073709551615ULL), uint64_t(1)),
uint64_t(0)),
"all bits plus one overflows to zero");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(uint64_t(9223372036854775808ULL),
uint64_t(9223372036854775807ULL)),
uint64_t(18446744073709551615ULL)),
"high bit plus all lower bits is all bits");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(uint64_t(14552598638644786479ULL),
uint64_t(3894174382537247221ULL)),
uint64_t(28947472482084)),
"9223372036854775808 + 3146492712 is 18446744073709551616 "
"+ 28947472482084");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int64_t(0), int64_t(-9223372036854775807LL - 1)),
int64_t(-9223372036854775807LL - 1)),
"zero plus anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int64_t(9223372036854775802LL), int64_t(8)),
int64_t(-9223372036854775806LL)),
"overflow to negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingAdd(int64_t(37482739294298742LL),
int64_t(-437843573929483498LL)),
int64_t(-400360834635184756LL)),
"37482739294298742 - 437843573929483498 is -400360834635184756");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(int64_t(-9127837934058953374LL),
int64_t(-4173572032144775807LL)),
int64_t(5145334107505822435L)),
"underflow to positive");
MOZ_RELEASE_ASSERT(TestEqual(WrappingAdd(int64_t(-9223372036854775807LL - 1),
int64_t(9223372036854775807LL)),
int64_t(-1)),
"high bit plus all lower bits is -1");
}
static void TestWrappingAdd() {
TestWrappingAdd8();
TestWrappingAdd16();
TestWrappingAdd32();
TestWrappingAdd64();
}
static void TestWrappingSubtract8() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint8_t(0), uint8_t(128)), uint8_t(128)),
"zero minus half is half");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint8_t(17), uint8_t(42)), uint8_t(231)),
"17 - 42 == -25 added to 256 is 231");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint8_t(0), uint8_t(1)), uint8_t(255)),
"zero underflows to all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint8_t(128), uint8_t(127)), uint8_t(1)),
"128 - 127 == 1");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint8_t(128), uint8_t(193)), uint8_t(191)),
"128 - 193 is -65 so -65 + 256 == 191");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int8_t(0), int8_t(-128)), int8_t(-128)),
"zero minus high bit wraps to high bit");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int8_t(-126), int8_t(4)), int8_t(126)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int8_t(5), int8_t(-123)), int8_t(-128)),
"overflow to negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int8_t(-85), int8_t(-73)), int8_t(-12)),
"negative minus smaller negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int8_t(-128), int8_t(127)), int8_t(1)),
"underflow to 1");
}
static void TestWrappingSubtract16() {
MOZ_RELEASE_ASSERT(TestEqual(WrappingSubtract(uint16_t(0), uint16_t(32768)),
uint16_t(32768)),
"zero minus half is half");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint16_t(24389), uint16_t(2682)),
uint16_t(21707)),
"24389 - 2682 == 21707");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint16_t(0), uint16_t(1)), uint16_t(65535)),
"zero underflows to all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint16_t(32768), uint16_t(32767)),
uint16_t(1)),
"high bit minus all lower bits is one");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint16_t(32768), uint16_t(47582)),
uint16_t(50722)),
"32768 - 47582 + 65536 is 50722");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int16_t(0), int16_t(-32768)), int16_t(-32768)),
"zero minus high bit wraps to high bit");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int16_t(-32766), int16_t(4)), int16_t(32766)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int16_t(5), int16_t(-28933)), int16_t(28938)),
"5 - -28933 is 28938");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int16_t(-23892), int16_t(-12893)),
int16_t(-10999)),
"negative minus smaller negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int16_t(-32768), int16_t(32767)), int16_t(1)),
"underflow to 1");
}
static void TestWrappingSubtract32() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint32_t(0), uint32_t(2147483648)),
uint32_t(2147483648)),
"zero minus half is half");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint32_t(1398742328), uint32_t(714192829)),
uint32_t(684549499)),
"1398742328 - 714192829 == 684549499");
MOZ_RELEASE_ASSERT(TestEqual(WrappingSubtract(uint32_t(0), uint32_t(1)),
uint32_t(4294967295)),
"zero underflows to all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint32_t(2147483648), uint32_t(2147483647)),
uint32_t(1)),
"high bit minus all lower bits is one");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint32_t(2147483648), uint32_t(3146492712)),
uint32_t(3295958232)),
"2147483648 - 3146492712 + 4294967296 is 3295958232");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int32_t(0), int32_t(-2147483647 - 1)),
int32_t(-2147483647 - 1)),
"zero minus high bit wraps to high bit");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int32_t(-2147483646), int32_t(4)),
int32_t(2147483646)),
"underflow to positive");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int32_t(257), int32_t(-23947248)),
int32_t(23947505)),
"257 - -23947248 is 23947505");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int32_t(-2147483220), int32_t(-12893)),
int32_t(-2147470327)),
"negative minus smaller negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int32_t(-2147483647 - 1), int32_t(2147483647)),
int32_t(1)),
"underflow to 1");
}
static void TestWrappingSubtract64() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint64_t(0), uint64_t(9223372036854775808ULL)),
uint64_t(9223372036854775808ULL)),
"zero minus half is half");
MOZ_RELEASE_ASSERT(TestEqual(WrappingSubtract(uint64_t(70368744177664),
uint64_t(3740873592)),
uint64_t(70365003304072)),
"70368744177664 - 3740873592 == 70365003304072");
MOZ_RELEASE_ASSERT(TestEqual(WrappingSubtract(uint64_t(0), uint64_t(1)),
uint64_t(18446744073709551615ULL)),
"zero underflows to all bits");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint64_t(9223372036854775808ULL),
uint64_t(9223372036854775807ULL)),
uint64_t(1)),
"high bit minus all lower bits is one");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(uint64_t(14552598638644786479ULL),
uint64_t(3894174382537247221ULL)),
uint64_t(10658424256107539258ULL)),
"14552598638644786479 - 39763621533397112216 is 10658424256107539258L");
MOZ_RELEASE_ASSERT(
TestEqual(
WrappingSubtract(int64_t(0), int64_t(-9223372036854775807LL - 1)),
int64_t(-9223372036854775807LL - 1)),
"zero minus high bit wraps to high bit");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int64_t(-9223372036854775802LL), int64_t(8)),
int64_t(9223372036854775806LL)),
"overflow to negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int64_t(37482739294298742LL),
int64_t(-437843573929483498LL)),
int64_t(475326313223782240)),
"37482739294298742 - -437843573929483498 is 475326313223782240");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int64_t(-9127837934058953374LL),
int64_t(-4173572032144775807LL)),
int64_t(-4954265901914177567LL)),
"negative minus smaller negative");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingSubtract(int64_t(-9223372036854775807LL - 1),
int64_t(9223372036854775807LL)),
int64_t(1)),
"underflow to 1");
}
static void TestWrappingSubtract() {
TestWrappingSubtract8();
TestWrappingSubtract16();
TestWrappingSubtract32();
TestWrappingSubtract64();
}
static void TestWrappingMultiply8() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint8_t(0), uint8_t(128)), uint8_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint8_t(128), uint8_t(1)), uint8_t(128)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint8_t(2), uint8_t(128)), uint8_t(0)),
"2 times high bit overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint8_t(8), uint8_t(16)), uint8_t(128)),
"multiply that populates the high bit produces that value");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint8_t(127), uint8_t(127)), uint8_t(1)),
"multiplying signed maxvals overflows all the way to 1");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(0), int8_t(-128)), int8_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(-128), int8_t(1)), int8_t(-128)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(2), int8_t(-128)), int8_t(0)),
"2 times min overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(16), int8_t(24)), int8_t(-128)),
"multiply that populates the sign bit produces minval");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(8), int8_t(16)), int8_t(-128)),
"multiply that populates the sign bit produces minval");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int8_t(127), int8_t(127)), int8_t(1)),
"multiplying maxvals overflows all the way to 1");
}
static void TestWrappingMultiply16() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint16_t(0), uint16_t(32768)), uint16_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(TestEqual(WrappingMultiply(uint16_t(32768), uint16_t(1)),
uint16_t(32768)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint16_t(2), uint16_t(32768)), uint16_t(0)),
"2 times high bit overflows, produces zero");
MOZ_RELEASE_ASSERT(TestEqual(WrappingMultiply(uint16_t(3), uint16_t(32768)),
uint16_t(-32768)),
"3 * 32768 - 65536 is 32768");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint16_t(64), uint16_t(512)), uint16_t(32768)),
"multiply that populates the high bit produces that value");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint16_t(32767), uint16_t(32767)),
uint16_t(1)),
"multiplying signed maxvals overflows all the way to 1");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(0), int16_t(-32768)), int16_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(-32768), int16_t(1)), int16_t(-32768)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(-456), int16_t(123)), int16_t(9448)),
"multiply opposite signs, then add 2**16 for the result");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(2), int16_t(-32768)), int16_t(0)),
"2 times min overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(64), int16_t(512)), int16_t(-32768)),
"multiply that populates the sign bit produces minval");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int16_t(32767), int16_t(32767)), int16_t(1)),
"multiplying maxvals overflows all the way to 1");
}
static void TestWrappingMultiply32() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(0), uint32_t(2147483648)),
uint32_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(42), uint32_t(17)), uint32_t(714)),
"42 * 17 is 714 without wraparound");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(2147483648), uint32_t(1)),
uint32_t(2147483648)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(2), uint32_t(2147483648)),
uint32_t(0)),
"2 times high bit overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(8192), uint32_t(262144)),
uint32_t(2147483648)),
"multiply that populates the high bit produces that value");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint32_t(2147483647), uint32_t(2147483647)),
uint32_t(1)),
"multiplying signed maxvals overflows all the way to 1");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int32_t(0), int32_t(-2147483647 - 1)),
int32_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int32_t(-2147483647 - 1), int32_t(1)),
int32_t(-2147483647 - 1)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int32_t(2), int32_t(-2147483647 - 1)),
int32_t(0)),
"2 times min overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int32_t(-7), int32_t(-9)), int32_t(63)),
"-7 * -9 is 63, no wraparound needed");
MOZ_RELEASE_ASSERT(TestEqual(WrappingMultiply(int32_t(8192), int32_t(262144)),
int32_t(-2147483647 - 1)),
"multiply that populates the sign bit produces minval");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int32_t(2147483647), int32_t(2147483647)),
int32_t(1)),
"multiplying maxvals overflows all the way to 1");
}
static void TestWrappingMultiply64() {
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint64_t(0), uint64_t(9223372036854775808ULL)),
uint64_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint64_t(9223372036854775808ULL), uint64_t(1)),
uint64_t(9223372036854775808ULL)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint64_t(2), uint64_t(9223372036854775808ULL)),
uint64_t(0)),
"2 times high bit overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(uint64_t(131072), uint64_t(70368744177664)),
uint64_t(9223372036854775808ULL)),
"multiply that populates the high bit produces that value");
MOZ_RELEASE_ASSERT(TestEqual(WrappingMultiply(uint64_t(9223372036854775807),
uint64_t(9223372036854775807)),
uint64_t(1)),
"multiplying signed maxvals overflows all the way to 1");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int64_t(0), int64_t(-9223372036854775807 - 1)),
int64_t(0)),
"zero times anything is zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int64_t(-9223372036854775807 - 1), int64_t(1)),
int64_t(-9223372036854775807 - 1)),
"1 times anything is anything");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int64_t(2), int64_t(-9223372036854775807 - 1)),
int64_t(0)),
"2 times min overflows, produces zero");
MOZ_RELEASE_ASSERT(
TestEqual(WrappingMultiply(int64_t(131072), int64_t(70368744177664)),
int64_t(-9223372036854775807 - 1)),
"multiply that populates the sign bit produces minval");
MOZ_RELEASE_ASSERT(TestEqual(WrappingMultiply(int64_t(9223372036854775807),
int64_t(9223372036854775807)),
int64_t(1)),
"multiplying maxvals overflows all the way to 1");
}
static void TestWrappingMultiply() {
TestWrappingMultiply8();
TestWrappingMultiply16();
TestWrappingMultiply32();
TestWrappingMultiply64();
}
int main() {
TestWrappingAdd();
TestWrappingSubtract();
TestWrappingMultiply();
return 0;
}
|