Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/MathAlgorithms.h" |
michael@0 | 7 | |
michael@0 | 8 | using mozilla::CeilingLog2; |
michael@0 | 9 | using mozilla::FloorLog2; |
michael@0 | 10 | using mozilla::RoundUpPow2; |
michael@0 | 11 | |
michael@0 | 12 | static void |
michael@0 | 13 | TestCeiling() |
michael@0 | 14 | { |
michael@0 | 15 | for (uint32_t i = 0; i <= 1; i++) |
michael@0 | 16 | MOZ_RELEASE_ASSERT(CeilingLog2(i) == 0); |
michael@0 | 17 | |
michael@0 | 18 | for (uint32_t i = 2; i <= 2; i++) |
michael@0 | 19 | MOZ_RELEASE_ASSERT(CeilingLog2(i) == 1); |
michael@0 | 20 | |
michael@0 | 21 | for (uint32_t i = 3; i <= 4; i++) |
michael@0 | 22 | MOZ_RELEASE_ASSERT(CeilingLog2(i) == 2); |
michael@0 | 23 | |
michael@0 | 24 | for (uint32_t i = 5; i <= 8; i++) |
michael@0 | 25 | MOZ_RELEASE_ASSERT(CeilingLog2(i) == 3); |
michael@0 | 26 | |
michael@0 | 27 | for (uint32_t i = 9; i <= 16; i++) |
michael@0 | 28 | MOZ_RELEASE_ASSERT(CeilingLog2(i) == 4); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | static void |
michael@0 | 32 | TestFloor() |
michael@0 | 33 | { |
michael@0 | 34 | for (uint32_t i = 0; i <= 1; i++) |
michael@0 | 35 | MOZ_RELEASE_ASSERT(FloorLog2(i) == 0); |
michael@0 | 36 | |
michael@0 | 37 | for (uint32_t i = 2; i <= 3; i++) |
michael@0 | 38 | MOZ_RELEASE_ASSERT(FloorLog2(i) == 1); |
michael@0 | 39 | |
michael@0 | 40 | for (uint32_t i = 4; i <= 7; i++) |
michael@0 | 41 | MOZ_RELEASE_ASSERT(FloorLog2(i) == 2); |
michael@0 | 42 | |
michael@0 | 43 | for (uint32_t i = 8; i <= 15; i++) |
michael@0 | 44 | MOZ_RELEASE_ASSERT(FloorLog2(i) == 3); |
michael@0 | 45 | |
michael@0 | 46 | for (uint32_t i = 16; i <= 31; i++) |
michael@0 | 47 | MOZ_RELEASE_ASSERT(FloorLog2(i) == 4); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | static void |
michael@0 | 51 | TestRoundUpPow2() |
michael@0 | 52 | { |
michael@0 | 53 | MOZ_RELEASE_ASSERT(RoundUpPow2(0) == 1); |
michael@0 | 54 | MOZ_RELEASE_ASSERT(RoundUpPow2(1) == 1); |
michael@0 | 55 | MOZ_RELEASE_ASSERT(RoundUpPow2(2) == 2); |
michael@0 | 56 | MOZ_RELEASE_ASSERT(RoundUpPow2(3) == 4); |
michael@0 | 57 | MOZ_RELEASE_ASSERT(RoundUpPow2(4) == 4); |
michael@0 | 58 | MOZ_RELEASE_ASSERT(RoundUpPow2(5) == 8); |
michael@0 | 59 | MOZ_RELEASE_ASSERT(RoundUpPow2(6) == 8); |
michael@0 | 60 | MOZ_RELEASE_ASSERT(RoundUpPow2(7) == 8); |
michael@0 | 61 | MOZ_RELEASE_ASSERT(RoundUpPow2(8) == 8); |
michael@0 | 62 | MOZ_RELEASE_ASSERT(RoundUpPow2(9) == 16); |
michael@0 | 63 | |
michael@0 | 64 | MOZ_RELEASE_ASSERT(RoundUpPow2(15) == 16); |
michael@0 | 65 | MOZ_RELEASE_ASSERT(RoundUpPow2(16) == 16); |
michael@0 | 66 | MOZ_RELEASE_ASSERT(RoundUpPow2(17) == 32); |
michael@0 | 67 | |
michael@0 | 68 | MOZ_RELEASE_ASSERT(RoundUpPow2(31) == 32); |
michael@0 | 69 | MOZ_RELEASE_ASSERT(RoundUpPow2(32) == 32); |
michael@0 | 70 | MOZ_RELEASE_ASSERT(RoundUpPow2(33) == 64); |
michael@0 | 71 | |
michael@0 | 72 | size_t MaxPow2 = size_t(1) << (sizeof(size_t) * CHAR_BIT - 1); |
michael@0 | 73 | MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2 - 1) == MaxPow2); |
michael@0 | 74 | MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2) == MaxPow2); |
michael@0 | 75 | // not valid to round up when past the max power of two |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | int main() |
michael@0 | 79 | { |
michael@0 | 80 | TestCeiling(); |
michael@0 | 81 | TestFloor(); |
michael@0 | 82 | |
michael@0 | 83 | TestRoundUpPow2(); |
michael@0 | 84 | return 0; |
michael@0 | 85 | } |