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