Created
September 24, 2022 06:43
-
-
Save skmp/60895b2e352ed204e434eb7128e936d8 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
// ./a.out | aplay -r 256000 ? | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <x86intrin.h> | |
#include <atomic> | |
#include <unistd.h> | |
#include <math.h> | |
std::atomic<uint8_t> caches[128 * 512 * 64]; | |
int main(int argc) { | |
long double avg = 0; | |
auto tries = 1000UL; | |
long double slack = 0.01; | |
for(;;) { | |
long double min = 100000; | |
long double max = 0; | |
unsigned line = 0; | |
auto old_tries = tries; | |
auto new_tries = 0; | |
tries = 0; | |
for (;;) | |
{ | |
line = line * 1 + line * 9 + line * 23 + (line / 7) * 314 ^ 0xDEFACE; | |
auto index = line % (sizeof(caches)/64); | |
_mm_mfence(); | |
auto earlier = __rdtsc(); | |
caches[index * 64 + 11]+= earlier; | |
_mm_mfence(); | |
auto later = __rdtsc(); | |
auto diff = later - earlier; | |
avg = avg + avg / (diff / 11.0) + diff; | |
avg = avg / (1 + 1.0/11 + 1); | |
max = max < avg? (max * 0.9 + avg * 0.1) : max*0.9999; | |
min = min > avg? (min * 0.9 + avg * 0.1) : min*1.0001; | |
auto i = abs(avg / max - 1); | |
auto q = abs(avg / min - 1); | |
tries++; | |
new_tries++; | |
if (new_tries > old_tries) { | |
new_tries -= old_tries; | |
slack *= 1.1; | |
//printf("RT: %p, %.8Lf i %.8Lf q %.8Lf\n", (void*)tries, slack, i, q); | |
} | |
slack = slack > 1? 0.99 : slack; | |
auto test = slack * slack * slack * slack; | |
if (i < test && q < test) { | |
if (new_tries == tries) { | |
slack = slack / 1.1; | |
} | |
if (argc==1) | |
printf("TM: %p avg: %.4Lf i:%.6Lf q:%.6Lf tries:%p slack: %.2Lf\n", (void*)later, avg, i, q, (void*)tries, slack); | |
else { | |
for (unsigned r = 0; r < (300 / avg); r++) | |
for (unsigned s = 0; s < avg; s++) { | |
auto phase = s / avg * 2 * 3.1415; | |
auto val = ((1+ sin(phase)) / 2 * q + i) / 2; | |
unsigned u = 128 * val; | |
u = u > 255? 255: u; | |
write(1, &u, 1); | |
} | |
} | |
usleep(1250); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment