jdk

Форк
0
/
test_byteswap.cpp 
84 строки · 2.9 Кб
1
/*
2
 * Copyright (c) 2023, 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

25
#include "precompiled.hpp"
26
#include "utilities/byteswap.hpp"
27
#include "utilities/globalDefinitions.hpp"
28
#include "unittest.hpp"
29

30
template<typename T>
31
static inline void test_byteswap() {
32
  const int  NBIT = sizeof(T) * 8;
33
  const bool IS_U = (T)-1 > 0;
34
  const int XOR_REV_BITS = (NBIT - 1);
35
  const int XOR_REV_BITS_IN_BYTES = 7;  // only flip position in byte
36
  const int XOR_REV_BYTES = XOR_REV_BITS ^ XOR_REV_BITS_IN_BYTES;
37
  ASSERT_EQ(byteswap<T>((T)0), (T)0);
38
  ASSERT_EQ(byteswap<T>((T)-1), (T)-1);
39
  for (int i1 = 0; i1 < NBIT; i1++) {
40
    T mask1 = (T)1 << i1;
41
    T rbym1 = (T)1 << (i1 ^ XOR_REV_BYTES);
42
    for (int i2 = 0; i2 <= i1; i2++) {
43
      T mask2 = (T)1 << i2;
44
      T rbym2 = (T)1 << (i2 ^ XOR_REV_BYTES);
45
      T mask = mask1|mask2;
46
#define STUFF (IS_U?"u":"s") << NBIT << "@" << i1 << "," << i2
47
      ASSERT_EQ(byteswap<T>(mask), rbym1|rbym2) << STUFF;
48
      ASSERT_EQ((T)~byteswap<T>((T)~mask), rbym1|rbym2) << STUFF;
49
    }
50
  }
51
}
52

53
TEST_VM(utilities, byteswap) {
54
  test_byteswap<int64_t>();
55
  test_byteswap<uint64_t>();
56
  test_byteswap<int32_t>();
57
  test_byteswap<uint32_t>();
58
  test_byteswap<int16_t>();
59
  test_byteswap<uint16_t>();
60
  test_byteswap<int8_t>();
61
  test_byteswap<uint8_t>();
62
}
63

64
// Here is some object code to look at if we want to do a manual
65
// study.  One could find the build file named test_byteswap.o.cmdline
66
// and hand-edit the command line to produce assembly code in
67
// test_byteswap.s.
68
//
69
// Or, given the two empty "fence functions", one could do a
70
// quick scan like this:
71
//
72
// $ objdump -D $(find build/*release -name test_byteswap.o) \
73
//   | sed -n '/start_code_quality/,$p;/end_code_quality/q' \
74
//   | egrep -B10 bswap  # or grep -B20 cfi_endproc
75

76
void start_code_quality_byteswap() { }
77

78
int32_t code_quality_reverse_bytes_32(int32_t x) {
79
  return byteswap(x);
80
}
81

82
int64_t code_quality_reverse_bytes_64(int64_t x) {
83
  return byteswap(x);
84
}
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.