Skip to content

Instantly share code, notes, and snippets.

@thisismiller
Created August 11, 2017 07:08
Show Gist options
  • Select an option

  • Save thisismiller/717ef49ae33c26df89b47af2e25ffe43 to your computer and use it in GitHub Desktop.

Select an option

Save thisismiller/717ef49ae33c26df89b47af2e25ffe43 to your computer and use it in GitHub Desktop.
C file for stoke
#include <stdint.h>
#include <endian.h>
#include <stdlib.h>
unsigned __int128 ntohlll(unsigned __int128 n) {
struct pieces {
uint64_t a;
uint64_t b;
} *p = (struct pieces*)&n;
return ((unsigned __int128)(be64toh(p->b))) << 64 | be64toh(p->a);
}
unsigned __int128 htonlll(unsigned __int128 n) {
return ntohlll(n);
}
void __attribute__((noinline)) dbl(unsigned __int128 *ptr) {
unsigned __int128 n = ntohlll(*ptr);
unsigned __int128 m = (n << 1) ^ (n & ((((unsigned __int128)1)<<127) ? 0x87 : 0x00));
*ptr = htonlll(m);
}
//void __attribute__((noinline)) dbl(unsigned __int128 *ptr) {
// unsigned __int128 n = ntohlll(*ptr);
// unsigned __int128 m = (n << 1) ^ (n >> 127) * 0b10000111;
// *ptr = htonlll(m);
//}
unsigned int reverseBits(unsigned int num)
{
unsigned int count = sizeof(num) * 8 - 1;
unsigned int reverse_num = num;
num >>= 1;
while(num)
{
reverse_num <<= 1;
reverse_num |= num & 1;
num >>= 1;
count--;
}
reverse_num <<= count;
return reverse_num;
}
int main(int argc, char** argv) {
int itr = atoi(argv[1]);
unsigned __int128 n;
for (int i = 0 ; i < itr; i++) {
uint32_t j = reverseBits(i);
n = ((unsigned __int128)j) << (32*3) | ((unsigned __int128)j) << (32*2) | ((unsigned __int128)j) << (32*1) | ((unsigned __int128)j);
dbl(&n);
}
return (uint8_t)n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment