Skip to content

Commit 82146b0

Browse files
committed
Fix: Type casting warnings & free
1 parent 4d8ac78 commit 82146b0

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

include/stringzilla/stringzilla.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6081,7 +6081,7 @@ SZ_PUBLIC void sz_fill_sve(sz_ptr_t target, sz_size_t length, sz_u8_t value) {
60816081

60826082
if (length <= vec_len) {
60836083
// Small buffer case: use mask to handle small writes
6084-
svbool_t mask = svwhilelt_b8(0ul, length);
6084+
svbool_t mask = svwhilelt_b8(0ull, length);
60856085
svst1_u8(mask, (unsigned char *)target, value_vec);
60866086
}
60876087
else {
@@ -6091,7 +6091,7 @@ SZ_PUBLIC void sz_fill_sve(sz_ptr_t target, sz_size_t length, sz_u8_t value) {
60916091
sz_size_t body_length = length - head_length - tail_length;
60926092

60936093
// Handle unaligned head
6094-
svbool_t head_mask = svwhilelt_b8(0ul, head_length);
6094+
svbool_t head_mask = svwhilelt_b8(0ull, head_length);
60956095
svst1_u8(head_mask, (unsigned char *)target, value_vec);
60966096
target += head_length;
60976097

@@ -6101,7 +6101,7 @@ SZ_PUBLIC void sz_fill_sve(sz_ptr_t target, sz_size_t length, sz_u8_t value) {
61016101
}
61026102

61036103
// Handle unaligned tail
6104-
svbool_t tail_mask = svwhilelt_b8(0ul, tail_length);
6104+
svbool_t tail_mask = svwhilelt_b8(0ull, tail_length);
61056105
svst1_u8(tail_mask, (unsigned char *)target, value_vec);
61066106
}
61076107
}
@@ -6118,7 +6118,7 @@ SZ_PUBLIC void sz_copy_sve(sz_ptr_t target, sz_cptr_t source, sz_size_t length)
61186118
// When the buffer is small, there isn't much to innovate.
61196119
if (length <= vec_len) {
61206120
// Small buffer case: use mask to handle small writes
6121-
svbool_t mask = svwhilelt_b8(0ul, length);
6121+
svbool_t mask = svwhilelt_b8(0ull, length);
61226122
svuint8_t data = svld1_u8(mask, (unsigned char *)source);
61236123
svst1_u8(mask, (unsigned char *)target, data);
61246124
}
@@ -6143,10 +6143,10 @@ SZ_PUBLIC void sz_copy_sve(sz_ptr_t target, sz_cptr_t source, sz_size_t length)
61436143
sz_size_t body_length = length - head_length - tail_length;
61446144

61456145
// Handle unaligned parts
6146-
svbool_t head_mask = svwhilelt_b8(0ul, head_length);
6146+
svbool_t head_mask = svwhilelt_b8(0ull, head_length);
61476147
svuint8_t head_data = svld1_u8(head_mask, (unsigned char *)source);
61486148
svst1_u8(head_mask, (unsigned char *)target, head_data);
6149-
svbool_t tail_mask = svwhilelt_b8(0ul, tail_length);
6149+
svbool_t tail_mask = svwhilelt_b8(0ull, tail_length);
61506150
svuint8_t tail_data = svld1_u8(tail_mask, (unsigned char *)source + head_length + body_length);
61516151
svst1_u8(tail_mask, (unsigned char *)target + head_length + body_length, tail_data);
61526152
target += head_length;
@@ -6162,15 +6162,15 @@ SZ_PUBLIC void sz_copy_sve(sz_ptr_t target, sz_cptr_t source, sz_size_t length)
61626162
// Up to (vec_len * 2 - 1) bytes of data may be left in the body,
61636163
// so we can unroll the last two optional loop iterations.
61646164
if (body_length > vec_len) {
6165-
svbool_t mask = svwhilelt_b8(0ul, body_length);
6165+
svbool_t mask = svwhilelt_b8(0ull, body_length);
61666166
svuint8_t data = svld1_u8(mask, (unsigned char *)source);
61676167
svst1_u8(mask, (unsigned char *)target, data);
61686168
body_length -= vec_len;
61696169
source += body_length;
61706170
target += body_length;
61716171
}
61726172
if (body_length) {
6173-
svbool_t mask = svwhilelt_b8(0ul, body_length);
6173+
svbool_t mask = svwhilelt_b8(0ull, body_length);
61746174
svuint8_t data = svld1_u8(mask, (unsigned char *)source);
61756175
svst1_u8(mask, (unsigned char *)target, data);
61766176
}

scripts/bench_memory.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222
using namespace ashvardanian::stringzilla::scripts;
2323
constexpr std::size_t max_shift_length = 299;
2424

25+
/**
26+
* @brief Wraps platform-specific @b aligned memory allocation and deallocation functions.
27+
* Compatible with `std::unique_ptr` as the second template argument, to free the memory.
28+
*/
29+
struct page_alloc_and_free_t {
30+
#ifdef _WIN32
31+
inline char *operator()(std::size_t alignment, std::size_t size) const noexcept {
32+
return reinterpret_cast<char *>(_aligned_malloc(size, alignment));
33+
}
34+
inline void operator()(char *ptr) const noexcept { _aligned_free(ptr); }
35+
#else
36+
inline char *operator()(std::size_t alignment, std::size_t size) const noexcept {
37+
return reinterpret_cast<char *>(std::aligned_alloc(alignment, size));
38+
}
39+
inline void operator()(char *ptr) const noexcept { std::free(ptr); }
40+
#endif
41+
};
42+
2543
/**
2644
* @brief Benchmarks `memcpy`-like operations in 2 modes: aligned @b output buffer and unaligned.
2745
*
@@ -226,17 +244,10 @@ int main(int argc, char const **argv) {
226244
if (!SZ_DEBUG) seconds_per_benchmark *= 5;
227245

228246
// Create an aligned buffer for the output
229-
struct aligned_free_t {
230-
inline void operator()(char *ptr) const noexcept { std::free(ptr); }
231-
};
232-
std::unique_ptr<char, aligned_free_t> output_buffer;
247+
std::unique_ptr<char, page_alloc_and_free_t> output_buffer;
233248
// Add space for at least one cache line to simplify unaligned exports
234249
std::size_t const output_length = round_up_to_multiple<4096>(dataset.text.size() + max_shift_length);
235-
#ifdef _WIN32
236-
output_buffer.reset(reinterpret_cast<char *>(_aligned_malloc(output_length, 4096)));
237-
#else
238-
output_buffer.reset(reinterpret_cast<char *>(std::aligned_alloc(4096, output_length)));
239-
#endif
250+
output_buffer.reset(page_alloc_and_free_t {}(4096, output_length));
240251
if (!output_buffer) {
241252
std::fprintf(stderr, "Failed to allocate an output buffer of %zu bytes.\n", output_length);
242253
return 1;

scripts/test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,16 +1447,16 @@ void test_replacements(std::size_t lookup_tables_to_try = 128, std::size_t slice
14471447
for (std::size_t lookup_table_variation = 0; lookup_table_variation != lookup_tables_to_try;
14481448
++lookup_table_variation) {
14491449
sz::look_up_table lut;
1450-
for (std::size_t i = 0; i < 256; i++) lut[i] = (char)(std::rand() % 256);
1450+
for (std::size_t i = 0; i < 256; i++) lut[(char)i] = (char)(std::rand() % 256);
14511451

14521452
for (std::size_t slice_idx = 0; slice_idx != slices_per_table; ++slice_idx) {
1453-
std::size_t slice_offset = std::rand() % body.length();
1453+
std::size_t slice_offset = std::rand() % (body.length());
14541454
std::size_t slice_length = std::rand() % (body.length() - slice_offset);
14551455

14561456
sz::transform<char>(sz::string_view(body.data() + slice_offset, slice_length), lut,
14571457
const_cast<char *>(transformed.data()) + slice_offset);
14581458
for (std::size_t i = 0; i != slice_length; ++i) {
1459-
assert(transformed[slice_offset + i] == lut[(unsigned char)body[slice_offset + i]]);
1459+
assert(transformed[slice_offset + i] == lut[body[slice_offset + i]]);
14601460
}
14611461
}
14621462
}

0 commit comments

Comments
 (0)