blob: bfb88552bf569a1fcb892970e16afc89671336ef [file] [log] [blame]
Chris Lattner84dc8632002-05-19 21:19:55 +00001//===-- Support/MathExtras.h - Useful math functions -------------*- C++ -*--=//
2//
3// This file contains some functions that are useful for math stuff.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner5de22042001-11-27 00:03:19 +00006
Brian Gaekea7a50132003-06-17 00:35:55 +00007#ifndef SUPPORT_MATHEXTRAS_H
8#define SUPPORT_MATHEXTRAS_H
Chris Lattner5de22042001-11-27 00:03:19 +00009
Chris Lattner94a4f222002-10-28 02:11:53 +000010#include "Support/DataTypes.h"
Chris Lattner5de22042001-11-27 00:03:19 +000011
Chris Lattner84dc8632002-05-19 21:19:55 +000012inline unsigned log2(uint64_t C) {
Vikram S. Advee3633072002-05-19 15:46:52 +000013 unsigned getPow;
Chris Lattner84dc8632002-05-19 21:19:55 +000014 for (getPow = 0; C > 1; ++getPow)
15 C >>= 1;
Vikram S. Advee3633072002-05-19 15:46:52 +000016 return getPow;
17}
Chris Lattner5de22042001-11-27 00:03:19 +000018
Chris Lattner84dc8632002-05-19 21:19:55 +000019inline bool isPowerOf2(int64_t C, unsigned &getPow) {
20 if (C < 0) C = -C;
21 if (C > 0 && C == (C & ~(C - 1))) {
Chris Lattner3eeba802002-09-17 23:56:50 +000022 getPow = log2((uint64_t)C);
Chris Lattner84dc8632002-05-19 21:19:55 +000023 return true;
24 }
25
26 return false;
Chris Lattner5de22042001-11-27 00:03:19 +000027}
28
Chris Lattner84dc8632002-05-19 21:19:55 +000029#endif