1 | /* |
2 | * util_unit.c |
3 | * |
4 | */ |
5 | |
6 | #include <assert.h> |
7 | #include <stdint.h> |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | |
11 | #include <roaring/bitset_util.h> |
12 | |
13 | #include "test.h" |
14 | |
15 | void setandextract_uint16() { |
16 | const unsigned int bitset_size = 1 << 16; |
17 | const unsigned int bitset_size_in_words = |
18 | bitset_size / (sizeof(uint64_t) * 8); |
19 | |
20 | for (unsigned int offset = 1; offset < bitset_size; offset++) { |
21 | const unsigned int valsize = bitset_size / offset; |
22 | uint16_t* vals = malloc(valsize * sizeof(uint16_t)); |
23 | uint64_t* bitset = calloc(bitset_size_in_words, sizeof(uint64_t)); |
24 | for (unsigned int k = 0; k < valsize; ++k) { |
25 | vals[k] = (uint16_t)(k * offset); |
26 | } |
27 | |
28 | bitset_set_list(bitset, vals, valsize); |
29 | uint16_t* newvals = malloc(valsize * sizeof(uint16_t)); |
30 | bitset_extract_setbits_uint16(bitset, bitset_size_in_words, newvals, 0); |
31 | |
32 | for (unsigned int k = 0; k < valsize; ++k) { |
33 | assert_int_equal(newvals[k], vals[k]); |
34 | } |
35 | |
36 | free(vals); |
37 | free(newvals); |
38 | free(bitset); |
39 | } |
40 | } |
41 | |
42 | #ifdef IS_X64 |
43 | void setandextract_sse_uint16() { |
44 | const unsigned int bitset_size = 1 << 16; |
45 | const unsigned int bitset_size_in_words = |
46 | bitset_size / (sizeof(uint64_t) * 8); |
47 | |
48 | for (unsigned int offset = 1; offset < bitset_size; offset++) { |
49 | const unsigned int valsize = bitset_size / offset; |
50 | uint16_t* vals = malloc(valsize * sizeof(uint16_t)); |
51 | uint64_t* bitset = calloc(bitset_size_in_words, sizeof(uint64_t)); |
52 | for (unsigned int k = 0; k < valsize; ++k) { |
53 | vals[k] = (uint16_t)(k * offset); |
54 | } |
55 | |
56 | bitset_set_list(bitset, vals, valsize); |
57 | uint16_t* newvals = malloc(valsize * sizeof(uint16_t) + 64); |
58 | bitset_extract_setbits_sse_uint16(bitset, bitset_size_in_words, newvals, |
59 | valsize, 0); |
60 | |
61 | for (unsigned int k = 0; k < valsize; ++k) { |
62 | assert_int_equal(newvals[k], vals[k]); |
63 | } |
64 | |
65 | free(vals); |
66 | free(newvals); |
67 | free(bitset); |
68 | } |
69 | } |
70 | #endif |
71 | |
72 | void setandextract_uint32() { |
73 | const unsigned int bitset_size = 1 << 16; |
74 | const unsigned int bitset_size_in_words = |
75 | bitset_size / (sizeof(uint64_t) * 8); |
76 | |
77 | for (unsigned int offset = 1; offset < bitset_size; offset++) { |
78 | const unsigned int valsize = bitset_size / offset; |
79 | uint16_t* vals = malloc(valsize * sizeof(uint16_t)); |
80 | uint64_t* bitset = calloc(bitset_size_in_words, sizeof(uint64_t)); |
81 | |
82 | for (unsigned int k = 0; k < valsize; ++k) { |
83 | vals[k] = (uint16_t)(k * offset); |
84 | } |
85 | |
86 | bitset_set_list(bitset, vals, valsize); |
87 | uint32_t* newvals = malloc(valsize * sizeof(uint32_t)); |
88 | bitset_extract_setbits(bitset, bitset_size_in_words, newvals, 0); |
89 | |
90 | for (unsigned int k = 0; k < valsize; ++k) { |
91 | assert_int_equal(newvals[k], vals[k]); |
92 | } |
93 | |
94 | free(vals); |
95 | free(newvals); |
96 | free(bitset); |
97 | } |
98 | } |
99 | |
100 | #ifdef USE_AVX |
101 | // returns 1 when ok |
102 | void setandextract_avx2_uint32() { |
103 | const unsigned int bitset_size = 1 << 16; |
104 | const unsigned int bitset_size_in_words = |
105 | bitset_size / (sizeof(uint64_t) * 8); |
106 | |
107 | for (unsigned int offset = 1; offset < bitset_size; offset++) { |
108 | const unsigned int valsize = bitset_size / offset; |
109 | uint16_t* vals = malloc(valsize * sizeof(uint16_t)); |
110 | uint64_t* bitset = calloc(bitset_size_in_words, sizeof(uint64_t)); |
111 | |
112 | for (unsigned int k = 0; k < valsize; ++k) { |
113 | vals[k] = (uint16_t)(k * offset); |
114 | } |
115 | |
116 | bitset_set_list(bitset, vals, valsize); |
117 | uint32_t* newvals = malloc(valsize * sizeof(uint32_t) + 64); |
118 | bitset_extract_setbits_avx2(bitset, bitset_size_in_words, newvals, |
119 | valsize, 0); |
120 | |
121 | for (unsigned int k = 0; k < valsize; ++k) { |
122 | assert_int_equal(newvals[k], vals[k]); |
123 | } |
124 | |
125 | free(vals); |
126 | free(newvals); |
127 | free(bitset); |
128 | } |
129 | } |
130 | #endif |
131 | |
132 | int main() { |
133 | const struct CMUnitTest tests[] = { |
134 | cmocka_unit_test(setandextract_uint16), |
135 | #ifdef IS_X64 |
136 | cmocka_unit_test(setandextract_sse_uint16), |
137 | #endif |
138 | cmocka_unit_test(setandextract_uint32), |
139 | #ifdef USE_AVX |
140 | cmocka_unit_test(setandextract_avx2_uint32), |
141 | #endif |
142 | }; |
143 | |
144 | return cmocka_run_group_tests(tests, NULL, NULL); |
145 | } |
146 | |