OLD | NEW |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* ***** BEGIN LICENSE BLOCK ***** | 2 /* ***** BEGIN LICENSE BLOCK ***** |
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 * | 4 * |
5 * The contents of this file are subject to the Mozilla Public License Version | 5 * The contents of this file are subject to the Mozilla Public License Version |
6 * 1.1 (the "License"); you may not use this file except in compliance with | 6 * 1.1 (the "License"); you may not use this file except in compliance with |
7 * the License. You may obtain a copy of the License at | 7 * the License. You may obtain a copy of the License at |
8 * http://www.mozilla.org/MPL/ | 8 * http://www.mozilla.org/MPL/ |
9 * | 9 * |
10 * Software distributed under the License is distributed on an "AS IS" basis, | 10 * Software distributed under the License is distributed on an "AS IS" basis, |
(...skipping 24 matching lines...) Expand all Loading... |
35 * | 35 * |
36 * ***** END LICENSE BLOCK ***** */ | 36 * ***** END LICENSE BLOCK ***** */ |
37 | 37 |
38 #include "prbit.h" | 38 #include "prbit.h" |
39 | 39 |
40 /* | 40 /* |
41 ** Compute the log of the least power of 2 greater than or equal to n | 41 ** Compute the log of the least power of 2 greater than or equal to n |
42 */ | 42 */ |
43 PR_IMPLEMENT(PRIntn) PR_CeilingLog2(PRUint32 n) | 43 PR_IMPLEMENT(PRIntn) PR_CeilingLog2(PRUint32 n) |
44 { | 44 { |
45 PRIntn log2 = 0; | 45 PRIntn log2; |
46 | 46 PR_CEILING_LOG2(log2, n); |
47 if (n & (n-1)) | |
48 » log2++; | |
49 if (n >> 16) | |
50 » log2 += 16, n >>= 16; | |
51 if (n >> 8) | |
52 » log2 += 8, n >>= 8; | |
53 if (n >> 4) | |
54 » log2 += 4, n >>= 4; | |
55 if (n >> 2) | |
56 » log2 += 2, n >>= 2; | |
57 if (n >> 1) | |
58 » log2++; | |
59 return log2; | 47 return log2; |
60 } | 48 } |
61 | 49 |
62 /* | 50 /* |
63 ** Compute the log of the greatest power of 2 less than or equal to n. | 51 ** Compute the log of the greatest power of 2 less than or equal to n. |
64 ** This really just finds the highest set bit in the word. | 52 ** This really just finds the highest set bit in the word. |
65 */ | 53 */ |
66 PR_IMPLEMENT(PRIntn) PR_FloorLog2(PRUint32 n) | 54 PR_IMPLEMENT(PRIntn) PR_FloorLog2(PRUint32 n) |
67 { | 55 { |
68 PRIntn log2 = 0; | 56 PRIntn log2; |
69 | 57 PR_FLOOR_LOG2(log2, n); |
70 if (n >> 16) | |
71 » log2 += 16, n >>= 16; | |
72 if (n >> 8) | |
73 » log2 += 8, n >>= 8; | |
74 if (n >> 4) | |
75 » log2 += 4, n >>= 4; | |
76 if (n >> 2) | |
77 » log2 += 2, n >>= 2; | |
78 if (n >> 1) | |
79 » log2++; | |
80 return log2; | 58 return log2; |
81 } | 59 } |
OLD | NEW |