aboutsummaryrefslogtreecommitdiff
path: root/tests/device/test-cpufeatures/jni/test_cpufeatures.c
blob: 861032a69232fe08ae10ebde678993b2445a514b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define __STDC_FORMAT_MACROS 1

#include <cpu-features.h>
#include <inttypes.h>
#include <stdio.h>

int main(void)
{
    AndroidCpuFamily family = android_getCpuFamily();
    switch (family) {
    case ANDROID_CPU_FAMILY_ARM:
        printf("CPU family is ARM\n");
        break;
    case ANDROID_CPU_FAMILY_X86:
        printf("CPU family is x86\n");
        break;
    case ANDROID_CPU_FAMILY_MIPS:
        printf("CPU family is MIPS\n");
        break;
    case ANDROID_CPU_FAMILY_ARM64:
        printf("CPU family is ARM64\n");
        break;
    case ANDROID_CPU_FAMILY_X86_64:
        printf("CPU family is x86_64\n");
        break;
    case ANDROID_CPU_FAMILY_MIPS64:
        printf("CPU family is MIPS64\n");
        break;
    default:
        fprintf(stderr, "Unsupported CPU family: %d\n", family);
        return 1;
    }

    if (family == ANDROID_CPU_FAMILY_ARM) {
        uint64_t features = android_getCpuFeatures();
        printf( "Supported ARM features:\n");
#define CHECK(name) \
        if ((features & ANDROID_CPU_ARM_FEATURE_## name) != 0) { \
            printf( "  "#name"\n" ); \
        }
        CHECK(LDREX_STREX)
        CHECK(VFPv2)
        CHECK(ARMv7)
        CHECK(VFPv3)
        CHECK(VFP_D32)
        CHECK(VFP_FP16)
        CHECK(VFP_FMA)
        CHECK(NEON)
        CHECK(NEON_FMA)
        CHECK(IDIV_ARM)
        CHECK(IDIV_THUMB2)
        CHECK(iWMMXt)
#undef CHECK
    }

#ifdef __arm__
    uint32_t cpu_id = android_getCpuIdArm();
    printf( "ARM CpuID: %08x\n", cpu_id);
    printf( "   implementer: %02x\n", (cpu_id >> 24) & 0xff);
    printf( "   variant    : %02x\n", (cpu_id >> 20) & 0x0f);
    printf( "   part       : %03x\n", (cpu_id >> 4) & 0xfff);
    printf( "   revision   : %x\n",    cpu_id & 0x0f);
#endif

    if (family == ANDROID_CPU_FAMILY_X86) {
        uint64_t features = android_getCpuFeatures();
        printf( "Supported x86 features:\n");
#define CHECK(name) \
        if ((features & ANDROID_CPU_X86_FEATURE_## name) != 0) { \
            printf( "  "#name"\n" ); \
        }
        CHECK(SSSE3)
        CHECK(POPCNT)
        CHECK(MOVBE)
#undef CHECK
    }

    int result = 0;

    // Check that the CPU features mask is empty for anything that isn't
    // 32-bit ARM or 32-bit x86.
    if (family != ANDROID_CPU_FAMILY_ARM &&
        family != ANDROID_CPU_FAMILY_X86) {
        uint64_t features = android_getCpuFeatures();
        if (features != 0) {
            printf("ERROR: Unexpected CPU features mask: %016" PRIX64 "\n",
                   features);
            result = 1;
        }
    }

    int count = android_getCpuCount();
    printf( "Number of CPU cores: %d\n", count);
    return result;
}