Hacker News new | ask | show | jobs
by jart 601 days ago
If you don't want a hard coded table, you could always do this.

    unsigned kCastagnoli[256];

    void InitializeCrc32(unsigned table[256], unsigned polynomial) {
      unsigned d, i, r;
      for (d = 0; d < 256; ++d) {
        r = d;
        for (i = 0; i < 8; ++i)
          r = r >> 1 ^ (r & 1 ? polynomial : 0);
        table[d] = r;
      }
    }

    unsigned Castagnoli(unsigned h, unsigned long w, long n) {
      long i;
      static int once;
      if (!once) {
        InitializeCrc32(kCastagnoli, 0x82f63b78);
        once = 1;
      }
      for (i = 0; i < n; ++i) {
        h = h >> 8 ^ kCastagnoli[(h & 255) ^ (w & 255)];
        w >>= 8;
      }
      return h;
    }
That does the same thing as the crc32 instructions in x86.
1 comments

I’d probably just go ahead on the original implementation and this and use uint32_t. The submitters because you might as well be more cache friendly on 64 bit systems, and here unsigned only guarantees 16 bits. People still use AVRs and the like where sizeof unsigned == 2. Granted, you’d probably use a 16 bit table there, but the portability costs nothing and gains clarity.

This version has the caveat that it is technically not thread safe.

What if you've got a Symbolics 3600 which has 36 bit words?

Your suggestion will break on that.

We clearly want uint_least32_t.

Fair enough (though a Unisys 2200 might be a stronger example, people still use them, but neither support C99 AFAIK). But by “break” here, it's only fair to point out, if such a compiler even existed, it would have the property of definitely failing with an error at compile time for uint32_t not existing, it’s not going to compile and overflow at runtime. (And yes you will practically get a warning with the pasted code, and shame on anyone for not using -Werror, but pedantically this isn't even guaranteed sadly enough). They are completely different classes of portability defects and only one is a correctness issue.

Setting aside whether there is even a C99 compiler for anything Symbolics, or anything where uint32_t doesn’t exist (already assuming C99 or above here). Is there? Meanwhile there are a few existing supported gcc architectures where sizeof int == 2.

Pick what level of pragmatism you prefer I guess. I prefer avoiding the standard integer types unless I can comfortably fit in their guaranteed sizes. I don’t really care about obsolete machines that will never have a C99 compiler outside a novelty.

> I don’t really care about obsolete machines that will never have a C99 compiler outside a novelty.

And you know what? I don't care about people who use C on 16-bit computers, unless I'm being paid to.

I've written plenty of assembly they can consume, like https://justine.lol/sectorlisp2/

Not sure why you're so triggered. I didn't even say you were wrong or anything. I originally thought the more pressing issue is filling half a table up with 0s. It's some idle commentary on a piece of code in a comment, relax?