Advertisement
Guest User

Untitled

a guest
Apr 19th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <thread>
  4. #include <atomic>
  5. #include <chrono>
  6.  
  7. std::atomic<long> A, B, C;
  8. int popcount(__uint128_t x) {
  9.     return __builtin_popcountll(x>>64) + __builtin_popcountll(x);
  10. }
  11. void f() {
  12.     FILE* fp = fopen("/dev/urandom", "rb");
  13.     __uint128_t r = 0;
  14.     const __uint128_t MASK = ((__uint128_t)1<<100)-1;
  15.     while (1) {
  16.         fread(&r, 13, 1, fp);
  17.         int a = popcount(r & r>>1 & MASK);
  18.         int b = popcount(~r & ~r>>2 & MASK);
  19.         //printf ("%d %d %016llX %016llX", a,b, r); exit(0);
  20.         ++C; A+=a>b; B+=b>a;
  21.     }
  22. }
  23. int main() {
  24.     for (int i=0; i<1; ++i) {
  25.         std::thread(f).detach();
  26.     }
  27.     puts("          Tries "
  28.          "          Alice "
  29.          "          Bob");
  30.     while (1) {
  31.         using namespace std::chrono;
  32.         std::this_thread::sleep_for(1s);
  33.         printf("%16ld%16ld%16ld%16f%16f\n",
  34.             (long)C, (long)A, (long)B, A/(double)C, B/(double)C);
  35.     }
  36. }
  37. /*
  38. /tmp$clang++ -O2 -march=native ace.cpp && ./a.out
  39.           Tries           Alice           Bob
  40.         16094129         7754735         7735217        0.481836        0.480624
  41.         32559419        15687511        15648598        0.481812        0.480617
  42.         49981597        24078565        24025584        0.481749        0.480689
  43.         67558478        32544818        32475877        0.481728        0.480708
  44.         84934340        40916597        40826653        0.481744        0.480685
  45.        102541469        49401059        49288415        0.481767        0.480668
  46.        120146039        57881533        57750303        0.481760        0.480668
  47.        137669395        66320196        66176514        0.481735        0.480692
  48.        155200340        74764887        74603746        0.481731        0.480693
  49.        172655724        83174404        82993198        0.481736        0.480686
  50.        190123706        91588181        91390641        0.481729        0.480690
  51.        207559197        99986660        99772391        0.481726        0.480694
  52.        225032664       108405140       108168940        0.481731        0.480681
  53.        242568649       116851786       116600393        0.481727        0.480690
  54.        260095684       125293907       125027412        0.481722        0.480698
  55.        277674456       133760597       133478529        0.481717        0.480702
  56.        295167942       142187855       141888559        0.481718        0.480705
  57.  
  58. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement