1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "utilities/formatBuffer.hpp"
  26 #include "utilities/globalDefinitions.hpp"
  27 #include "unittest.hpp"
  28 
  29 #include <limits>
  30 
  31 // A few arbitrarily chosen values to test the align functions on.
  32 static uint64_t values[] = {1, 3, 10, 345, 1023, 1024, 1025, 23909034, INT_MAX, uint64_t(-1) / 2, uint64_t(-1) / 2 + 100, -1 };
  33 
  34 template <typename T>
  35 static T max_alignment() {
  36   T max = std::numeric_limits<T>::max();
  37   return max ^ (max >> 1);
  38 }
  39 
  40 #define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__).buffer())
  41 
  42 template <typename T, typename A>
  43 static void test_alignments() {
  44   log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
  45       std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
  46       std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
  47 
  48   // Test all possible alignment values that fit in type A.
  49   for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
  50     log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
  51 
  52     for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
  53       log("--- Value: " UINT64_FORMAT "\n", values[i]);
  54 
  55       // Test align up
  56       const uint64_t up = align_up_(values[i], (uint64_t)alignment);
  57       if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
  58         log("Testing align_up:   alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], up);
  59 
  60         T value = T(values[i]);
  61 
  62         // Check against uint64_t version
  63         ASSERT_EQ(align_up((uint64_t)value, alignment), up);
  64         // Check inline function vs macro
  65         ASSERT_EQ(align_up(value, alignment), align_up_(value, alignment));
  66         // Sanity check
  67         ASSERT_GE(align_up(value, alignment), value);
  68       }
  69 
  70       // Test align down
  71       const uint64_t down = align_down_(values[i], (uint64_t)alignment);
  72       if (down <= (uint64_t)std::numeric_limits<T>::max()) {
  73         log("Testing align_down: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], down);
  74 
  75         T value = T(values[i]);
  76 
  77         // Check against uint64_t version
  78         ASSERT_EQ((uint64_t)align_down(value, alignment), down);
  79         // Check inline function vs macro
  80         ASSERT_EQ(align_down(value, alignment), align_down_(value, alignment));
  81         // Sanity check
  82         ASSERT_LE(align_down(value, alignment), value);
  83       }
  84 
  85       // Test is aligned
  86       const bool is = is_aligned_(values[i], (uint64_t)alignment);
  87       if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
  88         log("Testing is_aligned: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: %s\n", (uint64_t)alignment, values[i], is ? "true" : "false");
  89 
  90         T value = T(values[i]);
  91 
  92         // Check against uint64_t version
  93         ASSERT_EQ(is_aligned(value, alignment), is);
  94         // Check inline function vs macro
  95         ASSERT_EQ(is_aligned(value, alignment), is_aligned_(value, alignment));
  96       }
  97     }
  98   }
  99 }
 100 
 101 TEST(Align, functions_and_macros) {
 102   // Test the alignment functions with different type combinations.
 103 
 104   test_alignments<int64_t, uint8_t>();
 105   test_alignments<int64_t, uint16_t>();
 106   test_alignments<int64_t, uint32_t>();
 107   test_alignments<int64_t, int8_t>();
 108   test_alignments<int64_t, int16_t>();
 109   test_alignments<int64_t, int32_t>();
 110   test_alignments<int64_t, int64_t>();
 111 
 112   test_alignments<uint32_t, uint8_t>();
 113   test_alignments<uint32_t, uint16_t>();
 114   test_alignments<uint32_t, uint32_t>();
 115   test_alignments<uint32_t, int8_t>();
 116   test_alignments<uint32_t, int16_t>();
 117   test_alignments<uint32_t, int32_t>();
 118 
 119   test_alignments<int32_t, uint8_t>();
 120   test_alignments<int32_t, uint16_t>();
 121   test_alignments<int32_t, int8_t>();
 122   test_alignments<int32_t, int16_t>();
 123   test_alignments<int32_t, int32_t>();
 124 
 125   test_alignments<uint16_t, uint8_t>();
 126   test_alignments<uint16_t, uint16_t>();
 127   test_alignments<uint16_t, int8_t>();
 128   test_alignments<uint16_t, int16_t>();
 129 
 130   test_alignments<int16_t, uint8_t>();
 131   test_alignments<int16_t, int8_t>();
 132   test_alignments<int16_t, int16_t>();
 133 
 134   test_alignments<uint8_t, int8_t>();
 135   test_alignments<uint8_t, uint8_t>();
 136 
 137   test_alignments<int8_t, int8_t>();
 138 }