Fixes for PR114: Thanks to Reid Spencer!
[oota-llvm.git] / include / llvm / Support / MathExtras.h
1 //===-- Support/MathExtras.h - Useful math functions ------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains some functions that are useful for math stuff.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SUPPORT_MATHEXTRAS_H
15 #define SUPPORT_MATHEXTRAS_H
16
17 #include "Support/DataTypes.h"
18
19 namespace llvm {
20
21 inline unsigned log2(uint64_t C) {
22   unsigned getPow;
23   for (getPow = 0; C > 1; ++getPow)
24     C >>= 1;
25   return getPow;
26 }
27
28 inline bool isPowerOf2(int64_t C, unsigned &getPow) {
29   if (C < 0) C = -C;
30   if (C > 0 && C == (C & ~(C - 1))) {
31     getPow = log2(static_cast<uint64_t>(C));
32     return true;
33   }
34
35   return false;
36 }
37
38 } // End llvm namespace
39
40 #endif