rendered paste body#include <inttypes.h>#include <stdio.h>#include <stddef.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <time.h>#define MY_CLOCKTYPE CLOCK_MONOTONIC_RAWstatic inline uint64_t gettime_dif(struct timespec tm_prev){ struct timespec tm; clock_gettime(MY_CLOCKTYPE, &tm); return ((uint64_t)tm.tv_sec * 1000000000ull + tm.tv_nsec)-((uint64_t)tm_prev.tv_sec * 1000000000ull + tm_prev.tv_nsec);}#ifdef __i386inline uint64_t rdtsc() { uint64_t x; __asm__ volatile ("rdtsc" : "=A" (x)); return x;}#elif __amd64inline uint64_t rdtsc() { uint64_t a, d; __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); return (d<<32) | a;}#endifstatic inline uint64_t f1(uint8_t Imm){ static const uint32_t lookup[16] = { 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff, 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff, 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff }; return lookup[Imm & 0x0f] | ((uint64_t)lookup[Imm >> 4] << 32);}static inline uint64_t f2(uint8_t Imm){ uint64_t EncVal = 0; if (Imm & 0x80) EncVal |= 0xff00000000000000ULL; if (Imm & 0x40) EncVal |= 0x00ff000000000000ULL; if (Imm & 0x20) EncVal |= 0x0000ff0000000000ULL; if (Imm & 0x10) EncVal |= 0x000000ff00000000ULL; if (Imm & 0x08) EncVal |= 0x00000000ff000000ULL; if (Imm & 0x04) EncVal |= 0x0000000000ff0000ULL; if (Imm & 0x02) EncVal |= 0x000000000000ff00ULL; if (Imm & 0x01) EncVal |= 0x00000000000000ffULL; return EncVal;}#define SZARR 300#define NUMTEST 200uint8_t buf1[SZARR];uint64_t buf2[SZARR];int main(void){ int randomData = open("/dev/urandom", O_RDONLY); if (randomData == 0) return -1; if (sizeof(buf1) != read(randomData, buf1, sizeof(buf1))) return -1; uint64_t times[4][NUMTEST]; for(size_t i = 0; i < NUMTEST; i += 1) { struct timespec tmp; clock_gettime(MY_CLOCKTYPE, &tmp); uint64_t tmp2 = rdtsc(); for(size_t j = 0; j < SZARR; j++) { buf2[j] = f1(buf1[j]); } asm volatile ("" : : : "memory"); times[1][i] = rdtsc() - tmp2; times[0][i] = gettime_dif(tmp); } for(size_t i = 0; i < NUMTEST; i += 1) { struct timespec tmp; clock_gettime(MY_CLOCKTYPE, &tmp); uint64_t tmp2 = rdtsc(); for(size_t j = 0; j < SZARR; j++) { buf2[j] = f2(buf1[j]); } asm volatile ("" : : : "memory"); times[3][i] = rdtsc() - tmp2; times[2][i] = gettime_dif(tmp); } printf("\n(gettime()/rdtsc())\nf1\tf2\n"); for(size_t i = 0; i < NUMTEST; i += 2) { printf("%f\t%f\n", (double)times[0][i]/(double)times[1][i], (double)times[2][i]/(double)times[3][i]); } printf("\n\nf1\t\t\tf2\n(gett)\t(rdtsc)\t\t(gett)\t(rdtsc)\n"); for(size_t i = 0; i < NUMTEST; i += 2) { printf("%" PRIu64 "\t%" PRIu64 "\t\t%" PRIu64 "\t%" PRIu64 "\n", times[0][i], times[1][i], times[2][i], times[3][i]); } return 0;}