source: icXML/icXML-devel/src/icxmlc/clog2.h @ 2720

Last change on this file since 2720 was 2720, checked in by cameron, 6 years ago

Initial check-in of icXML 0.8 source files

File size: 886 bytes
Line 
1/*
2 *  Copyright © 2012 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icXML is a trademark of International Characters.
5 */
6
7/*
8 * @author Nigel Medforth, nigelm -at- interational-characters.com
9 * @version $Id: clog2.h 207 2012-12-02 20:38:22Z robc $
10 *
11 */
12
13#ifndef CONST_LOG_2
14
15/**
16 * These structs use template metaprogramming to recursively calculate log2(x) at compile-time.
17 * Use the CONST_LOG_2(x) macro for ease. NOTE: Although incorrect, this returns 0 for log2(0) for simplicity.
18 */
19
20template<unsigned long x> struct ConstLog2 { static const unsigned int value = ConstLog2<(x >> 1)>::value + 1; };
21
22template<> struct ConstLog2< 1 > { static const unsigned int value = 0; };
23
24template<> struct ConstLog2< 0 > { static const unsigned int value = 0; };
25
26#define CONST_LOG_2(x) (ConstLog2<(x)>::value)
27
28#endif
Note: See TracBrowser for help on using the repository browser.