Skip to content

Instantly share code, notes, and snippets.

@louisswarren

louisswarren/word.c

Last active Mar 17, 2021
Embed
What would you like to do?
That's 65% more word per word
#include <stdint.h>
#include <stdio.h>
uint64_t
strtoword(char *s)
{
uint64_t word = 0;
word |= ((uint64_t) *(s++) & 0x1f) << 55;
word |= ((uint64_t) *(s++) & 0x1f) << 50;
word |= ((uint64_t) *(s++) & 0x1f) << 45;
word |= ((uint64_t) *(s++) & 0x1f) << 40;
word |= ((uint64_t) *(s++) & 0x1f) << 35;
word |= ((uint64_t) *(s++) & 0x1f) << 30;
word |= ((uint64_t) *(s++) & 0x1f) << 25;
word |= ((uint64_t) *(s++) & 0x1f) << 20;
word |= ((uint64_t) *(s++) & 0x1f) << 15;
word |= ((uint64_t) *(s++) & 0x1f) << 10;
word |= ((uint64_t) *(s++) & 0x1f) << 5;
word |= ((uint64_t) *(s++) & 0x1f) << 0;
return word;
}
void
wordtostr(uint64_t word, char *s)
{
*(s++) = 0x60 | (word >> 55) & 0x1f;
*(s++) = 0x60 | (word >> 50) & 0x1f;
*(s++) = 0x60 | (word >> 45) & 0x1f;
*(s++) = 0x60 | (word >> 40) & 0x1f;
*(s++) = 0x60 | (word >> 35) & 0x1f;
*(s++) = 0x60 | (word >> 30) & 0x1f;
*(s++) = 0x60 | (word >> 25) & 0x1f;
*(s++) = 0x60 | (word >> 20) & 0x1f;
*(s++) = 0x60 | (word >> 15) & 0x1f;
*(s++) = 0x60 | (word >> 10) & 0x1f;
*(s++) = 0x60 | (word >> 5) & 0x1f;
*(s++) = 0x60 | (word >> 0) & 0x1f;
*s = 0;
}
int
main(void)
{
char test[13] = "equivalences";
char out[13];
uint64_t word = strtoword(test);
wordtostr(word, out);
printf("%s => %x => %s\n", test, word, out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment