xref: /aosp_15_r20/frameworks/rs/driver/runtime/arch/clamp.c (revision e1eccf28f96817838ad6867f7f39d2351ec11f56)
1*e1eccf28SAndroid Build Coastguard Worker /*
2*e1eccf28SAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*e1eccf28SAndroid Build Coastguard Worker  *
4*e1eccf28SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1eccf28SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1eccf28SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1eccf28SAndroid Build Coastguard Worker  *
8*e1eccf28SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1eccf28SAndroid Build Coastguard Worker  *
10*e1eccf28SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1eccf28SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1eccf28SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1eccf28SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1eccf28SAndroid Build Coastguard Worker  * limitations under the License.
15*e1eccf28SAndroid Build Coastguard Worker  */
16*e1eccf28SAndroid Build Coastguard Worker 
17*e1eccf28SAndroid Build Coastguard Worker #include "rs_core.rsh"
18*e1eccf28SAndroid Build Coastguard Worker 
19*e1eccf28SAndroid Build Coastguard Worker typedef unsigned long long ull;
20*e1eccf28SAndroid Build Coastguard Worker typedef unsigned long long ull2 __attribute__((ext_vector_type(2)));
21*e1eccf28SAndroid Build Coastguard Worker typedef unsigned long long ull3 __attribute__((ext_vector_type(3)));
22*e1eccf28SAndroid Build Coastguard Worker typedef unsigned long long ull4 __attribute__((ext_vector_type(4)));
23*e1eccf28SAndroid Build Coastguard Worker 
24*e1eccf28SAndroid Build Coastguard Worker #define S_CLAMP(T) \
25*e1eccf28SAndroid Build Coastguard Worker extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
26*e1eccf28SAndroid Build Coastguard Worker     return amount < low ? low : (amount > high ? high : amount);                    \
27*e1eccf28SAndroid Build Coastguard Worker }
28*e1eccf28SAndroid Build Coastguard Worker 
29*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(half);
30*e1eccf28SAndroid Build Coastguard Worker //_CLAMP(float);  implemented in .ll
31*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(double);
32*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(char);
33*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(uchar);
34*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(short);
35*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(ushort);
36*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(int);
37*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(uint);
38*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(long);
39*e1eccf28SAndroid Build Coastguard Worker S_CLAMP(ulong);
40*e1eccf28SAndroid Build Coastguard Worker 
41*e1eccf28SAndroid Build Coastguard Worker #undef S_CLAMP
42*e1eccf28SAndroid Build Coastguard Worker 
43*e1eccf28SAndroid Build Coastguard Worker 
44*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
45*e1eccf28SAndroid Build Coastguard Worker #define V_CLAMP(T) \
46*e1eccf28SAndroid Build Coastguard Worker extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
47*e1eccf28SAndroid Build Coastguard Worker     T##2 r;                                                                         \
48*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
49*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
50*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
51*e1eccf28SAndroid Build Coastguard Worker }                                                                                   \
52*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
53*e1eccf28SAndroid Build Coastguard Worker extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
54*e1eccf28SAndroid Build Coastguard Worker     T##3 r;                                                                         \
55*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
56*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
57*e1eccf28SAndroid Build Coastguard Worker     r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
58*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
59*e1eccf28SAndroid Build Coastguard Worker }                                                                                   \
60*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
61*e1eccf28SAndroid Build Coastguard Worker extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
62*e1eccf28SAndroid Build Coastguard Worker     T##4 r;                                                                         \
63*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
64*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
65*e1eccf28SAndroid Build Coastguard Worker     r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
66*e1eccf28SAndroid Build Coastguard Worker     r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
67*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
68*e1eccf28SAndroid Build Coastguard Worker }                                                                                   \
69*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
70*e1eccf28SAndroid Build Coastguard Worker extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
71*e1eccf28SAndroid Build Coastguard Worker     T##2 r;                                                                         \
72*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
73*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
74*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
75*e1eccf28SAndroid Build Coastguard Worker }                                                                                   \
76*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
77*e1eccf28SAndroid Build Coastguard Worker extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
78*e1eccf28SAndroid Build Coastguard Worker     T##3 r;                                                                         \
79*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
80*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
81*e1eccf28SAndroid Build Coastguard Worker     r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
82*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
83*e1eccf28SAndroid Build Coastguard Worker }                                                                                   \
84*e1eccf28SAndroid Build Coastguard Worker                                                                                     \
85*e1eccf28SAndroid Build Coastguard Worker extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
86*e1eccf28SAndroid Build Coastguard Worker     T##4 r;                                                                         \
87*e1eccf28SAndroid Build Coastguard Worker     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
88*e1eccf28SAndroid Build Coastguard Worker     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
89*e1eccf28SAndroid Build Coastguard Worker     r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
90*e1eccf28SAndroid Build Coastguard Worker     r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
91*e1eccf28SAndroid Build Coastguard Worker     return r;                                                                       \
92*e1eccf28SAndroid Build Coastguard Worker }
93*e1eccf28SAndroid Build Coastguard Worker 
94*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(half);
95*e1eccf28SAndroid Build Coastguard Worker //V_CLAMP(float);  implemented in .ll
96*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(double);
97*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(char);
98*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(uchar);
99*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(short);
100*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(ushort);
101*e1eccf28SAndroid Build Coastguard Worker #if !defined(ARCH_ARM_HAVE_NEON) && !defined (ARCH_ARM64_HAVE_NEON)
102*e1eccf28SAndroid Build Coastguard Worker     V_CLAMP(int);  //implemented in .ll
103*e1eccf28SAndroid Build Coastguard Worker     V_CLAMP(uint);  //implemented in .ll
104*e1eccf28SAndroid Build Coastguard Worker #endif
105*e1eccf28SAndroid Build Coastguard Worker 
106*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(long);
107*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(ulong);
108*e1eccf28SAndroid Build Coastguard Worker V_CLAMP(ull);
109*e1eccf28SAndroid Build Coastguard Worker 
110*e1eccf28SAndroid Build Coastguard Worker #undef _CLAMP
111