Skip to content

Instantly share code, notes, and snippets.

@ldmsys
Created June 14, 2023 05:14
Show Gist options
  • Save ldmsys/10f980df5048141ddbdb6c2884cfaa0c to your computer and use it in GitHub Desktop.
Save ldmsys/10f980df5048141ddbdb6c2884cfaa0c to your computer and use it in GitHub Desktop.
Get __uint128_t integer from UUID string (tested on clang-1403.0.22.14.1)
__uint128_t get128FromUUID(const char* uuid) {
__uint128_t result = 0;
unsigned char currentData, dashcnt = 0;
if(uuid[36] != 0x00)
return (__uint128_t)0xf << 124; // length mismatch
for(unsigned char i=0;i<36;i++) {
if(uuid[i] >= 0x30 && uuid[i] <= 0x39) currentData = uuid[i] - 0x30; // Numberic digit
else if(uuid[i] >= 0x41 && uuid[i] <= 0x46) currentData = 10 + uuid[i] - 0x41; // Capital letter digit
else if(uuid[i] >= 0x61 && uuid[i] <= 0x66) currentData = 10 + uuid[i] - 0x61; // small letter digit
else if(uuid[i] == '-') {dashcnt++; continue;} // dash
else continue; // undefined behavior
result |= ((__uint128_t)currentData) << 4*(31-i+dashcnt);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment