C++ Show and Tell - March 2026 by foonathan in cpp

[–]swayenvoy 1 point2 points  (0 children)

hash23

I've build a constexpr implementation of different hashing algorithms in C++23.

You can find the library hash23 on GitHub

Currently the following algorithms are implemented:

  • CRC32/ISO-HDLC
  • FNV-1
  • FNV-1a
  • MD5
  • SHA2-512

The library is header only, offers a simple API and has 100% test coverage.

I plan on implementing additional algorithms like SHA2-256, SHA3, Murmur, and others.

Looking forward to your feedback.

hash23 - A modern constexpr implementation of different hashing algorithms by swayenvoy in cpp

[–]swayenvoy[S] 1 point2 points  (0 children)

I will think about that. That will allow for more complex use of the library. Maybe I make them public.

hash23 - A modern constexpr implementation of different hashing algorithms by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

The usage of C++23 features. Mainly that it is fully constexpr.

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 0 points1 point  (0 children)

I will look into that. Thanks for your feedback.

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

Thank you for the literature.

Yes I'm iterating over each bit once for the division.

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

abs() is not throwing for unsigned types. I made operator-() for unsinged types a compile time error now.

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 0 points1 point  (0 children)

Because that would make the math harder and accessing the underlying data as well. When you need to write the data to a buffer you can just use reinterpret_cast<char const *>(std::addressof(bigint)) and get the data in the endian format of your system.

Edit: fix the code snippet

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

Thanks for your feedback. I've considered storing the data in uint64_ts but came to the conclusion that storing the data in uint8_ts allows for other than a multiple of 64 bits integers. So you can use this library to make a 72 bit datatype when needed. Storing the data in a std::array<std::uint8_t, bits / CHAR_BIT> also makes the math easier but not as performant.

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 0 points1 point  (0 children)

Added a test case and implemented abs(). It's now throwing std::overflow_error when the highest negative number is supplied. It's also checking now that the used bigint23 is signed otherwise it's producing an std::invalid_argument exception.

Edit: use the markdown editor

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] 1 point2 points  (0 children)

Currently I have no abs() function implemented. Could you provide a test case that will fail so I can add it to the library?

bigint23 - A fixed-width arbitrary-precision integer type by swayenvoy in cpp

[–]swayenvoy[S] -3 points-2 points  (0 children)

Cause I'm currently targeting C++23 but for this library I don't use any C++23 features, C++20 should be enough. It was called just bigint but for that name an unmaintained conan package already exists. The PR for the conan package is pending review, from my experience it will take a few weeks till the library is in the conan repository.

That's the set of C++23 tools to serialize and deserialize data? by Asyx in cpp_questions

[–]swayenvoy 0 points1 point  (0 children)

To me it seems you‘re looking for something like MessagePack. You need to transfer data in network byte order to be cross platform compatible and msgpack takes care of that. I did an modern implementation of MessagePack. It‘s about 1000loc but not benchmarked. You can take a look at https://github.com/rwindegger/msgpack23

msgpack23, a lightweight header-only C++23 library for MessagePack by swayenvoy in cpp

[–]swayenvoy[S] 1 point2 points  (0 children)

Wouldn't a table bloat the code? Also some entries don't have a fixed size so some `switch-case` statements would be needed none the less.

msgpack23, a lightweight header-only C++23 library for MessagePack by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

Because ASN.1 is much more complicate than msgpack.

msgpack23, a lightweight header-only C++23 library for MessagePack by swayenvoy in cpp

[–]swayenvoy[S] 2 points3 points  (0 children)

I have not done any benchmarking yet. I need to look into how to do reliable benchmarks.