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/align.hpp"
  26 #include "utilities/formatBuffer.hpp"
  27 #include "utilities/globalDefinitions.hpp"
  28 #include "unittest.hpp"
  29 
  30 #include <limits>
  31 
  32 // A few arbitrarily chosen values to test the align functions on.
  33 static uint64_t values[] = {1, 3, 10, 345, 1023, 1024, 1025, 23909034, INT_MAX, uint64_t(-1) / 2, uint64_t(-1) / 2 + 100, -1 };
  34 
  35 template <typename T>
  36 static T max_alignment() {
  37   T max = std::numeric_limits<T>::max();
  38   return max ^ (max >> 1);
  39 }
  40 
  41 #define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__).buffer())
  42 
  43 template <typename T, typename A>
  44 static void test_alignments() {
  45   log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
  46       std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
  47       std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
  48 
  49   // Test all possible alignment values that fit in type A.
  50   for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
  51     log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
  52 
  53     for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
  54       log("--- Value: " UINT64_FORMAT "\n", values[i]);
  55 
  56       // Test align up
  57       const uint64_t up = align_up_(values[i], (uint64_t)alignment);
  58       if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
  59         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);
  60 
  61         T value = T(values[i]);
  62 
  63         // Check against uint64_t version
  64         ASSERT_EQ(align_up((uint64_t)value, alignment), up);
  65         // Check inline function vs macro
  66         ASSERT_EQ(align_up(value, alignment), align_up_(value, alignment));
  67         // Sanity check
  68         ASSERT_GE(align_up(value, alignment), value);
  69       }
  70 
  71       // Test align down
  72       const uint64_t down = align_down_(values[i], (uint64_t)alignment);
  73       if (down <= (uint64_t)std::numeric_limits<T>::max()) {
  74         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);
  75 
  76         T value = T(values[i]);
  77 
  78         // Check against uint64_t version
  79         ASSERT_EQ((uint64_t)align_down(value, alignment), down);
  80         // Check inline function vs macro
  81         ASSERT_EQ(align_down(value, alignment), align_down_(value, alignment));
  82         // Sanity check
  83         ASSERT_LE(align_down(value, alignment), value);
  84       }
  85 
  86       // Test is aligned
  87       const bool is = is_aligned_(values[i], (uint64_t)alignment);
  88       if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
  89         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");
  90 
  91         T value = T(values[i]);
  92 
  93         // Check against uint64_t version
  94         ASSERT_EQ(is_aligned(value, alignment), is);
  95         // Check inline function vs macro
  96         ASSERT_EQ(is_aligned(value, alignment), is_aligned_(value, alignment));
  97       }
  98     }
  99   }
 100 }
 101 
 102 TEST(Align, functions_and_macros) {
 103   // Test the alignment functions with different type combinations.
 104 
 105   test_alignments<int64_t, uint8_t>();
 106   test_alignments<int64_t, uint16_t>();
 107   test_alignments<int64_t, uint32_t>();
 108   test_alignments<int64_t, int8_t>();
 109   test_alignments<int64_t, int16_t>();
 110   test_alignments<int64_t, int32_t>();
 111   test_alignments<int64_t, int64_t>();
 112 
 113   test_alignments<uint32_t, uint8_t>();
 114   test_alignments<uint32_t, uint16_t>();
 115   test_alignments<uint32_t, uint32_t>();
 116   test_alignments<uint32_t, int8_t>();
 117   test_alignments<uint32_t, int16_t>();
 118   test_alignments<uint32_t, int32_t>();
 119 
 120   test_alignments<int32_t, uint8_t>();
 121   test_alignments<int32_t, uint16_t>();
 122   test_alignments<int32_t, int8_t>();
 123   test_alignments<int32_t, int16_t>();
 124   test_alignments<int32_t, int32_t>();
 125 
 126   test_alignments<uint16_t, uint8_t>();
 127   test_alignments<uint16_t, uint16_t>();
 128   test_alignments<uint16_t, int8_t>();
 129   test_alignments<uint16_t, int16_t>();
 130 
 131   test_alignments<int16_t, uint8_t>();
 132   test_alignments<int16_t, int8_t>();
 133   test_alignments<int16_t, int16_t>();
 134 
 135   test_alignments<uint8_t, int8_t>();
 136   test_alignments<uint8_t, uint8_t>();
 137 
 138   test_alignments<int8_t, int8_t>();
 139 }