1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | #include "llvm/Support/TypeSize.h" |
9 | #include "gtest/gtest.h" |
10 | |
11 | using namespace llvm; |
12 | |
13 | namespace { |
14 | |
15 | constexpr ElementCount CEElementCount = ElementCount(); |
16 | |
17 | static_assert(!CEElementCount.isScalar()); |
18 | static_assert(!CEElementCount.isVector()); |
19 | |
20 | constexpr ElementCount CEElementCountFixed1 = ElementCount::getFixed(MinVal: 1); |
21 | static_assert(CEElementCountFixed1.isScalar()); |
22 | static_assert(!CEElementCountFixed1.isVector()); |
23 | static_assert(!CEElementCountFixed1.isScalable()); |
24 | |
25 | constexpr ElementCount CEElementCountFixed3 = ElementCount::getFixed(MinVal: 3); |
26 | constexpr ElementCount CEElementCountFixed4 = ElementCount::getFixed(MinVal: 4); |
27 | |
28 | static_assert(!CEElementCountFixed4.isScalar()); |
29 | static_assert(CEElementCountFixed4.isVector()); |
30 | static_assert(CEElementCountFixed4.isKnownEven()); |
31 | static_assert(!CEElementCountFixed3.isKnownEven()); |
32 | static_assert(!CEElementCountFixed4.isScalable()); |
33 | static_assert(!CEElementCountFixed3.isScalable()); |
34 | |
35 | constexpr ElementCount CEElementCountScalable4 = ElementCount::getScalable(MinVal: 4); |
36 | |
37 | static_assert(CEElementCountScalable4.isScalable()); |
38 | static_assert(!ElementCount().isScalable()); |
39 | static_assert( |
40 | CEElementCountScalable4.hasKnownScalarFactor(RHS: ElementCount::getScalable(MinVal: 2))); |
41 | static_assert(ElementCount::getScalable(MinVal: 8).getKnownScalarFactor( |
42 | RHS: ElementCount::getScalable(MinVal: 2)) == 4); |
43 | |
44 | static_assert(CEElementCountScalable4 == ElementCount::get(MinVal: 4, Scalable: true)); |
45 | static_assert(CEElementCountFixed4 == ElementCount::get(MinVal: 4, Scalable: false)); |
46 | static_assert(ElementCount::isKnownLT(LHS: CEElementCountFixed3, |
47 | RHS: CEElementCountFixed4)); |
48 | static_assert(ElementCount::isKnownLE(LHS: CEElementCountFixed3, |
49 | RHS: CEElementCountFixed4)); |
50 | static_assert(ElementCount::isKnownGT(LHS: CEElementCountFixed4, |
51 | RHS: CEElementCountFixed3)); |
52 | static_assert(ElementCount::isKnownGE(LHS: CEElementCountFixed4, |
53 | RHS: CEElementCountFixed3)); |
54 | static_assert(CEElementCountFixed3.coefficientNextPowerOf2() == |
55 | CEElementCountFixed4); |
56 | static_assert(ElementCount::getFixed(MinVal: 8).divideCoefficientBy(RHS: 2) == |
57 | ElementCount::getFixed(MinVal: 4)); |
58 | static_assert(ElementCount::getFixed(MinVal: 8).multiplyCoefficientBy(RHS: 3) == |
59 | ElementCount::getFixed(MinVal: 24)); |
60 | static_assert(ElementCount::getFixed(MinVal: 8).isKnownMultipleOf(RHS: 2)); |
61 | |
62 | constexpr TypeSize TSFixed0 = TypeSize::getFixed(ExactSize: 0); |
63 | constexpr TypeSize TSFixed1 = TypeSize::getFixed(ExactSize: 1); |
64 | constexpr TypeSize TSFixed32 = TypeSize::getFixed(ExactSize: 32); |
65 | |
66 | static_assert(TSFixed0.getFixedValue() == 0); |
67 | static_assert(TSFixed1.getFixedValue() == 1); |
68 | static_assert(TSFixed32.getFixedValue() == 32); |
69 | static_assert(TSFixed32.getKnownMinValue() == 32); |
70 | |
71 | static_assert(TypeSize::getScalable(MinimumSize: 32).getKnownMinValue() == 32); |
72 | |
73 | static_assert(TSFixed32 * 2 == TypeSize::getFixed(ExactSize: 64)); |
74 | static_assert(TSFixed32 * 2u == TypeSize::getFixed(ExactSize: 64)); |
75 | static_assert(TSFixed32 * INT64_C(2) == TypeSize::getFixed(ExactSize: 64)); |
76 | static_assert(TSFixed32 * UINT64_C(2) == TypeSize::getFixed(ExactSize: 64)); |
77 | |
78 | static_assert(2 * TSFixed32 == TypeSize::getFixed(ExactSize: 64)); |
79 | static_assert(2u * TSFixed32 == TypeSize::getFixed(ExactSize: 64)); |
80 | static_assert(INT64_C(2) * TSFixed32 == TypeSize::getFixed(ExactSize: 64)); |
81 | static_assert(UINT64_C(2) * TSFixed32 == TypeSize::getFixed(ExactSize: 64)); |
82 | static_assert(alignTo(Size: TypeSize::getFixed(ExactSize: 7), Align: 8) == TypeSize::getFixed(ExactSize: 8)); |
83 | |
84 | static_assert(TypeSize::getZero() == TypeSize::getFixed(ExactSize: 0)); |
85 | static_assert(TypeSize::getZero() != TypeSize::getScalable(MinimumSize: 0)); |
86 | static_assert(TypeSize::getFixed(ExactSize: 0) != TypeSize::getScalable(MinimumSize: 0)); |
87 | static_assert(TypeSize::getFixed(ExactSize: 0).isZero()); |
88 | static_assert(TypeSize::getScalable(MinimumSize: 0).isZero()); |
89 | static_assert(TypeSize::getZero().isZero()); |
90 | static_assert(TypeSize::getFixed(ExactSize: 0) == |
91 | (TypeSize::getFixed(ExactSize: 4) - TypeSize::getFixed(ExactSize: 4))); |
92 | static_assert(TypeSize::getScalable(MinimumSize: 0) == |
93 | (TypeSize::getScalable(MinimumSize: 4) - TypeSize::getScalable(MinimumSize: 4))); |
94 | static_assert(TypeSize::getFixed(ExactSize: 0) + TypeSize::getScalable(MinimumSize: 8) == |
95 | TypeSize::getScalable(MinimumSize: 8)); |
96 | static_assert(TypeSize::getScalable(MinimumSize: 8) + TypeSize::getFixed(ExactSize: 0) == |
97 | TypeSize::getScalable(MinimumSize: 8)); |
98 | static_assert(TypeSize::getFixed(ExactSize: 8) + TypeSize::getScalable(MinimumSize: 0) == |
99 | TypeSize::getFixed(ExactSize: 8)); |
100 | static_assert(TypeSize::getScalable(MinimumSize: 0) + TypeSize::getFixed(ExactSize: 8) == |
101 | TypeSize::getFixed(ExactSize: 8)); |
102 | static_assert(TypeSize::getScalable(MinimumSize: 8) - TypeSize::getFixed(ExactSize: 0) == |
103 | TypeSize::getScalable(MinimumSize: 8)); |
104 | static_assert(TypeSize::getFixed(ExactSize: 8) - TypeSize::getScalable(MinimumSize: 0) == |
105 | TypeSize::getFixed(ExactSize: 8)); |
106 | |
107 | TEST(TypeSize, FailIncompatibleTypes) { |
108 | EXPECT_DEBUG_DEATH(TypeSize::getFixed(8) + TypeSize::getScalable(8), |
109 | "Incompatible types" ); |
110 | } |
111 | |
112 | } // namespace |
113 | |