unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
2.27k stars 143 forks source link

Fix uint32_t to uint40_t construction and cast #404

Closed Ngalstyan4 closed 6 months ago

Ngalstyan4 commented 6 months ago

Found with a test case in lantern.

Quick example:

uint32_t n32 = 42;
uint64_t n64 = 42;
uint40_t n40_from_32(n32);
uint40_t n40_from_64(n64);
std::cerr << "n40_from_32: " << std::to_string(n40_from_32) << 
                    " n40_from_64: " << std::to_string(n40_from_64)  << "\n";

This was printing "n40_from_32: 10752 n40_from_64: 42". After the patch, it prints "n40_from_32: 42 n40_from_64: 42"

ashvardanian commented 6 months ago

Oh, this is very timely, thank you! Would you by any chance have time to add a test case on top of test.cpp? Ideally for little-endian and big-endian. Thank you again 🤗

Ngalstyan4 commented 6 months ago

Added some constructor tests and a test to do byteswaps with clang and GCC intrinsics. Not ideal, so feel free to change if you have other ideas!

ashvardanian commented 6 months ago

Thank you, @Ngalstyan4! Will merge later today 🤗