clamp.c revision 54cd5d1771ea5c95e181befc66ef8e2a2c1b78cd
1/*
2 * Copyright (C) 2013 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 "rs_core.rsh"
18
19typedef unsigned long long ull;
20typedef unsigned long long ull2 __attribute__((ext_vector_type(2)));
21typedef unsigned long long ull3 __attribute__((ext_vector_type(3)));
22typedef unsigned long long ull4 __attribute__((ext_vector_type(4)));
23
24#define S_CLAMP(T) \
25extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
26    return amount < low ? low : (amount > high ? high : amount);                    \
27}
28
29S_CLAMP(half);
30//_CLAMP(float);  implemented in .ll
31S_CLAMP(double);
32S_CLAMP(char);
33S_CLAMP(uchar);
34S_CLAMP(short);
35S_CLAMP(ushort);
36S_CLAMP(int);
37S_CLAMP(uint);
38S_CLAMP(long);
39S_CLAMP(ulong);
40
41#undef S_CLAMP
42
43
44                                                                                    \
45#define V_CLAMP(T) \
46extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
47    T##2 r;                                                                         \
48    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
49    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
50    return r;                                                                       \
51}                                                                                   \
52                                                                                    \
53extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
54    T##3 r;                                                                         \
55    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
56    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
57    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
58    return r;                                                                       \
59}                                                                                   \
60                                                                                    \
61extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
62    T##4 r;                                                                         \
63    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
64    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
65    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
66    r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
67    return r;                                                                       \
68}                                                                                   \
69                                                                                    \
70extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
71    T##2 r;                                                                         \
72    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
73    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
74    return r;                                                                       \
75}                                                                                   \
76                                                                                    \
77extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
78    T##3 r;                                                                         \
79    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
80    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
81    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
82    return r;                                                                       \
83}                                                                                   \
84                                                                                    \
85extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
86    T##4 r;                                                                         \
87    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
88    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
89    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
90    r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
91    return r;                                                                       \
92}
93
94V_CLAMP(half);
95//V_CLAMP(float);  implemented in .ll
96V_CLAMP(double);
97V_CLAMP(char);
98V_CLAMP(uchar);
99V_CLAMP(short);
100V_CLAMP(ushort);
101#if !defined(ARCH_ARM_HAVE_NEON) && !defined (ARCH_ARM64_HAVE_NEON)
102    V_CLAMP(int);  //implemented in .ll
103    V_CLAMP(uint);  //implemented in .ll
104#endif
105
106V_CLAMP(long);
107V_CLAMP(ulong);
108V_CLAMP(ull);
109
110#undef _CLAMP
111