128 lines
8.1 KiB
Plaintext
128 lines
8.1 KiB
Plaintext
|
<!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=cF2>/*"Fixed point" means you use ints</span><span class=cF0>
|
||
|
<a name="l2"></a></span><span class=cF2>that are scaled by a value. A common</span><span class=cF0>
|
||
|
<a name="l3"></a></span><span class=cF2>example would be using number of pennies</span><span class=cF0>
|
||
|
<a name="l4"></a></span><span class=cF2>instead of dollars with a float.</span><span class=cF0>
|
||
|
<a name="l5"></a>
|
||
|
<a name="l6"></a></span><span class=cF2>Fixed-point used to be much </span><a href="http://en.wikipedia.org/wiki/X87"><span class=cF2>faster</span></a><span class=cF2>,</span><span class=cF0>
|
||
|
<a name="l7"></a></span><span class=cF2>but modern processors do well with</span><span class=cF0>
|
||
|
<a name="l8"></a></span><span class=cF2>floats. It also depends on the compiler</span><span class=cF0>
|
||
|
<a name="l9"></a></span><span class=cF2>and my compiler is poor with floats.</span><span class=cF0>
|
||
|
<a name="l10"></a>
|
||
|
<a name="l11"></a></span><span class=cF2>I often use 64-bit ints with upper 32-bits</span><span class=cF0>
|
||
|
<a name="l12"></a></span><span class=cF2>as int and lower 32-bits as fraction.</span><span class=cF0>
|
||
|
<a name="l13"></a>
|
||
|
<a name="l14"></a></span><span class=cF2>See </span><a href="/Wb/Demo/SubIntAccess.HC.HTML#l1"><span class=cF4>::/Demo/SubIntAccess.HC</span></a><span class=cF2> for how</span><span class=cF0>
|
||
|
<a name="l15"></a></span><span class=cF2>to access upper or lower 32-bits.</span><span class=cF0>
|
||
|
<a name="l16"></a>
|
||
|
<a name="l17"></a></span><span class=cF2>For a complete lst of nonstandard compiler</span><span class=cF0>
|
||
|
<a name="l18"></a></span><span class=cF2>features, see </span><a href="/Wb/Doc/HolyC.DD.HTML#l1"><span class=cF4>::/Doc/HolyC.DD</span></a><span class=cF2>.</span><span class=cF0>
|
||
|
<a name="l19"></a></span><span class=cF2>*/</span><span class=cF0>
|
||
|
<a name="l20"></a>
|
||
|
<a name="l21"></a></span><span class=cF1>U0</span><span class=cF0> Main()
|
||
|
<a name="l22"></a>{
|
||
|
<a name="l23"></a> </span><span class=cF1>F64</span><span class=cF0> t0,f_sum=0,f_val;
|
||
|
<a name="l24"></a> </span><span class=cF9>I64</span><span class=cF0> i ,i_sum=0,i_val;
|
||
|
<a name="l25"></a>
|
||
|
<a name="l26"></a> i_val= 2.0000002 *0x100000000;
|
||
|
<a name="l27"></a> t0=</span><span class=cF5>tS</span><span class=cF0>;
|
||
|
<a name="l28"></a> </span><span class=cF1>for</span><span class=cF0> (i=1000000000;i;i--)
|
||
|
<a name="l29"></a> i_sum+=i_val;
|
||
|
<a name="l30"></a> </span><span class=cF6>"Int Sum\t\t:%.9f Time:%7.3fs\n"</span><span class=cF0>,i_sum/</span><span class=cF5>ToF64</span><span class=cF0>(0x100000000),</span><span class=cF5>tS</span><span class=cF0>-t0;
|
||
|
<a name="l31"></a>
|
||
|
<a name="l32"></a> f_val= 2.0000002;
|
||
|
<a name="l33"></a> t0=</span><span class=cF5>tS</span><span class=cF0>;
|
||
|
<a name="l34"></a> </span><span class=cF1>for</span><span class=cF0> (i=1000000000;i;i--)
|
||
|
<a name="l35"></a> f_sum+=f_val;
|
||
|
<a name="l36"></a> </span><span class=cF6>"Float Sum\t:%.9f Time:%7.3fs\n"</span><span class=cF0>,f_sum,</span><span class=cF5>tS</span><span class=cF0>-t0;
|
||
|
<a name="l37"></a> </span><span class=cF6>'\n'</span><span class=cF0>;
|
||
|
<a name="l38"></a>}
|
||
|
<a name="l39"></a>
|
||
|
<a name="l40"></a></span><span class=cF1>U0</span><span class=cF0> DoIt2()
|
||
|
<a name="l41"></a>{
|
||
|
<a name="l42"></a> </span><span class=cF9>I64</span><span class=cF0> i=0x123456789ABCDEF0;
|
||
|
<a name="l43"></a> </span><span class=cF6>"i\t\t=%X\n"</span><span class=cF0>,i;
|
||
|
<a name="l44"></a>
|
||
|
<a name="l45"></a> </span><span class=cF6>"i&0xFFFFFFFF\t=%X\n"</span><span class=cF0>,i&0xFFFFFFFF;
|
||
|
<a name="l46"></a> </span><span class=cF6>"i>>32\t\t=%X\n"</span><span class=cF0>,i>>32;
|
||
|
<a name="l47"></a>
|
||
|
<a name="l48"></a> </span><span class=cF2>/* Standard int types are declared</span><span class=cF0>
|
||
|
<a name="l49"></a></span><span class=cF2>with a special compiler feature which</span><span class=cF0>
|
||
|
<a name="l50"></a></span><span class=cF2>allows a structure to be accessed as a</span><span class=cF0>
|
||
|
<a name="l51"></a></span><span class=cF2>whole. That's why the i variable can</span><span class=cF0>
|
||
|
<a name="l52"></a></span><span class=cF2>be accessed normally in addition to</span><span class=cF0>
|
||
|
<a name="l53"></a></span><span class=cF2>structure member access </span><a href="/Wb/Kernel/KernelA.HH.HTML#l105"><span class=cF4>I64</span></a><span class=cF2>. The actual</span><span class=cF0>
|
||
|
<a name="l54"></a></span><span class=cF2>intrinsic compiler type is U64i.</span><span class=cF0>
|
||
|
<a name="l55"></a>
|
||
|
<a name="l56"></a></span><span class=cF2>public U64i union I64</span><span class=cF0>
|
||
|
<a name="l57"></a></span><span class=cF2>{</span><span class=cF0>
|
||
|
<a name="l58"></a></span><span class=cF2> I8i i8[8];</span><span class=cF0>
|
||
|
<a name="l59"></a></span><span class=cF2> U8i u8[8];</span><span class=cF0>
|
||
|
<a name="l60"></a></span><span class=cF2> I16 i16[4];</span><span class=cF0>
|
||
|
<a name="l61"></a></span><span class=cF2> U16 u16[4];</span><span class=cF0>
|
||
|
<a name="l62"></a></span><span class=cF2> I32 i32[2];</span><span class=cF0>
|
||
|
<a name="l63"></a></span><span class=cF2> U32 u32[2];</span><span class=cF0>
|
||
|
<a name="l64"></a></span><span class=cF2>};</span><span class=cF0>
|
||
|
<a name="l65"></a>
|
||
|
<a name="l66"></a></span><span class=cF2>It's not quite as great as it seems</span><span class=cF0>
|
||
|
<a name="l67"></a></span><span class=cF2>because the compiler decides it cannot</span><span class=cF0>
|
||
|
<a name="l68"></a></span><span class=cF2>place i into a reg, so there is a</span><span class=cF0>
|
||
|
<a name="l69"></a></span><span class=cF2>penalty.</span><span class=cF0>
|
||
|
<a name="l70"></a>
|
||
|
<a name="l71"></a></span><span class=cF2>For a complete lst of nonstandard compiler</span><span class=cF0>
|
||
|
<a name="l72"></a></span><span class=cF2>features, see </span><a href="/Wb/Doc/HolyC.DD.HTML#l1"><span class=cF4>::/Doc/HolyC.DD</span></a><span class=cF2>.</span><span class=cF0>
|
||
|
<a name="l73"></a></span><span class=cF2>*/</span><span class=cF0>
|
||
|
<a name="l74"></a>
|
||
|
<a name="l75"></a> </span><span class=cF6>"i.u32[0]\t=%X\n"</span><span class=cF0>,i.u32[0];
|
||
|
<a name="l76"></a> </span><span class=cF6>"i.u32[1]\t=%X\n"</span><span class=cF0>,i.u32[1];
|
||
|
<a name="l77"></a>}
|
||
|
<a name="l78"></a>
|
||
|
<a name="l79"></a></span><span class=cF5>CPURep</span><span class=cF0>;
|
||
|
<a name="l80"></a>Main;
|
||
|
<a name="l81"></a>DoIt2;
|
||
|
<a name="l82"></a>
|
||
|
<a name="l83"></a></span><span class=cF2>//See </span><a href="/Wb/Demo/Lectures/FixedPointAdvanced.HC.HTML#l1"><span class=cF4>::/Demo/Lectures/FixedPointAdvanced.HC</span></a><span class=cF0>
|
||
|
<a name="l84"></a>
|
||
|
<a name="l85"></a></span><span class=cF2>/*Program Output</span><span class=cF1>
|
||
|
<a name="l86"></a>
|
||
|
<a name="l87"></a>16 Cores 3.500GHz
|
||
|
<a name="l88"></a>Int Sum :2000000199.768690240 Time: 0.803s
|
||
|
<a name="l89"></a>Float Sum :2000000225.656127040 Time: 3.615s
|
||
|
<a name="l90"></a>
|
||
|
<a name="l91"></a>i =123456789ABCDEF0
|
||
|
<a name="l92"></a>i&0xFFFFFFFF =9ABCDEF0
|
||
|
<a name="l93"></a>i>>32 =12345678
|
||
|
<a name="l94"></a>i.u32[0] =9ABCDEF0
|
||
|
<a name="l95"></a>i.u32[1] =12345678
|
||
|
<a name="l96"></a></span><span class=cF2>*/</span><span class=cF1>
|
||
|
</span></pre></body>
|
||
|
</html>
|