1*32afb93cSXin Li /*
2*32afb93cSXin Li  * Copyright (C) 2021 The Android Open Source Project
3*32afb93cSXin Li  *
4*32afb93cSXin Li  * Licensed under the Apache License, Version 2.0 (the "License");
5*32afb93cSXin Li  * you may not use this file except in compliance with the License.
6*32afb93cSXin Li  * You may obtain a copy of the License at
7*32afb93cSXin Li  *
8*32afb93cSXin Li  *      http://www.apache.org/licenses/LICENSE-2.0
9*32afb93cSXin Li  *
10*32afb93cSXin Li  * Unless required by applicable law or agreed to in writing, software
11*32afb93cSXin Li  * distributed under the License is distributed on an "AS IS" BASIS,
12*32afb93cSXin Li  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*32afb93cSXin Li  * See the License for the specific language governing permissions and
14*32afb93cSXin Li  * limitations under the License.
15*32afb93cSXin Li  */
16*32afb93cSXin Li 
17*32afb93cSXin Li #include "Utils.h"
18*32afb93cSXin Li 
19*32afb93cSXin Li #include <cpu-features.h>
20*32afb93cSXin Li 
21*32afb93cSXin Li #include "RenderScriptToolkit.h"
22*32afb93cSXin Li 
23*32afb93cSXin Li namespace renderscript {
24*32afb93cSXin Li 
25*32afb93cSXin Li #define LOG_TAG "renderscript.toolkit.Utils"
26*32afb93cSXin Li 
cpuSupportsSimd()27*32afb93cSXin Li bool cpuSupportsSimd() {
28*32afb93cSXin Li     AndroidCpuFamily family = android_getCpuFamily();
29*32afb93cSXin Li     uint64_t features = android_getCpuFeatures();
30*32afb93cSXin Li 
31*32afb93cSXin Li     if (family == ANDROID_CPU_FAMILY_ARM && (features & ANDROID_CPU_ARM_FEATURE_NEON)) {
32*32afb93cSXin Li         // ALOGI("Arm with Neon");
33*32afb93cSXin Li         return true;
34*32afb93cSXin Li     } else if (family == ANDROID_CPU_FAMILY_ARM64 && (features & ANDROID_CPU_ARM64_FEATURE_ASIMD)) {
35*32afb93cSXin Li         // ALOGI("Arm64 with ASIMD");
36*32afb93cSXin Li         return true;
37*32afb93cSXin Li     } else if ((family == ANDROID_CPU_FAMILY_X86 || family == ANDROID_CPU_FAMILY_X86_64) &&
38*32afb93cSXin Li                (features & ANDROID_CPU_X86_FEATURE_SSSE3)) {
39*32afb93cSXin Li         // ALOGI("x86* with SSE3");
40*32afb93cSXin Li         return true;
41*32afb93cSXin Li     }
42*32afb93cSXin Li     // ALOGI("Not simd");
43*32afb93cSXin Li     return false;
44*32afb93cSXin Li }
45*32afb93cSXin Li 
46*32afb93cSXin Li #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
validRestriction(const char * tag,size_t sizeX,size_t sizeY,const Restriction * restriction)47*32afb93cSXin Li bool validRestriction(const char* tag, size_t sizeX, size_t sizeY, const Restriction* restriction) {
48*32afb93cSXin Li     if (restriction == nullptr) {
49*32afb93cSXin Li         return true;
50*32afb93cSXin Li     }
51*32afb93cSXin Li     if (restriction->startX >= sizeX || restriction->endX > sizeX) {
52*32afb93cSXin Li         ALOGE("%s. sizeX should be greater than restriction->startX and greater or equal to "
53*32afb93cSXin Li               "restriction->endX. %zu, %zu, and %zu were provided respectively.",
54*32afb93cSXin Li               tag, sizeX, restriction->startX, restriction->endY);
55*32afb93cSXin Li         return false;
56*32afb93cSXin Li     }
57*32afb93cSXin Li     if (restriction->startY >= sizeY && restriction->endY > sizeY) {
58*32afb93cSXin Li         ALOGE("%s. sizeY should be greater than restriction->startY and greater or equal to "
59*32afb93cSXin Li               "restriction->endY. %zu, %zu, and %zu were provided respectively.",
60*32afb93cSXin Li               tag, sizeY, restriction->startY, restriction->endY);
61*32afb93cSXin Li         return false;
62*32afb93cSXin Li     }
63*32afb93cSXin Li     if (restriction->startX >= restriction->endX) {
64*32afb93cSXin Li         ALOGE("%s. Restriction startX should be less than endX. "
65*32afb93cSXin Li               "%zu and %zu were provided respectively.",
66*32afb93cSXin Li               tag, restriction->startX, restriction->endX);
67*32afb93cSXin Li         return false;
68*32afb93cSXin Li     }
69*32afb93cSXin Li     if (restriction->startY >= restriction->endY) {
70*32afb93cSXin Li         ALOGE("%s. Restriction startY should be less than endY. "
71*32afb93cSXin Li               "%zu and %zu were provided respectively.",
72*32afb93cSXin Li               tag, restriction->startY, restriction->endY);
73*32afb93cSXin Li         return false;
74*32afb93cSXin Li     }
75*32afb93cSXin Li     return true;
76*32afb93cSXin Li }
77*32afb93cSXin Li #endif
78*32afb93cSXin Li 
79*32afb93cSXin Li }  // namespace renderscript
80