How do I do a 64 bits mulhi in D?

deadalnix deadalnix at gmail.com
Sun Nov 27 01:26:36 UTC 2022


Mulhi is an instruction that is effectively available on any 64 
bits CPU that multiply that effectively does the following:

```d
ulong mulhi(ulong a, ulong b) {
     return (ucent(a) * ucent(b)) >> 64;
}
```

And this is how you'd implement it in say, C++, using `__int128`.

This operation is absolutely capital to do hashing fast, as it 
allows to do a ton of shit and adds in one go. You'd ask, isn't 
that what the good old regular multiplication does too? and no, 
it isn't, because it never shift right, so high bits can never 
influence lowers ones.


More information about the Digitalmars-d mailing list