cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

test-bitops.c (3605B)


      1/*
      2 * Test bitops routines
      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/bitops.h"
     11
     12typedef struct {
     13    uint32_t value;
     14    int start;
     15    int length;
     16    int32_t result;
     17} S32Test;
     18
     19typedef struct {
     20    uint64_t value;
     21    int start;
     22    int length;
     23    int64_t result;
     24} S64Test;
     25
     26static const S32Test test_s32_data[] = {
     27    { 0x38463983, 4, 4, -8 },
     28    { 0x38463983, 12, 8, 0x63 },
     29    { 0x38463983, 0, 32, 0x38463983 },
     30};
     31
     32static const S64Test test_s64_data[] = {
     33    { 0x8459826734967223ULL, 60, 4, -8 },
     34    { 0x8459826734967223ULL, 0, 64, 0x8459826734967223LL },
     35};
     36
     37static void test_sextract32(void)
     38{
     39    int i;
     40
     41    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
     42        const S32Test *test = &test_s32_data[i];
     43        int32_t r = sextract32(test->value, test->start, test->length);
     44
     45        g_assert_cmpint(r, ==, test->result);
     46    }
     47}
     48
     49static void test_sextract64(void)
     50{
     51    int i;
     52
     53    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
     54        const S32Test *test = &test_s32_data[i];
     55        int64_t r = sextract64(test->value, test->start, test->length);
     56
     57        g_assert_cmpint(r, ==, test->result);
     58    }
     59
     60    for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) {
     61        const S64Test *test = &test_s64_data[i];
     62        int64_t r = sextract64(test->value, test->start, test->length);
     63
     64        g_assert_cmpint(r, ==, test->result);
     65    }
     66}
     67
     68typedef struct {
     69    uint32_t unshuffled;
     70    uint32_t shuffled;
     71} Shuffle32Test;
     72
     73typedef struct {
     74    uint64_t unshuffled;
     75    uint64_t shuffled;
     76} Shuffle64Test;
     77
     78static const Shuffle32Test test_shuffle32_data[] = {
     79    { 0x0000FFFF, 0x55555555 },
     80    { 0x000081C5, 0x40015011 },
     81};
     82
     83static const Shuffle64Test test_shuffle64_data[] = {
     84    { 0x00000000FFFFFFFFULL, 0x5555555555555555ULL },
     85    { 0x00000000493AB02CULL, 0x1041054445000450ULL },
     86};
     87
     88static void test_half_shuffle32(void)
     89{
     90    int i;
     91
     92    for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
     93        const Shuffle32Test *test = &test_shuffle32_data[i];
     94        uint32_t r = half_shuffle32(test->unshuffled);
     95
     96        g_assert_cmpint(r, ==, test->shuffled);
     97    }
     98}
     99
    100static void test_half_shuffle64(void)
    101{
    102    int i;
    103
    104    for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
    105        const Shuffle64Test *test = &test_shuffle64_data[i];
    106        uint64_t r = half_shuffle64(test->unshuffled);
    107
    108        g_assert_cmpint(r, ==, test->shuffled);
    109    }
    110}
    111
    112static void test_half_unshuffle32(void)
    113{
    114    int i;
    115
    116    for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
    117        const Shuffle32Test *test = &test_shuffle32_data[i];
    118        uint32_t r = half_unshuffle32(test->shuffled);
    119
    120        g_assert_cmpint(r, ==, test->unshuffled);
    121    }
    122}
    123
    124static void test_half_unshuffle64(void)
    125{
    126    int i;
    127
    128    for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
    129        const Shuffle64Test *test = &test_shuffle64_data[i];
    130        uint64_t r = half_unshuffle64(test->shuffled);
    131
    132        g_assert_cmpint(r, ==, test->unshuffled);
    133    }
    134}
    135
    136int main(int argc, char **argv)
    137{
    138    g_test_init(&argc, &argv, NULL);
    139    g_test_add_func("/bitops/sextract32", test_sextract32);
    140    g_test_add_func("/bitops/sextract64", test_sextract64);
    141    g_test_add_func("/bitops/half_shuffle32", test_half_shuffle32);
    142    g_test_add_func("/bitops/half_shuffle64", test_half_shuffle64);
    143    g_test_add_func("/bitops/half_unshuffle32", test_half_unshuffle32);
    144    g_test_add_func("/bitops/half_unshuffle64", test_half_unshuffle64);
    145    return g_test_run();
    146}