templeos-info/public/Wb/Home/Src/Kernel/KMathB.HC.HTML

189 lines
16 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>F64</span><span class=cF0> </span><span class=cF5>Clamp</span><span class=cF0>(</span><span class=cF1>F64</span><span class=cF0> d,</span><span class=cF1>F64</span><span class=cF0> lo,</span><span class=cF1>F64</span><span class=cF0> hi)
<a name="l2"></a>{</span><span class=cF2>//Clamp to F64 [] range.</span><span class=cF0>
<a name="l3"></a> </span><span class=cF1>if</span><span class=cF0> (d&lt;lo)
<a name="l4"></a> </span><span class=cF1>return</span><span class=cF0> lo;
<a name="l5"></a> </span><span class=cF1>if</span><span class=cF0> (d&gt;hi)
<a name="l6"></a> </span><span class=cF1>return</span><span class=cF0> hi;
<a name="l7"></a> </span><span class=cF1>return</span><span class=cF0> d;
<a name="l8"></a>}
<a name="l9"></a>
<a name="l10"></a></span><span class=cF1>F64</span><span class=cF0> </span><span class=cF5>Min</span><span class=cF0>(</span><span class=cF1>F64</span><span class=cF0> n1,</span><span class=cF1>F64</span><span class=cF0> n2)
<a name="l11"></a>{</span><span class=cF2>//Min of two F64s.</span><span class=cF0>
<a name="l12"></a> </span><span class=cF1>if</span><span class=cF0> (n1&lt;=n2)
<a name="l13"></a> </span><span class=cF1>return</span><span class=cF0> n1;
<a name="l14"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l15"></a> </span><span class=cF1>return</span><span class=cF0> n2;
<a name="l16"></a>}
<a name="l17"></a>
<a name="l18"></a></span><span class=cF1>F64</span><span class=cF0> </span><span class=cF5>Max</span><span class=cF0>(</span><span class=cF1>F64</span><span class=cF0> n1,</span><span class=cF1>F64</span><span class=cF0> n2)
<a name="l19"></a>{</span><span class=cF2>//Max of two F64s.</span><span class=cF0>
<a name="l20"></a> </span><span class=cF1>if</span><span class=cF0> (n1&gt;=n2)
<a name="l21"></a> </span><span class=cF1>return</span><span class=cF0> n1;
<a name="l22"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l23"></a> </span><span class=cF1>return</span><span class=cF0> n2;
<a name="l24"></a>}
<a name="l25"></a>
<a name="l26"></a></span><span class=cF1>F64</span><span class=cF0> </span><span class=cF5>Pow10I64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> i)
<a name="l27"></a>{</span><span class=cF2>//F64 int powers of ten.</span><span class=cF0>
<a name="l28"></a> </span><span class=cF1>if</span><span class=cF0> (i&gt;308)
<a name="l29"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>inf</span><span class=cF0>;
<a name="l30"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (i&lt;-308)
<a name="l31"></a> </span><span class=cF1>return</span><span class=cF0> 0.0;
<a name="l32"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l33"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFD>pow10_I64</span><span class=cF0>[i+309];
<a name="l34"></a>}
<a name="l35"></a>
<a name="l36"></a></span><span class=cF9>U64</span><span class=cF0> </span><span class=cF5>FloorU64</span><span class=cF0>(</span><span class=cF9>U64</span><span class=cF0> num,</span><span class=cF9>U64</span><span class=cF0> to)
<a name="l37"></a>{</span><span class=cF2>//Int multiples of num.</span><span class=cF0>
<a name="l38"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to;
<a name="l39"></a>}
<a name="l40"></a>
<a name="l41"></a></span><span class=cF9>U64</span><span class=cF0> </span><span class=cF5>CeilU64</span><span class=cF0>(</span><span class=cF9>U64</span><span class=cF0> num,</span><span class=cF9>U64</span><span class=cF0> to)
<a name="l42"></a>{</span><span class=cF2>//Int multiples of num.</span><span class=cF0>
<a name="l43"></a> num+=to-1;
<a name="l44"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to;
<a name="l45"></a>}
<a name="l46"></a>
<a name="l47"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>RoundI64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> num,</span><span class=cF9>I64</span><span class=cF0> to)
<a name="l48"></a>{</span><span class=cF2>//Int multiples of num.</span><span class=cF0>
<a name="l49"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to;
<a name="l50"></a>}
<a name="l51"></a>
<a name="l52"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>FloorI64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> num,</span><span class=cF9>I64</span><span class=cF0> to)
<a name="l53"></a>{</span><span class=cF2>//Int multiples of num.</span><span class=cF0>
<a name="l54"></a> </span><span class=cF1>if</span><span class=cF0> (num&gt;=0)
<a name="l55"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to;
<a name="l56"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l57"></a> num++;
<a name="l58"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to-to;
<a name="l59"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l60"></a>}
<a name="l61"></a>
<a name="l62"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>CeilI64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> num,</span><span class=cF9>I64</span><span class=cF0> to)
<a name="l63"></a>{</span><span class=cF2>//Int multiples of num.</span><span class=cF0>
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (num&gt;=0) </span><span class=cF7>{</span><span class=cF0>
<a name="l65"></a> num+=to-1;
<a name="l66"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to;
<a name="l67"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l68"></a> num+=to-1;
<a name="l69"></a> </span><span class=cF1>return</span><span class=cF0> num-num%to-to;
<a name="l70"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l71"></a>}
<a name="l72"></a>
<a name="l73"></a></span><span class=cF2>//See </span><a href="/Wb/Doc/Credits.DD.HTML#l1"><span class=cF4>::/Doc/Credits.DD</span></a><span class=cF2>.</span><span class=cF0>
<a name="l74"></a>#</span><span class=cF1>define</span><span class=cF0> LIN_CONGRUE_A 6364136223846793005
<a name="l75"></a>#</span><span class=cF1>define</span><span class=cF0> LIN_CONGRUE_C 1442695040888963407
<a name="l76"></a>
<a name="l77"></a></span><span class=cF9>I16</span><span class=cF0> </span><span class=cF5>RandI16</span><span class=cF0>()
<a name="l78"></a>{</span><span class=cF2>//Random I16.</span><span class=cF0>
<a name="l79"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l80"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l81"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l82"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l83"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l84"></a> </span><span class=cF1>return</span><span class=cF0> res.i16[0];
<a name="l85"></a>}
<a name="l86"></a>
<a name="l87"></a></span><span class=cF9>U16</span><span class=cF0> </span><span class=cF5>RandU16</span><span class=cF0>()
<a name="l88"></a>{</span><span class=cF2>//Random U16.</span><span class=cF0>
<a name="l89"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l90"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l91"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l92"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l93"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l94"></a> </span><span class=cF1>return</span><span class=cF0> res.u16[0];
<a name="l95"></a>}
<a name="l96"></a>
<a name="l97"></a></span><span class=cF9>I32</span><span class=cF0> </span><span class=cF5>RandI32</span><span class=cF0>()
<a name="l98"></a>{</span><span class=cF2>//Random I32.</span><span class=cF0>
<a name="l99"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l100"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l101"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l102"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l103"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l104"></a> </span><span class=cF1>return</span><span class=cF0> res.i32[0];
<a name="l105"></a>}
<a name="l106"></a>
<a name="l107"></a></span><span class=cF9>U32</span><span class=cF0> </span><span class=cF5>RandU32</span><span class=cF0>()
<a name="l108"></a>{</span><span class=cF2>//Random U32.</span><span class=cF0>
<a name="l109"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l110"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l111"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l112"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l113"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l114"></a> </span><span class=cF1>return</span><span class=cF0> res.u32[0];
<a name="l115"></a>}
<a name="l116"></a>
<a name="l117"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>RandI64</span><span class=cF0>()
<a name="l118"></a>{</span><span class=cF2>//Random I64.</span><span class=cF0>
<a name="l119"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l120"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l121"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l122"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l123"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l124"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l125"></a>}
<a name="l126"></a>
<a name="l127"></a></span><span class=cF9>U64</span><span class=cF0> </span><span class=cF5>RandU64</span><span class=cF0>()
<a name="l128"></a>{</span><span class=cF2>//Random U64.</span><span class=cF0>
<a name="l129"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l130"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l131"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l132"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l133"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l134"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l135"></a>}
<a name="l136"></a>
<a name="l137"></a></span><span class=cF1>F64</span><span class=cF0> </span><span class=cF5>Rand</span><span class=cF0>()
<a name="l138"></a>{</span><span class=cF2>//Random F64.</span><span class=cF0>
<a name="l139"></a> </span><span class=cF9>I64</span><span class=cF0> res=</span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed;
<a name="l140"></a> res=LIN_CONGRUE_A*res^(res&amp;0xFFFFFFFF0000)&gt;&gt;16+LIN_CONGRUE_C;
<a name="l141"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cF5>Fs</span><span class=cF0>-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF7>)</span><span class=cF0>)
<a name="l142"></a> res^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l143"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;rand_seed=res;
<a name="l144"></a> </span><span class=cF1>return</span><span class=cF0> (res&amp;0x3FFFFFFFFFFFFFFF)/</span><span class=cF5>ToF64</span><span class=cF0>(0x4000000000000000);
<a name="l145"></a>}
<a name="l146"></a>
<a name="l147"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>Seed</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> seed=0,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l148"></a>{</span><span class=cF2>//Set </span><a href="/Wb/Kernel/KMathB.HC.HTML#l137"><span class=cF4>Rand</span></a><span class=cF2>() seed. Zero for timer-based.</span><span class=cF0>
<a name="l149"></a> </span><span class=cF1>if</span><span class=cF0> (!task) task=</span><span class=cF5>Fs</span><span class=cF0>;
<a name="l150"></a> </span><span class=cF1>if</span><span class=cF0> (seed) </span><span class=cF7>{</span><span class=cF0>
<a name="l151"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;task-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF0>);
<a name="l152"></a> </span><span class=cF1>return</span><span class=cF0> task-&gt;rand_seed=seed;
<a name="l153"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l154"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;task-&gt;task_flags,</span><span class=cF3>TASKf_NONTIMER_RAND</span><span class=cF0>);
<a name="l155"></a> </span><span class=cF1>return</span><span class=cF0> task-&gt;rand_seed^=</span><span class=cF5>GetTSC</span><span class=cF0>;
<a name="l156"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l157"></a>}
</span></pre></body>
</html>