Visual Studio 2012 Support
[xy_vsfilter.git] / src / thirdparty / VirtualDub / system / source / bitmath.cpp
blob2833932baf241683716ecb6ed64b127dedb26f84
1 // VirtualDub - Video processing and capture application
2 // System library component
3 // Copyright (C) 1998-2007 Avery Lee, All Rights Reserved.
4 //
5 // Beginning with 1.6.0, the VirtualDub system library is licensed
6 // differently than the remainder of VirtualDub. This particular file is
7 // thus licensed as follows (the "zlib" license):
8 //
9 // This software is provided 'as-is', without any express or implied
10 // warranty. In no event will the authors be held liable for any
11 // damages arising from the use of this software.
13 // Permission is granted to anyone to use this software for any purpose,
14 // including commercial applications, and to alter it and redistribute it
15 // freely, subject to the following restrictions:
17 // 1. The origin of this software must not be misrepresented; you must
18 // not claim that you wrote the original software. If you use this
19 // software in a product, an acknowledgment in the product
20 // documentation would be appreciated but is not required.
21 // 2. Altered source versions must be plainly marked as such, and must
22 // not be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
26 #include "stdafx.h"
27 #include <vd2/system/bitmath.h>
29 int VDCountBits(uint32 v) {
30 v -= (v >> 1) & 0x55555555;
31 v = ((v & 0xcccccccc) >> 2) + (v & 0x33333333);
32 v = (v + (v >> 4)) & 0x0f0f0f0f;
33 return (v * 0x01010101) >> 24;
36 #ifndef VD_COMPILER_MSVC_VC8
38 int VDFindLowestSetBit(uint32 v) {
39 for(int i=0; i<32; ++i) {
40 if (v & 1)
41 return i;
42 v >>= 1;
45 return 32;
48 int VDFindHighestSetBit(uint32 v) {
49 for(int i=31; i>=0; --i) {
50 if ((sint32)v < 0)
51 return i;
52 v += v;
54 return -1;
57 #endif
59 uint32 VDCeilToPow2(uint32 v) {
60 v += v;
61 --v;
63 while(uint32 x = v & (v - 1))
64 v = x;
66 return v;