Comment

Comment by ridiculous_fish

It is possible to coax that codegen out of clang. The trick is that you need the high half of the product, so you must use a larger type, in this case __uint128_t:

    uint64_t div10(uint64_t y) {
        const uint64_t magic = 0xCCCCCCCCCCCCCCCDULL;
        __uint128_t prod = magic * (__uint128_t)y;
        return (uint64_t)(prod >> (64 + 3));
    }
See http://libdivide.com for how to get this codegen with runtime constants (I am the author).

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton