1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "instruction_set_features_riscv64.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace art HIDDEN {
22 
TEST(Riscv64InstructionSetFeaturesTest,Riscv64FeaturesFromDefaultVariant)23 TEST(Riscv64InstructionSetFeaturesTest, Riscv64FeaturesFromDefaultVariant) {
24   std::string error_msg;
25   std::unique_ptr<const InstructionSetFeatures> riscv64_features(
26       InstructionSetFeatures::FromVariant(InstructionSet::kRiscv64, "generic", &error_msg));
27   ASSERT_TRUE(riscv64_features.get() != nullptr) << error_msg;
28 
29   EXPECT_EQ(riscv64_features->GetInstructionSet(), InstructionSet::kRiscv64);
30 
31   EXPECT_TRUE(riscv64_features->Equals(riscv64_features.get()));
32 
33   // rv64gcv_zba_zbb_zbs, aka rv64imafdcv_zba_zbb_zbs
34   uint32_t expected_extensions =
35       Riscv64InstructionSetFeatures::kExtGeneric |
36       Riscv64InstructionSetFeatures::kExtCompressed |
37       Riscv64InstructionSetFeatures::kExtVector |
38       Riscv64InstructionSetFeatures::kExtZba |
39       Riscv64InstructionSetFeatures::kExtZbb |
40       Riscv64InstructionSetFeatures::kExtZbs;
41   EXPECT_EQ(riscv64_features->AsBitmap(), expected_extensions);
42 }
43 
TEST(Riscv64InstructionSetFeaturesTest,Riscv64FeaturesFromString)44 TEST(Riscv64InstructionSetFeaturesTest, Riscv64FeaturesFromString) {
45   std::string error_msg;
46   std::unique_ptr<const InstructionSetFeatures> generic_features(
47       InstructionSetFeatures::FromVariant(InstructionSet::kRiscv64, "generic", &error_msg));
48   ASSERT_TRUE(generic_features.get() != nullptr) << error_msg;
49 
50   std::unique_ptr<const InstructionSetFeatures> rv64gv_features(
51       generic_features->AddFeaturesFromString("rv64gv", &error_msg));
52   ASSERT_TRUE(rv64gv_features.get() != nullptr) << error_msg;
53 
54   EXPECT_FALSE(generic_features->Equals(rv64gv_features.get()));
55 
56   uint32_t expected_extensions = Riscv64InstructionSetFeatures::kExtGeneric |
57                                  Riscv64InstructionSetFeatures::kExtVector;
58   EXPECT_EQ(rv64gv_features->AsBitmap(), expected_extensions);
59 
60   std::unique_ptr<const InstructionSetFeatures> rv64gc_zba_zbb_features(
61       generic_features->AddFeaturesFromString("rv64gc_zba_zbb", &error_msg));
62   ASSERT_TRUE(rv64gc_zba_zbb_features.get() != nullptr) << error_msg;
63 
64   EXPECT_FALSE(generic_features->Equals(rv64gc_zba_zbb_features.get()));
65 
66   expected_extensions =
67       Riscv64InstructionSetFeatures::kExtGeneric |
68       Riscv64InstructionSetFeatures::kExtCompressed |
69       Riscv64InstructionSetFeatures::kExtZba |
70       Riscv64InstructionSetFeatures::kExtZbb;
71   EXPECT_EQ(rv64gc_zba_zbb_features->AsBitmap(), expected_extensions);
72 }
73 
74 }  // namespace art
75