Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# OgreBitwise.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright © 2000-2002 The OGRE Team
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025 #ifndef _Bitwise_H__
    00026 #define _Bitwise_H__
    00027
    00028 #include "OgrePrerequisites.h"
    00029
    00030 namespace Ogre {
    00031
    00034     class Bitwise {
    00035     public:
    00039         template<typename T>
    00040         static FORCEINLINE unsigned int getBitShift(T mask)
    00041         {
    00042             if (mask == 0)
    00043                 return 0;
    00044
    00045             unsigned int result = 0;
    00046             while ((mask & 1) == 0) {
    00047                 ++result;
    00048                 mask >>= 1;
    00049             }
    00050             return result;
    00051         }
    00052
    00058         template<typename SrcT, typename DestT>
    00059         static inline DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
    00060         {
    00061             // Mask off irrelevant source value bits (if any)
    00062             srcValue = srcValue & srcBitMask;
    00063
    00064             // Shift source down to bottom of DWORD
    00065             const unsigned int srcBitShift = getBitShift(srcBitMask);
    00066             srcValue >>= srcBitShift;
    00067
    00068             // Get max value possible in source from srcMask
    00069             const SrcT srcMax = srcBitMask >> srcBitShift;
    00070
    00071             // Get max avaiable in dest
    00072             const unsigned int destBitShift = getBitShift(destBitMask);
    00073             const DestT destMax = destBitMask >> destBitShift;
    00074
    00075             // Scale source value into destination, and shift back
    00076             DestT destValue = (srcValue * destMax) / srcMax;
    00077             return (destValue << destBitShift);
    00078         }
    00079     };
    00080 }
    00081
    00082 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:15 2004