Created
May 14, 2025 11:35
-
-
Save apangin/9f9c401f9e93daf90a4c620ac5a8c8e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
typedef unsigned int u32; | |
typedef unsigned long long u64; | |
#define N 1000000 | |
static u64 dataset[N]; | |
static inline u64 rdtsc() { | |
#if defined(__x86_64__) | |
u32 lo, hi; | |
asm volatile("rdtsc" : "=a" (lo), "=d" (hi)); | |
return ((u64)hi << 32) | lo; | |
#elif defined(__aarch64__) | |
u64 value; | |
asm volatile("mrs %0, cntvct_el0" : "=r"(value)); | |
return value; | |
#endif | |
} | |
static size_t francesco(u64 value) { | |
if (value <= 0x7F) return 1; | |
if (value <= 0x3FFF) return 2; | |
if (value <= 0x1FFFFF) return 3; | |
if (value <= 0xFFFFFFF) return 4; | |
if (value <= 0x7FFFFFFFF) return 5; | |
if (value <= 0x3FFFFFFFFFF) return 6; | |
if (value <= 0x1FFFFFFFFFFFF) return 7; | |
if (value <= 0xFFFFFFFFFFFFFF) return 8; | |
if (value <= 0x7FFFFFFFFFFFFFFF) return 9; | |
return 10; | |
} | |
static size_t andrei(u64 value) { | |
return (640 - __builtin_clzll(value | 1) * 9) / 64; | |
} | |
static size_t bara(u64 value) { | |
size_t size = 1; | |
while ((value = value >> 7)) { | |
size++; | |
} | |
return size; | |
} | |
int main() { | |
for (int i = 0; i < N; i++) { | |
dataset[i] = (u64)(rand() & 0xff) << (rand() & 63); | |
} | |
u64 sink = 0; | |
u64 before = rdtsc(); | |
for (int i = 0; i < N; ++i) sink += francesco(dataset[i]); | |
u64 after = rdtsc(); | |
printf("Francesco: %08llu\n", after - before); | |
before = rdtsc(); | |
for (int i = 0; i < N; ++i) sink += andrei(dataset[i]); | |
after = rdtsc(); | |
printf("Andrei : %08llu\n", after - before); | |
before = rdtsc(); | |
for (int i = 0; i < N; ++i) sink += bara(dataset[i]); | |
after = rdtsc(); | |
printf("Bara : %08llu\n", after - before); | |
printf("Sink ignore me: %llu\n", sink); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment