|
| 1 | +/* |
| 2 | + * bits.c -- test bit manipulation instructions |
| 3 | + * |
| 4 | + * Copyright (c) 2024, NLnet Labs. All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + * |
| 8 | + */ |
| 9 | +#include <assert.h> |
| 10 | +#include <stdarg.h> |
| 11 | +#include <setjmp.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <stddef.h> |
| 14 | +#include <string.h> |
| 15 | +#include <cmocka.h> |
| 16 | + |
| 17 | +#include "config.h" |
| 18 | +#include "attributes.h" |
| 19 | +#include "isadetection.h" |
| 20 | + |
| 21 | +#if _MSC_VER |
| 22 | +# define strcasecmp(s1, s2) _stricmp(s1, s2) |
| 23 | +# define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n) |
| 24 | +#else |
| 25 | +#include <strings.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +struct kernel { |
| 29 | + const char *name; |
| 30 | + uint32_t instruction_set; |
| 31 | + void (*test_trailing_zeroes)(void **state); |
| 32 | + void (*test_leading_zeroes)(void **state); |
| 33 | + void (*test_prefix_xor)(void **state); |
| 34 | + void (*test_add_overflow)(void **state); |
| 35 | +}; |
| 36 | + |
| 37 | +#if HAVE_HASWELL |
| 38 | +extern void test_haswell_trailing_zeroes(void **); |
| 39 | +extern void test_haswell_leading_zeroes(void **); |
| 40 | +extern void test_haswell_prefix_xor(void **); |
| 41 | +extern void test_haswell_add_overflow(void **); |
| 42 | +#endif |
| 43 | + |
| 44 | +#if HAVE_WESTMERE |
| 45 | +extern void test_westmere_trailing_zeroes(void **); |
| 46 | +extern void test_westmere_leading_zeroes(void **); |
| 47 | +extern void test_westmere_prefix_xor(void **); |
| 48 | +extern void test_westmere_add_overflow(void **); |
| 49 | +#endif |
| 50 | + |
| 51 | +extern void test_fallback_trailing_zeroes(void **); |
| 52 | +extern void test_fallback_leading_zeroes(void **); |
| 53 | + |
| 54 | +static const struct kernel kernels[] = { |
| 55 | +#if HAVE_HASWELL |
| 56 | + { "haswell", AVX2, &test_haswell_trailing_zeroes, |
| 57 | + &test_haswell_leading_zeroes, |
| 58 | + &test_haswell_prefix_xor, |
| 59 | + &test_haswell_add_overflow }, |
| 60 | +#endif |
| 61 | +#if HAVE_WESTMERE |
| 62 | + { "westmere", SSE42, &test_westmere_trailing_zeroes, |
| 63 | + &test_westmere_leading_zeroes, |
| 64 | + &test_westmere_prefix_xor, |
| 65 | + &test_westmere_add_overflow }, |
| 66 | +#endif |
| 67 | + { "fallback", DEFAULT, &test_fallback_trailing_zeroes, |
| 68 | + &test_fallback_leading_zeroes, |
| 69 | + 0, 0 } |
| 70 | +}; |
| 71 | + |
| 72 | +static inline const struct kernel * |
| 73 | +select_kernel(void) |
| 74 | +{ |
| 75 | + const char *preferred; |
| 76 | + const uint32_t supported = detect_supported_architectures(); |
| 77 | + const size_t length = sizeof(kernels)/sizeof(kernels[0]); |
| 78 | + size_t count = 0; |
| 79 | + |
| 80 | + if ((preferred = getenv("ZONE_KERNEL"))) { |
| 81 | + for (; count < length; count++) |
| 82 | + if (strcasecmp(preferred, kernels[count].name) == 0) |
| 83 | + break; |
| 84 | + if (count == length) |
| 85 | + count = 0; |
| 86 | + } |
| 87 | + |
| 88 | + for (; count < length; count++) |
| 89 | + if ((kernels[count].instruction_set & supported) == (kernels[count].instruction_set)) |
| 90 | + return &kernels[count]; |
| 91 | + |
| 92 | + return &kernels[length - 1]; |
| 93 | +} |
| 94 | + |
| 95 | +/*!cmocka */ |
| 96 | +void test_trailing_zeroes(void **state) |
| 97 | +{ |
| 98 | + const struct kernel *kernel = select_kernel(); |
| 99 | + assert(kernel); |
| 100 | + kernel->test_trailing_zeroes(state); |
| 101 | +} |
| 102 | + |
| 103 | +/*!cmocka */ |
| 104 | +void test_leading_zeroes(void **state) |
| 105 | +{ |
| 106 | + const struct kernel *kernel = select_kernel(); |
| 107 | + assert(kernel); |
| 108 | + kernel->test_leading_zeroes(state); |
| 109 | +} |
| 110 | + |
| 111 | +/*!cmocka */ |
| 112 | +void test_prefix_xor(void **state) |
| 113 | +{ |
| 114 | + const struct kernel *kernel = select_kernel(); |
| 115 | + assert(kernel); |
| 116 | + if (kernel->test_prefix_xor) |
| 117 | + kernel->test_prefix_xor(state); |
| 118 | + else |
| 119 | + assert_true(1); |
| 120 | +} |
| 121 | + |
| 122 | +/*!cmocka */ |
| 123 | +void test_add_overflow(void **state) |
| 124 | +{ |
| 125 | + const struct kernel *kernel = select_kernel(); |
| 126 | + assert(kernel); |
| 127 | + if (kernel->test_add_overflow) |
| 128 | + kernel->test_add_overflow(state); |
| 129 | + else |
| 130 | + assert_true(1); |
| 131 | +} |
0 commit comments