Remove libgcc dependency.
libgcc for boot environment isn't always present and compatible. libgcc is often absent if endianness or bit-size at boot is different from running OS. libgcc may use optimised opcodes that aren't available on boot time. So instead of relying on libgcc shipped with the compiler, supply the functions in GRUB directly. Tests are present to ensure that those replacement functions behave the way compiler expects them to.
This commit is contained in:
parent
77697d14e5
commit
064360e667
21 changed files with 1441 additions and 479 deletions
121
grub-core/tests/bswap_test.c
Normal file
121
grub-core/tests/bswap_test.c
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2015 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/test.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
static grub_uint64_t vectors[] = {
|
||||
0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
|
||||
};
|
||||
|
||||
static void
|
||||
test16 (grub_uint16_t a)
|
||||
{
|
||||
grub_uint16_t b, c;
|
||||
grub_uint8_t *ap, *bp;
|
||||
int i;
|
||||
b = grub_swap_bytes16 (a);
|
||||
c = grub_swap_bytes16 (b);
|
||||
grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b, (long long) c);
|
||||
ap = (grub_uint8_t *) &a;
|
||||
bp = (grub_uint8_t *) &b;
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
grub_test_assert (ap[i] == bp[1 - i],
|
||||
"bswap bytes wrong: 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test32 (grub_uint32_t a)
|
||||
{
|
||||
grub_uint32_t b, c;
|
||||
grub_uint8_t *ap, *bp;
|
||||
int i;
|
||||
b = grub_swap_bytes32 (a);
|
||||
c = grub_swap_bytes32 (b);
|
||||
grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b, (long long) c);
|
||||
ap = (grub_uint8_t *) &a;
|
||||
bp = (grub_uint8_t *) &b;
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
grub_test_assert (ap[i] == bp[3 - i],
|
||||
"bswap bytes wrong: 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test64 (grub_uint64_t a)
|
||||
{
|
||||
grub_uint64_t b, c;
|
||||
grub_uint8_t *ap, *bp;
|
||||
int i;
|
||||
b = grub_swap_bytes64 (a);
|
||||
c = grub_swap_bytes64 (b);
|
||||
grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b, (long long) c);
|
||||
ap = (grub_uint8_t *) &a;
|
||||
bp = (grub_uint8_t *) &b;
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
grub_test_assert (ap[i] == bp[7 - i],
|
||||
"bswap bytes wrong: 0x%llx, 0x%llx",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_all(grub_uint64_t a)
|
||||
{
|
||||
test64 (a);
|
||||
test32 (a);
|
||||
test16 (a);
|
||||
}
|
||||
|
||||
static void
|
||||
bswap_test (void)
|
||||
{
|
||||
grub_uint64_t a = 404, b = 7;
|
||||
grub_size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE (vectors); i++)
|
||||
{
|
||||
test_all (vectors[i]);
|
||||
}
|
||||
for (i = 0; i < 40000; i++)
|
||||
{
|
||||
a = 17 * a + 13 * b;
|
||||
b = 23 * a + 29 * b;
|
||||
if (b == 0)
|
||||
b = 1;
|
||||
if (a == 0)
|
||||
a = 1;
|
||||
test_all (a);
|
||||
test_all (b);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register example_test method as a functional test. */
|
||||
GRUB_FUNCTIONAL_TEST (bswap_test, bswap_test);
|
190
grub-core/tests/cmp_test.c
Normal file
190
grub-core/tests/cmp_test.c
Normal file
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2015 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/test.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
static grub_uint64_t vectors[][2] = {
|
||||
{ 0xffffffffffffffffULL, 1},
|
||||
{ 1, 0xffffffffffffffffULL},
|
||||
{ 0xffffffffffffffffULL, 0xffffffffffffffffULL},
|
||||
{ 1, 1 },
|
||||
{ 2, 1 }
|
||||
};
|
||||
|
||||
/* Don't change those to use shift as shift may call to compile rt
|
||||
functions and we're not testing them now.
|
||||
*/
|
||||
static int
|
||||
leading_bit64 (grub_uint64_t a)
|
||||
{
|
||||
return !!(a & 0x8000000000000000LL);
|
||||
}
|
||||
|
||||
static int
|
||||
leading_bit32 (grub_uint32_t a)
|
||||
{
|
||||
return !!(a & 0x80000000);
|
||||
}
|
||||
|
||||
/* Computes (a < b) without involving comparison operator. */
|
||||
static int
|
||||
is_less32 (grub_uint32_t a, grub_uint32_t b)
|
||||
{
|
||||
if (leading_bit32(a) && !leading_bit32(b))
|
||||
return 0;
|
||||
if (!leading_bit32(a) && leading_bit32(b))
|
||||
return 1;
|
||||
return leading_bit32(a - b);
|
||||
}
|
||||
|
||||
static void
|
||||
test32 (grub_uint32_t a, grub_uint32_t b)
|
||||
{
|
||||
grub_test_assert ((a < b) == is_less32(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((a > b) == is_less32(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b < a) == is_less32(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b > a) == is_less32(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert (!(is_less32(a, b) && is_less32(b, a)), "comparison inconsistent: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
|
||||
/* Computes (a > b) without involving comparison operator. */
|
||||
static int
|
||||
is_less32s (grub_int32_t a, grub_int32_t b)
|
||||
{
|
||||
if (leading_bit32(a) && !leading_bit32(b))
|
||||
return 1; /* a < 0 && b >= 0. */
|
||||
if (!leading_bit32(a) && leading_bit32(b))
|
||||
return 0; /* b < 0 && a >= 0. */
|
||||
return leading_bit32(a - b);
|
||||
}
|
||||
|
||||
static void
|
||||
test32s (grub_int32_t a, grub_int32_t b)
|
||||
{
|
||||
grub_test_assert ((a < b) == is_less32s(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((a > b) == is_less32s(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b < a) == is_less32s(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b > a) == is_less32s(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert (!(is_less32s(a, b) && is_less32s(b, a)), "comparison inconsistent: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
|
||||
/* Computes (a > b) without involving comparison operator. */
|
||||
static int
|
||||
is_less64 (grub_uint64_t a, grub_uint64_t b)
|
||||
{
|
||||
if (leading_bit64(a) && !leading_bit64(b))
|
||||
return 0;
|
||||
if (!leading_bit64(a) && leading_bit64(b))
|
||||
return 1;
|
||||
return leading_bit64(a - b);
|
||||
}
|
||||
|
||||
static void
|
||||
test64 (grub_uint64_t a, grub_uint64_t b)
|
||||
{
|
||||
grub_test_assert ((a < b) == is_less64(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((a > b) == is_less64(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b < a) == is_less64(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b > a) == is_less64(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert (!(is_less64(a, b) && is_less64(b, a)), "comparison inconsistent: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
|
||||
/* Computes (a > b) without involving comparison operator. */
|
||||
static int
|
||||
is_less64s (grub_int64_t a, grub_int64_t b)
|
||||
{
|
||||
if (leading_bit64(a) && !leading_bit64(b))
|
||||
return 1; /* a < 0 && b >= 0. */
|
||||
if (!leading_bit64(a) && leading_bit64(b))
|
||||
return 0; /* b < 0 && a >= 0. */
|
||||
return leading_bit64(a - b);
|
||||
}
|
||||
|
||||
static void
|
||||
test64s (grub_int64_t a, grub_int64_t b)
|
||||
{
|
||||
grub_test_assert ((a < b) == is_less64s(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((a > b) == is_less64s(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b < a) == is_less64s(b, a), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert ((b > a) == is_less64s(a, b), "comparison result mismatch: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
grub_test_assert (!(is_less64s(a, b) && is_less64s(b, a)), "comparison inconsistent: %lld, %lld",
|
||||
(long long) a, (long long) b);
|
||||
}
|
||||
|
||||
static void
|
||||
test_all(grub_uint64_t a, grub_uint64_t b)
|
||||
{
|
||||
test64 (a, b);
|
||||
test32 (a, b);
|
||||
test64s (a, b);
|
||||
test32s (a, b);
|
||||
test64s (a, -b);
|
||||
test32s (a, -b);
|
||||
test64s (-a, b);
|
||||
test32s (-a, b);
|
||||
test64s (-a, -b);
|
||||
test32s (-a, -b);
|
||||
}
|
||||
|
||||
static void
|
||||
cmp_test (void)
|
||||
{
|
||||
grub_uint64_t a = 404, b = 7;
|
||||
grub_size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE (vectors); i++)
|
||||
{
|
||||
test_all (vectors[i][0], vectors[i][1]);
|
||||
}
|
||||
for (i = 0; i < 40000; i++)
|
||||
{
|
||||
a = 17 * a + 13 * b;
|
||||
b = 23 * a + 29 * b;
|
||||
if (b == 0)
|
||||
b = 1;
|
||||
if (a == 0)
|
||||
a = 1;
|
||||
test_all (a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register example_test method as a functional test. */
|
||||
GRUB_FUNCTIONAL_TEST (cmp_test, cmp_test);
|
111
grub-core/tests/ctz_test.c
Normal file
111
grub-core/tests/ctz_test.c
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/test.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
/* ull version is not used on i386 other than in this test.
|
||||
Avoid requiring extra function.
|
||||
*/
|
||||
#if defined (__i386__)
|
||||
#define SKIP_ULL 1
|
||||
#endif
|
||||
|
||||
static grub_uint64_t vectors[] = {
|
||||
0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
|
||||
};
|
||||
|
||||
static void
|
||||
test_ui (unsigned int a)
|
||||
{
|
||||
int i;
|
||||
a |= 1;
|
||||
for (i = 0; i < (int) (8 * sizeof (a)); i++)
|
||||
{
|
||||
grub_test_assert (__builtin_ctz(a << i) == i,
|
||||
"ctz mismatch: ctz(0x%llx) != 0x%x",
|
||||
(long long) (a << i), __builtin_ctz(a << i));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_ul (unsigned long a)
|
||||
{
|
||||
int i;
|
||||
a |= 1;
|
||||
for (i = 0; i < (int) (8 * sizeof (a)); i++)
|
||||
{
|
||||
grub_test_assert (__builtin_ctzl(a << i) == i,
|
||||
"ctzl mismatch: ctzl(0x%llx) != 0x%x",
|
||||
(long long) (a << i), __builtin_ctz(a << i));
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef SKIP_ULL
|
||||
static void
|
||||
test_ull (unsigned long long a)
|
||||
{
|
||||
int i;
|
||||
a |= 1;
|
||||
for (i = 0; i < (int) (8 * sizeof (a)); i++)
|
||||
{
|
||||
grub_test_assert (__builtin_ctzll(a << i) == i,
|
||||
"ctzll mismatch: ctzll(0x%llx) != 0x%x",
|
||||
(long long) (a << i), __builtin_ctz(a << i));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
test_all(grub_uint64_t a)
|
||||
{
|
||||
test_ui (a);
|
||||
test_ul (a);
|
||||
#ifndef SKIP_ULL
|
||||
test_ull (a);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
ctz_test (void)
|
||||
{
|
||||
grub_uint64_t a = 404, b = 7;
|
||||
grub_size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE (vectors); i++)
|
||||
{
|
||||
test_all (vectors[i]);
|
||||
}
|
||||
for (i = 0; i < 40000; i++)
|
||||
{
|
||||
a = 17 * a + 13 * b;
|
||||
b = 23 * a + 29 * b;
|
||||
if (b == 0)
|
||||
b = 1;
|
||||
if (a == 0)
|
||||
a = 1;
|
||||
test_all (a);
|
||||
test_all (b);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register example_test method as a functional test. */
|
||||
GRUB_FUNCTIONAL_TEST (ctz_test, ctz_test);
|
|
@ -65,6 +65,11 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),
|
|||
grub_dl_load ("pbkdf2_test");
|
||||
grub_dl_load ("signature_test");
|
||||
grub_dl_load ("sleep_test");
|
||||
grub_dl_load ("bswap_test");
|
||||
grub_dl_load ("ctz_test");
|
||||
grub_dl_load ("cmp_test");
|
||||
grub_dl_load ("mul_test");
|
||||
grub_dl_load ("shift_test");
|
||||
|
||||
FOR_LIST_ELEMENTS (test, grub_test_list)
|
||||
ok = !grub_test_run (test) && ok;
|
||||
|
|
73
grub-core/tests/mul_test.c
Normal file
73
grub-core/tests/mul_test.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/test.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
static grub_uint64_t vectors[][2] = {
|
||||
{ 0xffffffffffffffffULL, 1},
|
||||
{ 1, 0xffffffffffffffffULL},
|
||||
{ 0xffffffffffffffffULL, 0xffffffffffffffffULL},
|
||||
{ 1, 1 },
|
||||
{ 2, 1 }
|
||||
};
|
||||
|
||||
static void
|
||||
test64(grub_uint64_t a, grub_uint64_t b)
|
||||
{
|
||||
grub_uint64_t r1 = a * b, r2 = 0, r3;
|
||||
int i;
|
||||
for (i = 0; i < 64; i++)
|
||||
if ((a & (1LL << i)))
|
||||
r2 += b << i;
|
||||
r3 = ((grub_int64_t) a) * ((grub_int64_t) b);
|
||||
grub_test_assert (r1 == r2,
|
||||
"multiplication mismatch (u): 0x%llx x 0x%llx = 0x%llx != 0x%llx",
|
||||
(long long) a, (long long) b, (long long) r2, (long long) r1);
|
||||
grub_test_assert (r3 == r2,
|
||||
"multiplication mismatch (s): 0x%llx x 0x%llx = 0x%llx != 0x%llx",
|
||||
(long long) a, (long long) b, (long long) r2, (long long) r3);
|
||||
}
|
||||
|
||||
static void
|
||||
mul_test (void)
|
||||
{
|
||||
grub_uint64_t a = 404, b = 7;
|
||||
grub_size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE (vectors); i++)
|
||||
{
|
||||
test64 (vectors[i][0], vectors[i][1]);
|
||||
}
|
||||
for (i = 0; i < 40000; i++)
|
||||
{
|
||||
a = 17 * a + 13 * b;
|
||||
b = 23 * a + 29 * b;
|
||||
if (b == 0)
|
||||
b = 1;
|
||||
if (a == 0)
|
||||
a = 1;
|
||||
test64 (a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register example_test method as a functional test. */
|
||||
GRUB_FUNCTIONAL_TEST (mul_test, mul_test);
|
157
grub-core/tests/shift_test.c
Normal file
157
grub-core/tests/shift_test.c
Normal file
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2015 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/test.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
static grub_uint64_t vectors[] = {
|
||||
0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
|
||||
};
|
||||
|
||||
/* We're testing shifts, don't replace access to this with a shift. */
|
||||
static const grub_uint8_t bitmask[] =
|
||||
{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
|
||||
|
||||
typedef union {
|
||||
grub_uint64_t v64;
|
||||
grub_uint8_t v8[8];
|
||||
} grub_raw_u64_t;
|
||||
|
||||
static int
|
||||
get_bit64 (grub_uint64_t v, int b)
|
||||
{
|
||||
grub_raw_u64_t vr = { .v64 = v };
|
||||
grub_uint8_t *p = vr.v8;
|
||||
if (b >= 64)
|
||||
return 0;
|
||||
#ifdef GRUB_CPU_WORDS_BIGENDIAN
|
||||
p += 7 - b / 8;
|
||||
#else
|
||||
p += b / 8;
|
||||
#endif
|
||||
return !!(*p & bitmask[b % 8]);
|
||||
}
|
||||
|
||||
static grub_uint64_t
|
||||
set_bit64 (grub_uint64_t v, int b)
|
||||
{
|
||||
grub_raw_u64_t vr = { .v64 = v };
|
||||
grub_uint8_t *p = vr.v8;
|
||||
if (b >= 64)
|
||||
return v;
|
||||
#ifdef GRUB_CPU_WORDS_BIGENDIAN
|
||||
p += 7 - b / 8;
|
||||
#else
|
||||
p += b / 8;
|
||||
#endif
|
||||
*p |= bitmask[b % 8];
|
||||
return vr.v64;
|
||||
}
|
||||
|
||||
static grub_uint64_t
|
||||
left_shift64 (grub_uint64_t v, int s)
|
||||
{
|
||||
grub_uint64_t r = 0;
|
||||
int i;
|
||||
for (i = 0; i + s < 64; i++)
|
||||
if (get_bit64 (v, i))
|
||||
r = set_bit64 (r, i + s);
|
||||
return r;
|
||||
}
|
||||
|
||||
static grub_uint64_t
|
||||
right_shift64 (grub_uint64_t v, int s)
|
||||
{
|
||||
grub_uint64_t r = 0;
|
||||
int i;
|
||||
for (i = s; i < 64; i++)
|
||||
if (get_bit64 (v, i))
|
||||
r = set_bit64 (r, i - s);
|
||||
return r;
|
||||
}
|
||||
|
||||
static grub_uint64_t
|
||||
arithmetic_right_shift64 (grub_uint64_t v, int s)
|
||||
{
|
||||
grub_uint64_t r = 0;
|
||||
int i;
|
||||
for (i = s; i < 64; i++)
|
||||
if (get_bit64 (v, i))
|
||||
r = set_bit64 (r, i - s);
|
||||
if (get_bit64 (v, 63))
|
||||
for (i -= s; i < 64; i++)
|
||||
r = set_bit64 (r, i);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void
|
||||
test64 (grub_uint64_t v)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 64; i++)
|
||||
{
|
||||
grub_test_assert ((v << i) == left_shift64 (v, i),
|
||||
"lshift wrong: 0x%llx << %d: 0x%llx, 0x%llx",
|
||||
(long long) v, i,
|
||||
(long long) (v << i), (long long) left_shift64 (v, i));
|
||||
grub_test_assert ((v >> i) == right_shift64 (v, i),
|
||||
"rshift wrong: 0x%llx >> %d: 0x%llx, 0x%llx",
|
||||
(long long) v, i,
|
||||
(long long) (v >> i), (long long) right_shift64 (v, i));
|
||||
grub_test_assert ((((grub_int64_t) v) >> i) == (grub_int64_t) arithmetic_right_shift64 (v, i),
|
||||
"arithmetic rshift wrong: ((grub_int64_t) 0x%llx) >> %d: 0x%llx, 0x%llx",
|
||||
(long long) v, i,
|
||||
(long long) (((grub_int64_t) v) >> i), (long long) arithmetic_right_shift64 (v, i));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_all(grub_uint64_t a)
|
||||
{
|
||||
test64 (a);
|
||||
}
|
||||
|
||||
static void
|
||||
shift_test (void)
|
||||
{
|
||||
grub_uint64_t a = 404, b = 7;
|
||||
grub_size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE (vectors); i++)
|
||||
{
|
||||
test_all (vectors[i]);
|
||||
}
|
||||
for (i = 0; i < 4000; i++)
|
||||
{
|
||||
a = 17 * a + 13 * b;
|
||||
b = 23 * a + 29 * b;
|
||||
if (b == 0)
|
||||
b = 1;
|
||||
if (a == 0)
|
||||
a = 1;
|
||||
test_all (a);
|
||||
test_all (b);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register example_test method as a functional test. */
|
||||
GRUB_FUNCTIONAL_TEST (shift_test, shift_test);
|
Loading…
Add table
Add a link
Reference in a new issue