# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

fast_floor.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_FAST_FLOOR_H
00027 #define NL_FAST_FLOOR_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include <math.h>
00031 
00032 namespace NL3D
00033 {                                         
00034 
00035 // fastFloor function. 
00036 #ifdef NL_OS_WINDOWS
00037 
00038 #include <float.h>
00039 
00040 // The magic constant value. support both positive and negative numbers.
00041 extern double   OptFastFloorMagicConst ; 
00042 extern int OptFastFloorBkupCW ;
00043 // init float CW.
00044 inline void  OptFastFloorBegin()
00045 {
00046         OptFastFloorBkupCW= _controlfp(0, 0);
00047         _controlfp( _RC_DOWN|_PC_53, _MCW_RC|_MCW_PC );
00048 }
00049 
00050 // reset float CW.
00051 inline void  OptFastFloorEnd()
00052 {
00053         _controlfp(OptFastFloorBkupCW, _MCW_RC|_MCW_PC);
00054 }
00055 
00056 // Force __stdcall to not pass parameters in registers.
00057 inline sint32 __stdcall OptFastFloor(float x)
00058 {       
00059         static __int64  res;
00060         __asm
00061         {
00062                 fld             x
00063                 fadd    qword ptr OptFastFloorMagicConst                
00064                 fstp    qword ptr res           
00065         }
00066 
00067         return (sint32) res;
00068 }
00069 
00070 
00071 // Force __stdcall to not pass parameters in registers.
00072 inline float __stdcall OptFastFractionnalPart(float x)
00073 {
00074         static double res;
00075         __asm
00076         {
00077                 fld             x
00078                 fld     st(0)
00079                 fadd    qword ptr OptFastFloorMagicConst
00080                 fstp    qword ptr res
00081                 fisub   dword ptr res           
00082                 fstp    dword ptr res
00083         }
00084 
00085         return * (float *) &res;
00086 }
00087 
00088 
00089 
00090 
00091 #else
00092 inline void  OptFastFloorBegin() {}
00093 inline void  OptFastFloorEnd() {}
00094 inline sint  OptFastFloor(float x)
00095 {
00096         return (sint)floor(x);
00097 }
00098 
00099 inline float  OptFastFractionnalPart(float x)
00100 {
00101         return x - (sint) x ;
00102 }
00103 
00104 
00105 #endif
00106 
00107 
00108 
00109 } // NL3D
00110 
00111 
00112 #endif // NL_FAST_FLOOR_H
00113 
00114 /* End of fast_floor.h */