Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes847downloads
Primes.txt77 linesDownload Raw Back to MultiCore
1 2U8 *master_bitmap;3I64 prime_range,my_mp_cnt,pending;4 5U0 PrimesJob(I64 i)6{7  I64 j,k,l=Sqrt(prime_range),8        lo=i*prime_range/my_mp_cnt,9        hi=(i+1)*prime_range/my_mp_cnt,10        lo2=lo+sys_cache_line_width,11        hi2=hi-sys_cache_line_width;12  if (lo2>hi2) lo2=hi2;13  for (j=2;j<=l;j++) {14    if (!Bt(master_bitmap,j)) {15      if (j>=lo)16        k=j+j;17      else {18        k=lo-lo%j;19        if (k<lo)20          k+=j;21      }22      while (k<lo2) {23        LBts(master_bitmap,k);24        k+=j;25      }26      while (k<hi2) {27        Bts(master_bitmap,k);28        k+=j;29      }30      while (k<hi) {31        LBts(master_bitmap,k);32        k+=j;33      }34    }35  }36  lock pending--;37}38 39I64 Primes(I64 range,I64 _my_mp_cnt)40{41  I64 i,prime_cnt=0;42  F64 t0=tS,tf;43  prime_range=range;44  if (_my_mp_cnt<=mp_cnt)45    my_mp_cnt=_my_mp_cnt;46  else47    my_mp_cnt=mp_cnt;48  master_bitmap=CAlloc((prime_range+7)/8+1);49  Bts(master_bitmap,0);50  Bts(master_bitmap,1);51  pending=my_mp_cnt;52  for (i=0;i<my_mp_cnt;i++)53    JobQue(&PrimesJob,i,i);54  while (pending)55    Yield;56  tf=tS;57 58  for (i=0;i<prime_range;i++)59    if (!Bt(master_bitmap,i))60      prime_cnt++;61 62  for (i=MaxI64(prime_range-100,0);i<prime_range;i++)63    if (!Bt(master_bitmap,i))64      "%d ",i;65 66  "\n$RED$CPUs:%d PrimeRange:%,d PrimeCnt:%,d Time:%9.7,f$FG$\n",67        my_mp_cnt,prime_range,prime_cnt,tf-t0;68 69  Free(master_bitmap);70  return prime_cnt;71}72 73Primes(100,1);74Primes(100,mp_cnt);75Primes(1000000,1);76Primes(1000000,mp_cnt);77