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