OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 int log = 0; | 48 int log = 0; |
49 unsigned int value = n; | 49 unsigned int value = n; |
50 for (int i = 4; i >= 0; --i) { | 50 for (int i = 4; i >= 0; --i) { |
51 int shift = (1 << i); | 51 int shift = (1 << i); |
52 unsigned int x = value >> shift; | 52 unsigned int x = value >> shift; |
53 if (x != 0) { | 53 if (x != 0) { |
54 value = x; | 54 value = x; |
55 log += shift; | 55 log += shift; |
56 } | 56 } |
57 } | 57 } |
58 DCHECK_EQ(value, 1); | 58 DCHECK_EQ(value, 1u); |
59 return log; | 59 return log; |
60 } | 60 } |
61 | 61 |
62 // Returns the integer i such as 2^(i-1) < n <= 2^i | 62 // Returns the integer i such as 2^(i-1) < n <= 2^i |
63 static inline int Log2Ceiling(unsigned int n) { | 63 static inline int Log2Ceiling(unsigned int n) { |
64 if (n == 0) { | 64 if (n == 0) { |
65 return -1; | 65 return -1; |
66 } else { | 66 } else { |
67 // Log2Floor returns -1 for 0, so the following works correctly for n=1. | 67 // Log2Floor returns -1 for 0, so the following works correctly for n=1. |
68 return 1 + Log2Floor(n - 1); | 68 return 1 + Log2Floor(n - 1); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 } // namespace bits | 72 } // namespace bits |
73 } // namespace base | 73 } // namespace base |
74 } // namespace o3d | 74 } // namespace o3d |
75 | 75 |
76 #endif // O3D_BASE_CROSS_BITS_H_ | 76 #endif // O3D_BASE_CROSS_BITS_H_ |
OLD | NEW |