Skip to content

Commit 5d8f968

Browse files
committed
Add unit tests for bit manipulation instructions
1 parent 3a6f9d1 commit 5d8f968

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

src/westmere/bits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <stdbool.h>
1313
#include <stdint.h>
14+
#include <immintrin.h>
1415

1516
static inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) {
1617
#if has_builtin(__builtin_uaddll_overflow)

tests/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
find_package(cmocka REQUIRED)
2-
cmocka_add_tests(zone-tests types.c include.c ip4.c time.c base32.c svcb.c syntax.c eui.c)
2+
if(HAVE_WESTMERE)
3+
set(sources ${sources} bits-westmere.c)
4+
set_source_files_properties(bits-westmere.c PROPERTIES COMPILE_FLAGS "-march=westmere")
5+
endif()
6+
if(HAVE_HASWELL)
7+
set(sources ${sources} bits-haswell.c)
8+
set_source_files_properties(bits-haswell.c PROPERTIES COMPILE_FLAGS "-march=haswell")
9+
endif()
10+
11+
cmocka_add_tests(zone-tests types.c include.c ip4.c time.c base32.c svcb.c syntax.c eui.c ${sources})
312

413
target_link_libraries(zone-tests PRIVATE zone)
514
target_sources(zone-tests PRIVATE tools.c)

tests/bits-haswell.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* bits-haswell.c -- test Haswell specific 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 <stdarg.h>
10+
#include <setjmp.h>
11+
#include <string.h>
12+
#include <cmocka.h>
13+
14+
#include "attributes.h"
15+
#include "haswell/bits.h"
16+
17+
/*!cmocka */
18+
void haswell_trailing_zeroes(void **state)
19+
{
20+
(void)state;
21+
for (uint64_t shift = 0; shift < 63; shift++) {
22+
uint64_t bit = 1llu << shift;
23+
uint64_t tz = trailing_zeroes(bit);
24+
assert_int_equal(tz, shift);
25+
}
26+
}
27+
28+
/*!cmocka */
29+
void haswell_leading_zeroes(void **state)
30+
{
31+
(void)state;
32+
for (uint64_t shift = 0; shift << 63; shift++) {
33+
const uint64_t bit = 1llu << shift;
34+
uint64_t lz = leading_zeroes(bit);
35+
assert_int_equal(lz, 63 - shift);
36+
}
37+
}
38+
39+
/*!cmocka */
40+
void haswell_prefix_xor(void **state)
41+
{
42+
(void)state;
43+
// "0001 0001 0000 0101 0000 0110 0000 0000"
44+
const uint64_t mask = 0x11050600llu;
45+
// "0001 1110 0000 0110 0000 0100 0000 0000"
46+
const uint64_t xor_mask = 0x1E060400llu;
47+
48+
assert_int_equal(prefix_xor(mask), xor_mask);
49+
}
50+
51+
/*!cm ocka */
52+
// add_overflow

tests/bits-westmere.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* bits-westmere.c -- test Westmere specific 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 <stdarg.h>
10+
#include <setjmp.h>
11+
#include <string.h>
12+
#include <cmocka.h>
13+
14+
#include "attributes.h"
15+
#include "westmere/bits.h"
16+
17+
/*!cmocka */
18+
void westmere_trailing_zeroes(void **state)
19+
{
20+
(void)state;
21+
for (uint64_t shift = 0; shift < 63; shift++) {
22+
uint64_t bit = 1llu << shift;
23+
uint64_t tz = trailing_zeroes(bit);
24+
assert_int_equal(tz, shift);
25+
}
26+
}
27+
28+
/*!cmocka */
29+
void westmere_leading_zeroes(void **state)
30+
{
31+
(void)state;
32+
for (uint64_t shift = 0; shift << 63; shift++) {
33+
const uint64_t bit = 1llu << shift;
34+
uint64_t lz = leading_zeroes(bit);
35+
assert_int_equal(lz, 63 - shift);
36+
}
37+
}
38+
39+
/*!cmocka */
40+
void westmere_prefix_xor(void **state)
41+
{
42+
(void)state;
43+
// "0001 0001 0000 0101 0000 0110 0000 0000"
44+
const uint64_t mask = 0x11050600llu;
45+
// "0001 1110 0000 0110 0000 0100 0000 0000"
46+
const uint64_t xor_mask = 0x1E060400llu;
47+
48+
assert_int_equal(prefix_xor(mask), xor_mask);
49+
}
50+
51+
/*!cm ocka */
52+
// add_overflow

0 commit comments

Comments
 (0)