1

I have the following code which implements the adler32 checksum:

constexpr uint32_t adler32(std::string_view sv)
{
    constexpr const uint32_t MOD_ADLER= 65521;
    uint32_t rv= 0, a= 1, b= 0;
    for (unsigned char c:sv)
    {
        a= (a+c)%MOD_ADLER;
        b= (b+a)%MOD_ADLER;
    }
    rv= a|(b<<16);
    return rv;
}

//----------------------------------------------------
constexpr uint16_t operator ""_csum(const char* str,long unsigned len)
{
    return adler32(std::string_view(str,len));
}

and the following test routine:

#include "adler32.h"
using easyUtils::operator""_csum;

#include <iostream>
using namespace std;

int main()
{
    auto i= easyUtils::adler32("hello");
    auto j= "hello"_csum;
    auto k= easyUtils::adler32("hello");
    cout << i << '\t' << j  << '\t' << k << endl;
    return 0;
}

Which gives the following output when compiled for std=c++17 using either clang or g++ under Linux:

./test/adlerTest
103547413   533 103547413

I would have expected 103547413 three times. Any ideas why this is not so?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Michael Surette
  • 701
  • 1
  • 4
  • 12

1 Answers1

5
constexpr uint16_t operator ""_csum
              ^^

And

103547413L % 65536L == 533L
iBug
  • 35,554
  • 7
  • 89
  • 134