source: OGRE/trunk/ogrenew/Docs/api/html/OgreBitwise_8h-source.html @ 657

Revision 657, 22.9 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

Line 
1<html>
2<head>
3<title>OgreBitwise.h Source File - OGRE Documentation</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4<link type="text/css" rel="stylesheet" href="style.css">
5</head>
6
7<body>
8<!-- Generated by Doxygen 1.3.6 -->
9<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
10<h1>OgreBitwise.h</h1><a href="OgreBitwise_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>
1100002 <span class="comment">-----------------------------------------------------------------------------</span>
1200003 <span class="comment">This source file is part of OGRE</span>
1300004 <span class="comment">    (Object-oriented Graphics Rendering Engine)</span>
1400005 <span class="comment">For the latest info, see http://www.ogre3d.org/</span>
1500006 <span class="comment"></span>
1600007 <span class="comment">Copyright (c) 2000-2005 The OGRE Team</span>
1700008 <span class="comment">Also see acknowledgements in Readme.html</span>
1800009 <span class="comment"></span>
1900010 <span class="comment">This program is free software; you can redistribute it and/or modify it under</span>
2000011 <span class="comment">the terms of the GNU Lesser General Public License as published by the Free Software</span>
2100012 <span class="comment">Foundation; either version 2 of the License, or (at your option) any later</span>
2200013 <span class="comment">version.</span>
2300014 <span class="comment"></span>
2400015 <span class="comment">This program is distributed in the hope that it will be useful, but WITHOUT</span>
2500016 <span class="comment">ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS</span>
2600017 <span class="comment">FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.</span>
2700018 <span class="comment"></span>
2800019 <span class="comment">You should have received a copy of the GNU Lesser General Public License along with</span>
2900020 <span class="comment">this program; if not, write to the Free Software Foundation, Inc., 59 Temple</span>
3000021 <span class="comment">Place - Suite 330, Boston, MA 02111-1307, USA, or go to</span>
3100022 <span class="comment">http://www.gnu.org/copyleft/lesser.txt.</span>
3200023 <span class="comment">-----------------------------------------------------------------------------</span>
3300024 <span class="comment">*/</span>
3400025 <span class="preprocessor">#ifndef _Bitwise_H__</span>
3500026 <span class="preprocessor"></span><span class="preprocessor">#define _Bitwise_H__</span>
3600027 <span class="preprocessor"></span>
3700028 <span class="preprocessor">#include "<a class="code" href="OgrePrerequisites_8h.html">OgrePrerequisites.h</a>"</span>
3800029
3900030 <span class="keyword">namespace </span>Ogre {
4000031
41<a name="l00034"></a><a class="code" href="classOgre_1_1Bitwise.html">00034</a>     <span class="keyword">class </span><a class="code" href="classOgre_1_1Bitwise.html">Bitwise</a> {
4200035     <span class="keyword">public</span>:
4300039         <span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;
44<a name="l00040"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee0">00040</a>         <span class="keyword">static</span> <a class="code" href="OgrePlatform_8h.html#a10">FORCEINLINE</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee0">getBitShift</a>(T mask)
4500041         {
4600042             <span class="keywordflow">if</span> (mask == 0)
4700043                 <span class="keywordflow">return</span> 0;
4800044
4900045             <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> result = 0;
5000046             <span class="keywordflow">while</span> ((mask &amp; 1) == 0) {
5100047                 ++result;
5200048                 mask &gt;&gt;= 1;
5300049             }
5400050             <span class="keywordflow">return</span> result;
5500051         }
5600052
5700058         <span class="keyword">template</span>&lt;<span class="keyword">typename</span> SrcT, <span class="keyword">typename</span> DestT&gt;
58<a name="l00059"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee1">00059</a>         <span class="keyword">static</span> <span class="keyword">inline</span> DestT <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee1">convertBitPattern</a>(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
5900060         {
6000061             <span class="comment">// Mask off irrelevant source value bits (if any)</span>
6100062             srcValue = srcValue &amp; srcBitMask;
6200063
6300064             <span class="comment">// Shift source down to bottom of DWORD</span>
6400065             <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> srcBitShift = <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee0">getBitShift</a>(srcBitMask);
6500066             srcValue &gt;&gt;= srcBitShift;
6600067
6700068             <span class="comment">// Get max value possible in source from srcMask</span>
6800069             <span class="keyword">const</span> SrcT srcMax = srcBitMask &gt;&gt; srcBitShift;
6900070
7000071             <span class="comment">// Get max avaiable in dest</span>
7100072             <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> destBitShift = <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee0">getBitShift</a>(destBitMask);
7200073             <span class="keyword">const</span> DestT destMax = destBitMask &gt;&gt; destBitShift;
7300074
7400075             <span class="comment">// Scale source value into destination, and shift back</span>
7500076             DestT destValue = (srcValue * destMax) / srcMax;
7600077             <span class="keywordflow">return</span> (destValue &lt;&lt; destBitShift);
7700078         }
7800079
79<a name="l00084"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee2">00084</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee2">fixedToFixed</a>(<a class="code" href="namespaceOgre.html#a412">uint32</a> value, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> p)
8000085         {
8100086             <span class="keywordflow">if</span>(n &gt; p)
8200087             {
8300088                 <span class="comment">// Less bits required than available; this is easy</span>
8400089                 value &gt;&gt;= n-p;
8500090             }
8600091             <span class="keywordflow">else</span> <span class="keywordflow">if</span>(n &lt; p)
8700092             {
8800093                 <span class="comment">// More bits required than are there, do the fill</span>
8900094                 <span class="comment">// Use old fashioned division, probably better than a loop</span>
9000095                 <span class="keywordflow">if</span>(value == 0)
9100096                         value = 0;
9200097                 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(value == (static_cast&lt;unsigned int&gt;(1)&lt;&lt;n)-1)
9300098                         value = (1&lt;&lt;p)-1;
9400099                 <span class="keywordflow">else</span>    value = value*(1&lt;&lt;p)/((1&lt;&lt;n)-1);
9500100             }
9600101             <span class="keywordflow">return</span> value;   
9700102         }
9800103
99<a name="l00108"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee3">00108</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee3">floatToFixed</a>(<span class="keyword">const</span> <span class="keywordtype">float</span> value, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bits)
10000109         {
10100110             <span class="keywordflow">if</span>(value &lt;= 0.0f) <span class="keywordflow">return</span> 0;
10200111             <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value &gt;= 1.0f) <span class="keywordflow">return</span> (1&lt;&lt;bits)-1;
10300112             <span class="keywordflow">else</span> <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)(value * (1&lt;&lt;bits));     
10400113         }
10500114
106<a name="l00118"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee4">00118</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">float</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee4">fixedToFloat</a>(<span class="keywordtype">unsigned</span> value, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bits)
10700119         {
10800120             <span class="keywordflow">return</span> (<span class="keywordtype">float</span>)value/(<span class="keywordtype">float</span>)((1&lt;&lt;bits)-1);
10900121         }
11000122
111<a name="l00126"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee5">00126</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee5">intWrite</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> *dest, <span class="keyword">const</span> <span class="keywordtype">int</span> n, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> value)
11200127         {
11300128             <span class="keywordflow">switch</span>(n) {
11400129                 <span class="keywordflow">case</span> 1:
11500130                     ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[0] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)value;
11600131                     <span class="keywordflow">break</span>;
11700132                 <span class="keywordflow">case</span> 2:
11800133                     ((<a class="code" href="namespaceOgre.html#a413">uint16</a>*)dest)[0] = (<a class="code" href="namespaceOgre.html#a413">uint16</a>)value;
11900134                     <span class="keywordflow">break</span>;
12000135                 <span class="keywordflow">case</span> 3:
12100136 <span class="preprocessor">#if OGRE_ENDIAN == OGRE_ENDIAN_BIG      </span>
12200137 <span class="preprocessor"></span>                    ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[0] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)((value &gt;&gt; 16) &amp; 0xFF);
12300138                     ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[1] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)((value &gt;&gt; 8) &amp; 0xFF);
12400139                     ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[2] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)(value &amp; 0xFF);
12500140 <span class="preprocessor">#else</span>
12600141 <span class="preprocessor"></span>                    ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[2] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)((value &gt;&gt; 16) &amp; 0xFF);
12700142                     ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[1] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)((value &gt;&gt; 8) &amp; 0xFF);
12800143                     ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)dest)[0] = (<a class="code" href="namespaceOgre.html#a414">uint8</a>)(value &amp; 0xFF);
12900144 <span class="preprocessor">#endif</span>
13000145 <span class="preprocessor"></span>                    <span class="keywordflow">break</span>;
13100146                 <span class="keywordflow">case</span> 4:
13200147                     ((<a class="code" href="namespaceOgre.html#a412">uint32</a>*)dest)[0] = (<a class="code" href="namespaceOgre.html#a412">uint32</a>)value;               
13300148                     <span class="keywordflow">break</span>;               
13400149             }       
13500150         }
136<a name="l00154"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee6">00154</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee6">intRead</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> *src, <span class="keywordtype">int</span> n) {
13700155             <span class="keywordflow">switch</span>(n) {
13800156                 <span class="keywordflow">case</span> 1:
13900157                     <span class="keywordflow">return</span> ((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[0];
14000158                 <span class="keywordflow">case</span> 2:
14100159                     <span class="keywordflow">return</span> ((<a class="code" href="namespaceOgre.html#a413">uint16</a>*)src)[0];
14200160                 <span class="keywordflow">case</span> 3:
14300161 <span class="preprocessor">#if OGRE_ENDIAN == OGRE_ENDIAN_BIG      </span>
14400162 <span class="preprocessor"></span>                    <span class="keywordflow">return</span> ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[0]&lt;&lt;16)|
14500163                             ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[1]&lt;&lt;8)|
14600164                             ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[2]);
14700165 <span class="preprocessor">#else</span>
14800166 <span class="preprocessor"></span>                    <span class="keywordflow">return</span> ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[0])|
14900167                             ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[1]&lt;&lt;8)|
15000168                             ((<a class="code" href="namespaceOgre.html#a412">uint32</a>)((<a class="code" href="namespaceOgre.html#a414">uint8</a>*)src)[2]&lt;&lt;16);
15100169 <span class="preprocessor">#endif</span>
15200170 <span class="preprocessor"></span>                <span class="keywordflow">case</span> 4:
15300171                     <span class="keywordflow">return</span> ((<a class="code" href="namespaceOgre.html#a412">uint32</a>*)src)[0];
15400172             }
15500173             <span class="keywordflow">return</span> 0; <span class="comment">// ?</span>
15600174         }
15700175
158<a name="l00179"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee7">00179</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <a class="code" href="namespaceOgre.html#a413">uint16</a> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee7">floatToHalf</a>(<span class="keywordtype">float</span> i)
15900180         {
16000181             <span class="keywordflow">return</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee8">floatToHalfI</a>(*reinterpret_cast&lt;uint32*&gt;(&amp;i));
16100182         }
162<a name="l00185"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee8">00185</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <a class="code" href="namespaceOgre.html#a413">uint16</a> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee8">floatToHalfI</a>(<a class="code" href="namespaceOgre.html#a412">uint32</a> i)
16300186         {
16400187             <span class="keyword">register</span> <span class="keywordtype">int</span> s =  (i &gt;&gt; 16) &amp; 0x00008000;
16500188             <span class="keyword">register</span> <span class="keywordtype">int</span> e = ((i &gt;&gt; 23) &amp; 0x000000ff) - (127 - 15);
16600189             <span class="keyword">register</span> <span class="keywordtype">int</span> m =   i        &amp; 0x007fffff;
16700190         
16800191             <span class="keywordflow">if</span> (e &lt;= 0)
16900192             {
17000193                 <span class="keywordflow">if</span> (e &lt; -10)
17100194                 {
17200195                     <span class="keywordflow">return</span> 0;
17300196                 }
17400197                 m = (m | 0x00800000) &gt;&gt; (1 - e);
17500198         
17600199                 <span class="keywordflow">return</span> s | (m &gt;&gt; 13);
17700200             }
17800201             <span class="keywordflow">else</span> <span class="keywordflow">if</span> (e == 0xff - (127 - 15))
17900202             {
18000203                 <span class="keywordflow">if</span> (m == 0) <span class="comment">// Inf</span>
18100204                 {
18200205                     <span class="keywordflow">return</span> s | 0x7c00;
18300206                 }
18400207                 <span class="keywordflow">else</span>    <span class="comment">// NAN</span>
18500208                 {
18600209                     m &gt;&gt;= 13;
18700210                     <span class="keywordflow">return</span> s | 0x7c00 | m | (m == 0);
18800211                 }
18900212             }
19000213             <span class="keywordflow">else</span>
19100214             {
19200215                 <span class="keywordflow">if</span> (e &gt; 30) <span class="comment">// Overflow</span>
19300216                 {
19400217                     <span class="keywordflow">return</span> s | 0x7c00;
19500218                 }
19600219         
19700220                 <span class="keywordflow">return</span> s | (e &lt;&lt; 10) | (m &gt;&gt; 13);
19800221             }
19900222         }
20000223         
201<a name="l00228"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee9">00228</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">float</span> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee9">halfToFloat</a>(<a class="code" href="namespaceOgre.html#a413">uint16</a> y)
20200229         {
20300230             <a class="code" href="namespaceOgre.html#a412">uint32</a> r = <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee10">halfToFloatI</a>(y);
20400231             <span class="keywordflow">return</span> *reinterpret_cast&lt;float*&gt;(&amp;r);
20500232         }
206<a name="l00236"></a><a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee10">00236</a>         <span class="keyword">static</span> <span class="keyword">inline</span> <a class="code" href="namespaceOgre.html#a412">uint32</a> <a class="code" href="classOgre_1_1Bitwise.html#Ogre_1_1Bitwisee10">halfToFloatI</a>(<a class="code" href="namespaceOgre.html#a413">uint16</a> y)
20700237         {
20800238             <span class="keyword">register</span> <span class="keywordtype">int</span> s = (y &gt;&gt; 15) &amp; 0x00000001;
20900239             <span class="keyword">register</span> <span class="keywordtype">int</span> e = (y &gt;&gt; 10) &amp; 0x0000001f;
21000240             <span class="keyword">register</span> <span class="keywordtype">int</span> m =  y        &amp; 0x000003ff;
21100241         
21200242             <span class="keywordflow">if</span> (e == 0)
21300243             {
21400244                 <span class="keywordflow">if</span> (m == 0) <span class="comment">// Plus or minus zero</span>
21500245                 {
21600246                     <span class="keywordflow">return</span> s &lt;&lt; 31;
21700247                 }
21800248                 <span class="keywordflow">else</span> <span class="comment">// Denormalized number -- renormalize it</span>
21900249                 {
22000250                     <span class="keywordflow">while</span> (!(m &amp; 0x00000400))
22100251                     {
22200252                         m &lt;&lt;= 1;
22300253                         e -=  1;
22400254                     }
22500255         
22600256                     e += 1;
22700257                     m &amp;= ~0x00000400;
22800258                 }
22900259             }
23000260             <span class="keywordflow">else</span> <span class="keywordflow">if</span> (e == 31)
23100261             {
23200262                 <span class="keywordflow">if</span> (m == 0) <span class="comment">// Inf</span>
23300263                 {
23400264                     <span class="keywordflow">return</span> (s &lt;&lt; 31) | 0x7f800000;
23500265                 }
23600266                 <span class="keywordflow">else</span> <span class="comment">// NaN</span>
23700267                 {
23800268                     <span class="keywordflow">return</span> (s &lt;&lt; 31) | 0x7f800000 | (m &lt;&lt; 13);
23900269                 }
24000270             }
24100271         
24200272             e = e + (127 - 15);
24300273             m = m &lt;&lt; 13;
24400274         
24500275             <span class="keywordflow">return</span> (s &lt;&lt; 31) | (e &lt;&lt; 23) | m;
24600276         }
24700277         
24800278
24900279     };
25000280 }
25100281
25200282 <span class="preprocessor">#endif</span>
253</pre></div><hr>
254<p>
255Copyright &copy; 2000-2005 by The OGRE Team<br />
256<!--Creative Commons License--><a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights20.png"/></a><br/>
257This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/">Creative Commons Attribution-ShareAlike 2.5 License</a>.<br/>
258                <!--/Creative Commons License--><!-- <rdf:RDF xmlns="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
259                <Work rdf:about="">
260                        <license rdf:resource="http://creativecommons.org/licenses/by-sa/2.5/" />
261        <dc:type rdf:resource="http://purl.org/dc/dcmitype/Text" />
262                </Work>
263                <License rdf:about="http://creativecommons.org/licenses/by-sa/2.5/"><permits rdf:resource="http://web.resource.org/cc/Reproduction"/><permits rdf:resource="http://web.resource.org/cc/Distribution"/><requires rdf:resource="http://web.resource.org/cc/Notice"/><requires rdf:resource="http://web.resource.org/cc/Attribution"/><permits rdf:resource="http://web.resource.org/cc/DerivativeWorks"/><requires rdf:resource="http://web.resource.org/cc/ShareAlike"/></License></rdf:RDF> -->
264
265Last modified Sun Feb 12 12:59:41 2006
266</p>
267</body>
268</html>
Note: See TracBrowser for help on using the repository browser.