clamp.c 5.6 KB

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