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