2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2015 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
[] = {
26 0xffffffffffffffffULL
, 1, 2, 0, 0x0102030405060708ULL
29 /* We're testing shifts, don't replace access to this with a shift. */
30 static const grub_uint8_t bitmask
[] =
31 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
39 get_bit64 (grub_uint64_t v
, int b
)
41 grub_raw_u64_t vr
= { .v64
= v
};
42 grub_uint8_t
*p
= vr
.v8
;
45 #ifdef GRUB_CPU_WORDS_BIGENDIAN
50 return !!(*p
& bitmask
[b
% 8]);
54 set_bit64 (grub_uint64_t v
, int b
)
56 grub_raw_u64_t vr
= { .v64
= v
};
57 grub_uint8_t
*p
= vr
.v8
;
60 #ifdef GRUB_CPU_WORDS_BIGENDIAN
70 left_shift64 (grub_uint64_t v
, int s
)
74 for (i
= 0; i
+ s
< 64; i
++)
76 r
= set_bit64 (r
, i
+ s
);
81 right_shift64 (grub_uint64_t v
, int s
)
85 for (i
= s
; i
< 64; i
++)
87 r
= set_bit64 (r
, i
- s
);
92 arithmetic_right_shift64 (grub_uint64_t v
, int s
)
96 for (i
= s
; i
< 64; i
++)
98 r
= set_bit64 (r
, i
- s
);
99 if (get_bit64 (v
, 63))
100 for (i
-= s
; i
< 64; i
++)
101 r
= set_bit64 (r
, i
);
107 test64 (grub_uint64_t v
)
110 for (i
= 0; i
< 64; i
++)
112 grub_test_assert ((v
<< i
) == left_shift64 (v
, i
),
113 "lshift wrong: 0x%llx << %d: 0x%llx, 0x%llx",
115 (long long) (v
<< i
), (long long) left_shift64 (v
, i
));
116 grub_test_assert ((v
>> i
) == right_shift64 (v
, i
),
117 "rshift wrong: 0x%llx >> %d: 0x%llx, 0x%llx",
119 (long long) (v
>> i
), (long long) right_shift64 (v
, i
));
120 grub_test_assert ((((grub_int64_t
) v
) >> i
) == (grub_int64_t
) arithmetic_right_shift64 (v
, i
),
121 "arithmetic rshift wrong: ((grub_int64_t) 0x%llx) >> %d: 0x%llx, 0x%llx",
123 (long long) (((grub_int64_t
) v
) >> i
), (long long) arithmetic_right_shift64 (v
, i
));
128 test_all(grub_uint64_t a
)
136 grub_uint64_t a
= 404, b
= 7;
139 for (i
= 0; i
< ARRAY_SIZE (vectors
); i
++)
141 test_all (vectors
[i
]);
143 for (i
= 0; i
< 4000; i
++)
156 /* Register example_test method as a functional test. */
157 GRUB_FUNCTIONAL_TEST (shift_test
, shift_test
);