qemu/tests/test-shift128.c
<<
>>
Prefs
   1/*
   2 * Test unsigned left and right shift
   3 *
   4 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
   5 * See the COPYING.LIB file in the top-level directory.
   6 *
   7 */
   8
   9#include "qemu/osdep.h"
  10#include "qemu/host-utils.h"
  11
  12typedef struct {
  13    uint64_t low;
  14    uint64_t high;
  15    uint64_t rlow;
  16    uint64_t rhigh;
  17    int32_t shift;
  18    bool overflow;
  19} test_data;
  20
  21static const test_data test_ltable[] = {
  22    { 0x4C7ULL, 0x0ULL, 0x00000000000004C7ULL,
  23      0x0000000000000000ULL,   0, false },
  24    { 0x001ULL, 0x0ULL, 0x0000000000000002ULL,
  25      0x0000000000000000ULL,   1, false },
  26    { 0x001ULL, 0x0ULL, 0x0000000000000004ULL,
  27      0x0000000000000000ULL,   2, false },
  28    { 0x001ULL, 0x0ULL, 0x0000000000000010ULL,
  29      0x0000000000000000ULL,   4, false },
  30    { 0x001ULL, 0x0ULL, 0x0000000000000100ULL,
  31      0x0000000000000000ULL,   8, false },
  32    { 0x001ULL, 0x0ULL, 0x0000000000010000ULL,
  33      0x0000000000000000ULL,  16, false },
  34    { 0x001ULL, 0x0ULL, 0x0000000080000000ULL,
  35      0x0000000000000000ULL,  31, false },
  36    { 0x001ULL, 0x0ULL, 0x0000200000000000ULL,
  37      0x0000000000000000ULL,  45, false },
  38    { 0x001ULL, 0x0ULL, 0x1000000000000000ULL,
  39      0x0000000000000000ULL,  60, false },
  40    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  41      0x0000000000000001ULL,  64, false },
  42    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  43      0x0000000000010000ULL,  80, false },
  44    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  45      0x8000000000000000ULL, 127, false },
  46    { 0x000ULL, 0x1ULL, 0x0000000000000000ULL,
  47      0x0000000000000000ULL,  64,  true },
  48    { 0x008ULL, 0x0ULL, 0x0000000000000000ULL,
  49      0x0000000000000008ULL,  64, false },
  50    { 0x008ULL, 0x0ULL, 0x0000000000000000ULL,
  51      0x8000000000000000ULL, 124, false },
  52    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  53      0x4000000000000000ULL, 126, false },
  54    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  55      0x8000000000000000ULL, 127, false },
  56    { 0x001ULL, 0x0ULL, 0x0000000000000001ULL,
  57      0x0000000000000000ULL, 128,  false },
  58    { 0x000ULL, 0x0ULL, 0x0000000000000000ULL,
  59      0x0000000000000000ULL, 200, false },
  60    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  61      0x0000000000000100ULL, 200,  false },
  62    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  63      0x8000000000000000ULL,  -1, false },
  64    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  65      0x8000000000000000ULL, INT32_MAX, false },
  66    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  67      0x4000000000000000ULL,  -2, false },
  68    { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
  69      0x4000000000000000ULL, INT32_MAX - 1, false },
  70    { 0x8888888888888888ULL, 0x9999999999999999ULL,
  71      0x8000000000000000ULL, 0x9888888888888888ULL, 60, true },
  72    { 0x8888888888888888ULL, 0x9999999999999999ULL,
  73      0x0000000000000000ULL, 0x8888888888888888ULL, 64, true },
  74};
  75
  76static const test_data test_rtable[] = {
  77    { 0x00000000000004C7ULL, 0x0ULL, 0x00000000000004C7ULL, 0x0ULL,  0, false },
  78    { 0x0800000000000000ULL, 0x0ULL, 0x0400000000000000ULL, 0x0ULL,  1, false },
  79    { 0x0800000000000000ULL, 0x0ULL, 0x0200000000000000ULL, 0x0ULL,  2, false },
  80    { 0x0800000000000000ULL, 0x0ULL, 0x0008000000000000ULL, 0x0ULL,  8, false },
  81    { 0x0800000000000000ULL, 0x0ULL, 0x0000080000000000ULL, 0x0ULL, 16, false },
  82    { 0x0800000000000000ULL, 0x0ULL, 0x0000000008000000ULL, 0x0ULL, 32, false },
  83    { 0x8000000000000000ULL, 0x0ULL, 0x0000000000000001ULL, 0x0ULL, 63, false },
  84    { 0x8000000000000000ULL, 0x0ULL, 0x0000000000000000ULL, 0x0ULL, 64, false },
  85    { 0x0000000000000000ULL, 0x8000000000000000ULL,
  86      0x0000000000000000ULL, 0x8000000000000000ULL, 128, false },
  87    { 0x0000000000000000ULL, 0x8000000000000000ULL,
  88      0x0080000000000000ULL, 0x0000000000000000ULL, 200, false },
  89    { 0x0000000000000000ULL, 0x0000000000000000ULL,
  90      0x0000000000000000ULL, 0x0000000000000000ULL, 200, false },
  91    { 0x0000000000000000ULL, 0x8000000000000000ULL,
  92      0x0000000000000000ULL, 0x0000000000000080ULL, -200, false },
  93    { 0x8000000000000000ULL, 0x8000000000000000ULL,
  94      0x0000000080000000ULL, 0x0000000080000000ULL, 32, false },
  95    { 0x0800000000000000ULL, 0x0800000000000000ULL,
  96      0x0800000000000000ULL, 0x0000000000000000ULL, 64, false },
  97    { 0x0800000000000000ULL, 0x0800000000000000ULL,
  98      0x0008000000000000ULL, 0x0000000000000000ULL, 72, false },
  99    { 0x8000000000000000ULL, 0x8000000000000000ULL,
 100      0x0000000000000001ULL, 0x0000000000000000ULL, 127, false },
 101    { 0x0000000000000000ULL, 0x8000000000000000ULL,
 102      0x0000000000000001ULL, 0x0000000000000000ULL, -1, false },
 103    { 0x0000000000000000ULL, 0x8000000000000000ULL,
 104      0x0000000000000002ULL, 0x0000000000000000ULL, -2, false },
 105};
 106
 107static void test_lshift(void)
 108{
 109    int i;
 110
 111    for (i = 0; i < ARRAY_SIZE(test_ltable); ++i) {
 112        bool overflow = false;
 113        test_data tmp = test_ltable[i];
 114        ulshift(&tmp.low, &tmp.high, tmp.shift, &overflow);
 115        g_assert_cmpuint(tmp.low, ==, tmp.rlow);
 116        g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
 117        g_assert_cmpuint(tmp.overflow, ==, overflow);
 118    }
 119}
 120
 121static void test_rshift(void)
 122{
 123    int i;
 124
 125    for (i = 0; i < ARRAY_SIZE(test_rtable); ++i) {
 126        test_data tmp = test_rtable[i];
 127        urshift(&tmp.low, &tmp.high, tmp.shift);
 128        g_assert_cmpuint(tmp.low, ==, tmp.rlow);
 129        g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
 130    }
 131}
 132
 133int main(int argc, char **argv)
 134{
 135    g_test_init(&argc, &argv, NULL);
 136    g_test_add_func("/host-utils/test_lshift", test_lshift);
 137    g_test_add_func("/host-utils/test_rshift", test_rshift);
 138    return g_test_run();
 139}
 140