1 /*
2  * Copyright (C) 2021 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 "Utils.h"
18 
19 #include <cpu-features.h>
20 
21 #include "RenderScriptToolkit.h"
22 
23 namespace renderscript {
24 
25 #define LOG_TAG "renderscript.toolkit.Utils"
26 
cpuSupportsSimd()27 bool cpuSupportsSimd() {
28     AndroidCpuFamily family = android_getCpuFamily();
29     uint64_t features = android_getCpuFeatures();
30 
31     if (family == ANDROID_CPU_FAMILY_ARM && (features & ANDROID_CPU_ARM_FEATURE_NEON)) {
32         // ALOGI("Arm with Neon");
33         return true;
34     } else if (family == ANDROID_CPU_FAMILY_ARM64 && (features & ANDROID_CPU_ARM64_FEATURE_ASIMD)) {
35         // ALOGI("Arm64 with ASIMD");
36         return true;
37     } else if ((family == ANDROID_CPU_FAMILY_X86 || family == ANDROID_CPU_FAMILY_X86_64) &&
38                (features & ANDROID_CPU_X86_FEATURE_SSSE3)) {
39         // ALOGI("x86* with SSE3");
40         return true;
41     }
42     // ALOGI("Not simd");
43     return false;
44 }
45 
46 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
validRestriction(const char * tag,size_t sizeX,size_t sizeY,const Restriction * restriction)47 bool validRestriction(const char* tag, size_t sizeX, size_t sizeY, const Restriction* restriction) {
48     if (restriction == nullptr) {
49         return true;
50     }
51     if (restriction->startX >= sizeX || restriction->endX > sizeX) {
52         ALOGE("%s. sizeX should be greater than restriction->startX and greater or equal to "
53               "restriction->endX. %zu, %zu, and %zu were provided respectively.",
54               tag, sizeX, restriction->startX, restriction->endY);
55         return false;
56     }
57     if (restriction->startY >= sizeY && restriction->endY > sizeY) {
58         ALOGE("%s. sizeY should be greater than restriction->startY and greater or equal to "
59               "restriction->endY. %zu, %zu, and %zu were provided respectively.",
60               tag, sizeY, restriction->startY, restriction->endY);
61         return false;
62     }
63     if (restriction->startX >= restriction->endX) {
64         ALOGE("%s. Restriction startX should be less than endX. "
65               "%zu and %zu were provided respectively.",
66               tag, restriction->startX, restriction->endX);
67         return false;
68     }
69     if (restriction->startY >= restriction->endY) {
70         ALOGE("%s. Restriction startY should be less than endY. "
71               "%zu and %zu were provided respectively.",
72               tag, restriction->startY, restriction->endY);
73         return false;
74     }
75     return true;
76 }
77 #endif
78 
79 }  // namespace renderscript
80