1 /*
2  * Copyright (C) 2014 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_x86.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace art {
22 
TEST(X86InstructionSetFeaturesTest,X86FeaturesFromDefaultVariant)23 TEST(X86InstructionSetFeaturesTest, X86FeaturesFromDefaultVariant) {
24   std::string error_msg;
25   std::unique_ptr<const InstructionSetFeatures> x86_features(
26       InstructionSetFeatures::FromVariant(kX86, "default", &error_msg));
27   ASSERT_TRUE(x86_features.get() != nullptr) << error_msg;
28   EXPECT_EQ(x86_features->GetInstructionSet(), kX86);
29   EXPECT_TRUE(x86_features->Equals(x86_features.get()));
30   EXPECT_STREQ("smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2", x86_features->GetFeatureString().c_str());
31   EXPECT_EQ(x86_features->AsBitmap(), 1U);
32 }
33 
TEST(X86InstructionSetFeaturesTest,X86FeaturesFromAtomVariant)34 TEST(X86InstructionSetFeaturesTest, X86FeaturesFromAtomVariant) {
35   // Build features for a 32-bit x86 atom processor.
36   std::string error_msg;
37   std::unique_ptr<const InstructionSetFeatures> x86_features(
38       InstructionSetFeatures::FromVariant(kX86, "atom", &error_msg));
39   ASSERT_TRUE(x86_features.get() != nullptr) << error_msg;
40   EXPECT_EQ(x86_features->GetInstructionSet(), kX86);
41   EXPECT_TRUE(x86_features->Equals(x86_features.get()));
42   EXPECT_STREQ("smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2", x86_features->GetFeatureString().c_str());
43   EXPECT_EQ(x86_features->AsBitmap(), 3U);
44 
45   // Build features for a 32-bit x86 default processor.
46   std::unique_ptr<const InstructionSetFeatures> x86_default_features(
47       InstructionSetFeatures::FromVariant(kX86, "default", &error_msg));
48   ASSERT_TRUE(x86_default_features.get() != nullptr) << error_msg;
49   EXPECT_EQ(x86_default_features->GetInstructionSet(), kX86);
50   EXPECT_TRUE(x86_default_features->Equals(x86_default_features.get()));
51   EXPECT_STREQ("smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2",
52                x86_default_features->GetFeatureString().c_str());
53   EXPECT_EQ(x86_default_features->AsBitmap(), 1U);
54 
55   // Build features for a 64-bit x86-64 atom processor.
56   std::unique_ptr<const InstructionSetFeatures> x86_64_features(
57       InstructionSetFeatures::FromVariant(kX86_64, "atom", &error_msg));
58   ASSERT_TRUE(x86_64_features.get() != nullptr) << error_msg;
59   EXPECT_EQ(x86_64_features->GetInstructionSet(), kX86_64);
60   EXPECT_TRUE(x86_64_features->Equals(x86_64_features.get()));
61   EXPECT_STREQ("smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2",
62                x86_64_features->GetFeatureString().c_str());
63   EXPECT_EQ(x86_64_features->AsBitmap(), 3U);
64 
65   EXPECT_FALSE(x86_64_features->Equals(x86_features.get()));
66   EXPECT_FALSE(x86_64_features->Equals(x86_default_features.get()));
67   EXPECT_FALSE(x86_features->Equals(x86_default_features.get()));
68 }
69 
TEST(X86InstructionSetFeaturesTest,X86FeaturesFromSilvermontVariant)70 TEST(X86InstructionSetFeaturesTest, X86FeaturesFromSilvermontVariant) {
71   // Build features for a 32-bit x86 silvermont processor.
72   std::string error_msg;
73   std::unique_ptr<const InstructionSetFeatures> x86_features(
74       InstructionSetFeatures::FromVariant(kX86, "silvermont", &error_msg));
75   ASSERT_TRUE(x86_features.get() != nullptr) << error_msg;
76   EXPECT_EQ(x86_features->GetInstructionSet(), kX86);
77   EXPECT_TRUE(x86_features->Equals(x86_features.get()));
78   EXPECT_STREQ("smp,ssse3,sse4.1,sse4.2,-avx,-avx2", x86_features->GetFeatureString().c_str());
79   EXPECT_EQ(x86_features->AsBitmap(), 15U);
80 
81   // Build features for a 32-bit x86 default processor.
82   std::unique_ptr<const InstructionSetFeatures> x86_default_features(
83       InstructionSetFeatures::FromVariant(kX86, "default", &error_msg));
84   ASSERT_TRUE(x86_default_features.get() != nullptr) << error_msg;
85   EXPECT_EQ(x86_default_features->GetInstructionSet(), kX86);
86   EXPECT_TRUE(x86_default_features->Equals(x86_default_features.get()));
87   EXPECT_STREQ("smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2",
88                x86_default_features->GetFeatureString().c_str());
89   EXPECT_EQ(x86_default_features->AsBitmap(), 1U);
90 
91   // Build features for a 64-bit x86-64 silvermont processor.
92   std::unique_ptr<const InstructionSetFeatures> x86_64_features(
93       InstructionSetFeatures::FromVariant(kX86_64, "silvermont", &error_msg));
94   ASSERT_TRUE(x86_64_features.get() != nullptr) << error_msg;
95   EXPECT_EQ(x86_64_features->GetInstructionSet(), kX86_64);
96   EXPECT_TRUE(x86_64_features->Equals(x86_64_features.get()));
97   EXPECT_STREQ("smp,ssse3,sse4.1,sse4.2,-avx,-avx2",
98                x86_64_features->GetFeatureString().c_str());
99   EXPECT_EQ(x86_64_features->AsBitmap(), 15U);
100 
101   EXPECT_FALSE(x86_64_features->Equals(x86_features.get()));
102   EXPECT_FALSE(x86_64_features->Equals(x86_default_features.get()));
103   EXPECT_FALSE(x86_features->Equals(x86_default_features.get()));
104 }
105 
106 }  // namespace art
107