2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/test.h>
21 #include <grub/misc.h>
23 GRUB_MOD_LICENSE ("GPLv3+");
25 static grub_uint64_t vectors
[][2] = {
26 { 0xffffffffffffffffULL
, 1},
27 { 1, 0xffffffffffffffffULL
},
28 { 0xffffffffffffffffULL
, 0xffffffffffffffffULL
},
34 test32 (grub_uint32_t a
, grub_uint32_t b
)
37 q
= grub_divmod64 (a
, b
, &r
);
38 grub_test_assert (r
< b
, "remainder is larger than dividend: 0x%llx %% 0x%llx = 0x%llx",
39 (long long) a
, (long long) b
, (long long) r
);
40 grub_test_assert (q
* b
+ r
== a
, "division doesn't satisfy base property: 0x%llx * 0x%llx + 0x%llx != 0x%llx", (long long) q
, (long long) b
, (long long) r
,
43 grub_test_assert ((q
>> 32) == 0,
44 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
45 grub_test_assert ((r
>> 32) == 0,
46 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
47 /* q * b + r is at most:
48 0xffffffff * 0xffffffff + 0xffffffff = 0xffffffff00000000
51 grub_test_assert (q
== (a
/ b
),
52 "C compiler division failure in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
53 grub_test_assert (r
== (a
% b
),
54 "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
58 test64 (grub_uint64_t a
, grub_uint64_t b
)
62 q
= grub_divmod64 (a
, b
, &r
);
63 grub_test_assert (r
< b
, "remainder is larger than dividend: 0x%llx %% 0x%llx = 0x%llx",
64 (long long) a
, (long long) b
, (long long) r
);
65 grub_test_assert (q
* b
+ r
== a
, "division doesn't satisfy base property: 0x%llx * 0x%llx + 0x%llx != 0x%llx", (long long) q
, (long long) b
, (long long) r
,
67 /* Overflow checks. */
68 grub_test_assert ((q
>> 32) * (b
>> 32) == 0,
69 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
70 x1
= (q
>> 32) * (b
& 0xffffffff);
71 grub_test_assert (x1
< (1LL << 32),
72 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
74 x2
= (b
>> 32) * (q
& 0xffffffff);
75 grub_test_assert (x2
< (1LL << 32),
76 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
78 grub_test_assert (x1
<= ~x2
,
79 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
81 x2
= (q
& 0xffffffff) * (b
& 0xffffffff);
82 grub_test_assert (x1
<= ~x2
,
83 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
85 grub_test_assert (x1
<= ~r
,
86 "division overflow in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
88 grub_test_assert (a
== x1
,
89 "division overflow test failure in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
90 #if GRUB_TARGET_SIZEOF_VOID_P == 8
91 grub_test_assert (q
== (a
/ b
),
92 "C compiler division failure in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
93 grub_test_assert (r
== (a
% b
),
94 "C compiler modulo failure in 0x%llx, 0x%llx", (long long) a
, (long long) b
);
101 grub_uint64_t a
= 404, b
= 7;
104 for (i
= 0; i
< ARRAY_SIZE (vectors
); i
++)
106 test64 (vectors
[i
][0], vectors
[i
][1]);
107 test32 (vectors
[i
][0], vectors
[i
][1]);
109 for (i
= 0; i
< 40000; i
++)
123 /* Register example_test method as a functional test. */
124 GRUB_FUNCTIONAL_TEST (div_test
, div_test
);