source: cpp/PrintFloat/Math.cpp @ 1287

Last change on this file since 1287 was 834, checked in by Maciej Komosinski, 6 years ago

Renamed Math.h to MathDragon4.h because the <math.h> name clash is unfortunate (and caused problems with C++Builder and filename case-insensitive Windows)

File size: 2.6 KB
Line 
1/******************************************************************************
2  Copyright (c) 2014 Ryan Juckett
3  http://www.ryanjuckett.com/
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14     claim that you wrote the original software. If you use this software
15     in a product, an acknowledgment in the product documentation would be
16     appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19     misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22     distribution.
23******************************************************************************/
24
25#include "MathDragon4.h"
26
27//******************************************************************************
28// Get the log base 2 of a 32-bit unsigned integer.
29// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
30//******************************************************************************
31tU32 LogBase2( tU32 val )
32{
33    static const tU8 logTable[256] =
34    {
35        0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
36        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
37        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
38        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
39        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
40        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
41        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
42        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
43        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
44        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
45        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
46        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
47        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
48        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
49        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
51    };
52 
53    tU32 temp;
54 
55    temp = val >> 24;
56    if (temp)
57        return 24 + logTable[temp];
58 
59    temp = val >> 16;
60    if (temp)
61        return 16 + logTable[temp];
62 
63    temp = val >> 8;
64    if (temp)
65        return 8 + logTable[temp];
66 
67    return logTable[val];
68}
Note: See TracBrowser for help on using the repository browser.