00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #include <common/FixedVector.h> 00022 00023 static FixedVector nullVector; 00024 00025 FixedVector &FixedVector::getNullVector() 00026 { 00027 nullVector.zero(); 00028 return nullVector; 00029 } 00030 00031 #define sqrt_step(shift) \ 00032 if((0x40000000l >> shift) + root <= value) \ 00033 { \ 00034 value -= (0x40000000l >> shift) + root; \ 00035 root = (root >> 1) | (0x40000000l >> shift); \ 00036 } \ 00037 else \ 00038 { \ 00039 root = root >> 1; \ 00040 } 00041 00042 static long long iSqrtLL(long long value) 00043 { 00044 long long root = 0; 00045 00046 sqrt_step( 0); 00047 sqrt_step( 2); 00048 sqrt_step( 4); 00049 sqrt_step( 6); 00050 sqrt_step( 8); 00051 sqrt_step(10); 00052 sqrt_step(12); 00053 sqrt_step(14); 00054 sqrt_step(16); 00055 sqrt_step(18); 00056 sqrt_step(20); 00057 sqrt_step(22); 00058 sqrt_step(24); 00059 sqrt_step(26); 00060 sqrt_step(28); 00061 sqrt_step(30); 00062 00063 // round to the nearest integer, cuts max error in half 00064 00065 if(root < value) 00066 { 00067 ++root; 00068 } 00069 00070 return root; 00071 } 00072 00073 static long iSqrtL(long value) 00074 { 00075 long root = 0; 00076 00077 sqrt_step( 0); 00078 sqrt_step( 2); 00079 sqrt_step( 4); 00080 sqrt_step( 6); 00081 sqrt_step( 8); 00082 sqrt_step(10); 00083 sqrt_step(12); 00084 sqrt_step(14); 00085 sqrt_step(16); 00086 sqrt_step(18); 00087 sqrt_step(20); 00088 sqrt_step(22); 00089 sqrt_step(24); 00090 sqrt_step(26); 00091 sqrt_step(28); 00092 sqrt_step(30); 00093 00094 // round to the nearest integer, cuts max error in half 00095 00096 if(root < value) 00097 { 00098 ++root; 00099 } 00100 00101 return root; 00102 } 00103 00104 fixed FixedVector::Magnitude() 00105 { 00106 if (sizeof(long) == 8) 00107 { 00108 long a = ((long)V[0].getInternal()*V[0].getInternal())/FIXED_RESOLUTION; 00109 long b = ((long)V[1].getInternal()*V[1].getInternal())/FIXED_RESOLUTION; 00110 long c = ((long)V[2].getInternal()*V[2].getInternal())/FIXED_RESOLUTION; 00111 00112 long res = a + b + c; 00113 long result = iSqrtL(res); 00114 result *= 100; 00115 00116 return fixed(true, (int) result); 00117 } 00118 else if (sizeof(long long) == 8) 00119 { 00120 long long a = ((long long)V[0].getInternal()*V[0].getInternal())/FIXED_RESOLUTION; 00121 long long b = ((long long)V[1].getInternal()*V[1].getInternal())/FIXED_RESOLUTION; 00122 long long c = ((long long)V[2].getInternal()*V[2].getInternal())/FIXED_RESOLUTION; 00123 00124 long long res = a + b + c; 00125 long long result = iSqrtLL(res); 00126 result *= 100; 00127 00128 return fixed(true, (int) result); 00129 } 00130 return fixed(0); 00131 }
1.5.3