OSDN Git Service

Index: component/ChangeLog
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / cache / log2.h
1 // log2.h -- Utility functions for power of two values. -*- C++ -*-
2
3 // Copyright (C) 2001 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
6
7 #ifndef LOG2_H
8 #define LOG2_H
9
10 // Is V a power of two (ie. 32, 64)?
11
12 template <typename V>
13 bool power_of_two_p (const V& v1)
14 {
15   int count = 0;
16   V v2(1);
17
18   for (int i = 0; i < 8 * sizeof (V); i++)
19     if (v1 & (v2 << i))
20       count++;
21
22   // Only one bit was set in v1.
23   return (count == 1);
24 }
25
26 // Compute log2 (V).
27
28 template <typename V>
29 int log2 (const V& v1)
30 {
31   assert (power_of_two_p (v1));
32
33   V v2(1);
34   for (int i = 0; i < 8 * sizeof (V); i++)
35     if (v1 & (v2 << i))
36       return i;
37 }
38
39 #endif // LOG2_H