CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
KMathB.txt159 linesDownload Raw Back to Kernel
1 2F64 Clamp(F64 d,F64 lo,F64 hi)3{//Clamp to F64 [] range.4  if (d<lo)5    return lo;6  if (d>hi)7    return hi;8  return d;9}10 11F64 Min(F64 n1,F64 n2)12{//Min of two F64s.13  if (n1<=n2)14    return n1;15  else16    return n2;17}18 19F64 Max(F64 n1,F64 n2)20{//Max of two F64s.21  if (n1>=n2)22    return n1;23  else24    return n2;25}26 27F64 Pow10I64(I64 i)28{//F64 int powers of ten.29  if (i>308)30    return inf;31  else if (i<-308)32    return 0.0;33  else34    return pow10_I64[i+309];35}36 37U64 FloorU64(U64 num,U64 to)38{//Int multiples of num.39  return num-num%to;40}41 42U64 CeilU64(U64 num,U64 to)43{//Int multiples of num.44  num+=to-1;45  return num-num%to;46}47 48I64 RoundI64(I64 num,I64 to)49{//Int multiples of num.50  return num-num%to;51}52 53I64 FloorI64(I64 num,I64 to)54{//Int multiples of num.55  if (num>=0)56    return num-num%to;57  else {58    num++;59    return num-num%to-to;60  }61}62 63I64 CeilI64(I64 num,I64 to)64{//Int multiples of num.65  if (num>=0) {66    num+=to-1;67    return num-num%to;68  } else {69    num+=to-1;70    return num-num%to-to;71  }72}73 74//See ::/Doc/Credits.DD.75#define LIN_CONGRUE_A   636413622384679300576#define LIN_CONGRUE_C   144269504088896340777 78I16 RandI16()79{//Random I16.80  I64 res=Fs->rand_seed;81  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;82  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))83    res^=GetTSC;84  Fs->rand_seed=res;85  return res.i16[0];86}87 88U16 RandU16()89{//Random U16.90  I64 res=Fs->rand_seed;91  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;92  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))93    res^=GetTSC;94  Fs->rand_seed=res;95  return res.u16[0];96}97 98I32 RandI32()99{//Random I32.100  I64 res=Fs->rand_seed;101  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;102  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))103    res^=GetTSC;104  Fs->rand_seed=res;105  return res.i32[0];106}107 108U32 RandU32()109{//Random U32.110  I64 res=Fs->rand_seed;111  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;112  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))113    res^=GetTSC;114  Fs->rand_seed=res;115  return res.u32[0];116}117 118I64 RandI64()119{//Random I64.120  I64 res=Fs->rand_seed;121  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;122  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))123    res^=GetTSC;124  Fs->rand_seed=res;125  return res;126}127 128U64 RandU64()129{//Random U64.130  I64 res=Fs->rand_seed;131  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;132  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))133    res^=GetTSC;134  Fs->rand_seed=res;135  return res;136}137 138F64 Rand()139{//Random F64.140  I64 res=Fs->rand_seed;141  res=LIN_CONGRUE_A*res^(res&0xFFFFFFFF0000)>>16+LIN_CONGRUE_C;142  if (!Bt(&Fs->task_flags,TASKf_NONTIMER_RAND))143    res^=GetTSC;144  Fs->rand_seed=res;145  return (res&0x3FFFFFFFFFFFFFFF)/ToF64(0x4000000000000000);146}147 148I64 Seed(I64 seed=0,CTask *task=NULL)149{//Set Rand() seed. Zero for timer-based.150  if (!task) task=Fs;151  if (seed) {152    LBts(&task->task_flags,TASKf_NONTIMER_RAND);153    return task->rand_seed=seed;154  } else {155    LBtr(&task->task_flags,TASKf_NONTIMER_RAND);156    return task->rand_seed^=GetTSC;157  }158}159