summaryrefslogtreecommitdiff
path: root/driver/runtime/arch/clamp.c
blob: 86d6f96fb69b72efbd73ac36b70003ab1a20d2bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "rs_core.rsh"

typedef unsigned long long ull;
typedef unsigned long long ull2 __attribute__((ext_vector_type(2)));
typedef unsigned long long ull3 __attribute__((ext_vector_type(3)));
typedef unsigned long long ull4 __attribute__((ext_vector_type(4)));

#define S_CLAMP(T) \
extern T __attribute__((overloadable)) clamp(T amount, T low, T high) {             \
    return amount < low ? low : (amount > high ? high : amount);                    \
}

//_CLAMP(float);  implemented in .ll
S_CLAMP(double);
S_CLAMP(char);
S_CLAMP(uchar);
S_CLAMP(short);
S_CLAMP(ushort);
S_CLAMP(int);
S_CLAMP(uint);
S_CLAMP(long);
S_CLAMP(ulong);

#undef S_CLAMP


                                                                                    \
#define V_CLAMP(T) \
extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T##2 low, T##2 high) { \
    T##2 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T##3 low, T##3 high) { \
    T##3 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T##4 low, T##4 high) { \
    T##4 r;                                                                         \
    r.x = amount.x < low.x ? low.x : (amount.x > high.x ? high.x : amount.x);       \
    r.y = amount.y < low.y ? low.y : (amount.y > high.y ? high.y : amount.y);       \
    r.z = amount.z < low.z ? low.z : (amount.z > high.z ? high.z : amount.z);       \
    r.w = amount.w < low.w ? low.w : (amount.w > high.w ? high.w : amount.w);       \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##2 __attribute__((overloadable)) clamp(T##2 amount, T low, T high) {       \
    T##2 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##3 __attribute__((overloadable)) clamp(T##3 amount, T low, T high) {       \
    T##3 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
    return r;                                                                       \
}                                                                                   \
                                                                                    \
extern T##4 __attribute__((overloadable)) clamp(T##4 amount, T low, T high) {       \
    T##4 r;                                                                         \
    r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);               \
    r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);               \
    r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);               \
    r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);               \
    return r;                                                                       \
}

//V_CLAMP(float);  implemented in .ll
V_CLAMP(double);
V_CLAMP(char);
V_CLAMP(uchar);
V_CLAMP(short);
V_CLAMP(ushort);
#if !defined(ARCH_ARM_HAVE_NEON) && !defined (ARCH_ARM64_HAVE_NEON)
    V_CLAMP(int);  //implemented in .ll
    V_CLAMP(uint);  //implemented in .ll
#endif

V_CLAMP(long);
V_CLAMP(ulong);
V_CLAMP(ull);

#undef _CLAMP