Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Last active July 9, 2023 01:05
Show Gist options
  • Save tkoz0/f9ccc791874e6a9bbbd918aafbabc92b to your computer and use it in GitHub Desktop.
Save tkoz0/f9ccc791874e6a9bbbd918aafbabc92b to your computer and use it in GitHub Desktop.
64 bit number modulo mersenne number (M61, 2^61-1) compiles with no multiplication or division with gcc 9.4 -O3
#include <stdint.h>
#define MP(p) ((1uLL << p) - 1)
uint64_t mod_m61(uint64_t a)
{
uint64_t tmp = (a & MP(61)) + (a >> 61);
if (tmp >= MP(61))
tmp -= MP(61);
return tmp;
}
// a simple (a % MP(61)) compiles with a multiplication and longer instruction sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment