[libclc] Move tan to the CLC library (#139547)

There was already a __clc_tan in the OpenCL layer. This commit moves the
function over whilst vectorizing it.

The function __clc_tan is no longer a public symbol, which should have
never been the case.

GitOrigin-RevId: 87978ea2723d39d3045bfe7624e198389ad35749
diff --git a/generic/include/math/clc_tan.h b/clc/include/clc/math/clc_tan.h
similarity index 83%
rename from generic/include/math/clc_tan.h
rename to clc/include/clc/math/clc_tan.h
index 52c0a34..028ff28 100644
--- a/generic/include/math/clc_tan.h
+++ b/clc/include/clc/math/clc_tan.h
@@ -6,7 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define __CLC_FUNCTION __clc_tan
+#ifndef __CLC_MATH_CLC_TAN_H__
+#define __CLC_MATH_CLC_TAN_H__
+
 #define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_tan
+
 #include <clc/math/gentype.inc>
+
 #undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_TAN_H__
diff --git a/clc/lib/generic/SOURCES b/clc/lib/generic/SOURCES
index 4d66c74..0a839e8 100644
--- a/clc/lib/generic/SOURCES
+++ b/clc/lib/generic/SOURCES
@@ -95,6 +95,7 @@
 math/clc_sqrt.cl
 math/clc_sw_fma.cl
 math/clc_tables.cl
+math/clc_tan.cl
 math/clc_tanh.cl
 math/clc_tanpi.cl
 math/clc_tgamma.cl
diff --git a/clc/lib/generic/math/clc_sincos_helpers.inc b/clc/lib/generic/math/clc_sincos_helpers.inc
index 516a40c..29c7421 100644
--- a/clc/lib/generic/math/clc_sincos_helpers.inc
+++ b/clc/lib/generic/math/clc_sincos_helpers.inc
@@ -90,7 +90,7 @@
   __CLC_FLOATN t = __clc_mad(x * r, __clc_native_divide(a, b), x);
   __CLC_FLOATN tr = -MATH_RECIP(t);
 
-  return regn & 1 ? tr : t;
+  return (regn & 1) != 0 ? tr : t;
 }
 
 _CLC_DEF _CLC_OVERLOAD void __clc_fullMulS(private __CLC_FLOATN *hi,
diff --git a/clc/lib/generic/math/clc_tan.cl b/clc/lib/generic/math/clc_tan.cl
new file mode 100644
index 0000000..adf42c4
--- /dev/null
+++ b/clc/lib/generic/math/clc_tan.cl
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <clc/clc_convert.h>
+#include <clc/float/definitions.h>
+#include <clc/internal/clc.h>
+#include <clc/math/clc_fabs.h>
+#include <clc/math/clc_sincos_helpers.h>
+#include <clc/math/clc_sincos_piby4.h>
+#include <clc/math/math.h>
+#include <clc/math/tables.h>
+#include <clc/relational/clc_isinf.h>
+#include <clc/relational/clc_isnan.h>
+#include <clc/relational/clc_select.h>
+
+#define __CLC_BODY <clc_tan.inc>
+#include <clc/math/gentype.inc>
diff --git a/clc/lib/generic/math/clc_tan.inc b/clc/lib/generic/math/clc_tan.inc
new file mode 100644
index 0000000..8a318a5
--- /dev/null
+++ b/clc/lib/generic/math/clc_tan.inc
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#if __CLC_FPSIZE == 32
+
+_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) {
+  __CLC_GENTYPE absx = __clc_fabs(x);
+  __CLC_UINTN x_signbit = __CLC_AS_UINTN(x) & SIGNBIT_SP32;
+
+  __CLC_GENTYPE r0, r1;
+  __CLC_INTN regn = __clc_argReductionS(&r0, &r1, absx);
+
+  __CLC_GENTYPE t = __clc_tanf_piby4(r0 + r1, regn);
+  t = __CLC_AS_GENTYPE(__CLC_AS_UINTN(t) ^ x_signbit);
+
+  t = __clc_select(t, __CLC_GENTYPE_NAN, __clc_isnan(x) || __clc_isinf(x));
+  // Take care of subnormals
+  t = (x == 0.0f) ? x : t;
+  return t;
+}
+
+#elif __CLC_FPSIZE == 64
+
+_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) {
+  __CLC_GENTYPE y = __clc_fabs(x);
+
+  __CLC_BIT_INTN is_medium = y < 0x1.0p+30;
+
+  __CLC_INTN regn_m, regn_l;
+  __CLC_GENTYPE r_m, r_l, rr_m, rr_l;
+
+  __clc_remainder_piby2_medium(y, &r_m, &rr_m, &regn_m);
+  __clc_remainder_piby2_large(y, &r_l, &rr_l, &regn_l);
+
+  __CLC_GENTYPE r = is_medium ? r_m : r_l;
+  __CLC_GENTYPE rr = is_medium ? rr_m : rr_l;
+  __CLC_INTN regn = __CLC_CONVERT_INTN(is_medium) ? regn_m : regn_l;
+
+  __CLC_GENTYPE lead, tail;
+  __clc_tan_piby4(r, rr, &lead, &tail);
+
+  __CLC_LONGN t =
+      __CLC_AS_LONGN(__CLC_CONVERT_BIT_INTN((regn & 1) != 0) ? tail : lead);
+  t ^= __CLC_CONVERT_BIT_INTN(x < 0.0) << 63;
+
+  return __clc_isnan(x) || __clc_isinf(x) ? __CLC_GENTYPE_NAN
+                                          : __CLC_AS_GENTYPE(t);
+}
+
+#elif __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) {
+  return __CLC_CONVERT_GENTYPE(__clc_tan(__CLC_CONVERT_FLOATN(x)));
+}
+
+#endif
diff --git a/clspv/lib/SOURCES b/clspv/lib/SOURCES
index 437210d..d7cd36d 100644
--- a/clspv/lib/SOURCES
+++ b/clspv/lib/SOURCES
@@ -16,7 +16,6 @@
 ../../generic/lib/math/atanh.cl
 ../../generic/lib/math/atanpi.cl
 ../../generic/lib/math/cbrt.cl
-../../generic/lib/math/clc_tan.cl
 ../../generic/lib/math/cos.cl
 ../../generic/lib/math/cosh.cl
 ../../generic/lib/math/cospi.cl
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index 8c7565e..46ce6d6 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -146,7 +146,6 @@
 math/sinh.cl
 math/sinpi.cl
 math/sqrt.cl
-math/clc_tan.cl
 math/tan.cl
 math/tanh.cl
 math/tanpi.cl
diff --git a/generic/lib/math/clc_sw_unary.inc b/generic/lib/math/clc_sw_unary.inc
deleted file mode 100644
index 6fa051d..0000000
--- a/generic/lib/math/clc_sw_unary.inc
+++ /dev/null
@@ -1,30 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include <clc/utils.h>
-
-#define __CLC_SW_FUNC(x) __CLC_CONCAT(__clc_, x)
-
-#if __CLC_FPSIZE > 16
-_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) {
-  return __CLC_SW_FUNC(__CLC_FUNC)(x);
-}
-#elif __CLC_FPSIZE == 16
-#ifdef __CLC_SCALAR
-_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) {
-  return convert_half(__CLC_SW_FUNC(__CLC_FUNC)(convert_float(x)));
-}
-#else
-_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) {
-  return __CLC_XCONCAT(convert_half, __CLC_VECSIZE)(__CLC_SW_FUNC(__CLC_FUNC)(
-      __CLC_XCONCAT(convert_float, __CLC_VECSIZE)(x)));
-}
-#endif
-#endif
-
-#undef __CLC_SW_FUNC
diff --git a/generic/lib/math/clc_tan.cl b/generic/lib/math/clc_tan.cl
deleted file mode 100644
index ce51c10..0000000
--- a/generic/lib/math/clc_tan.cl
+++ /dev/null
@@ -1,61 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include <clc/clc.h>
-#include <clc/clcmacro.h>
-#include <clc/math/clc_fabs.h>
-#include <clc/math/clc_sincos_helpers.h>
-#include <clc/math/math.h>
-#include <clc/math/tables.h>
-#include <clc/relational/clc_isinf.h>
-#include <clc/relational/clc_isnan.h>
-
-_CLC_DEF _CLC_OVERLOAD float __clc_tan(float x) {
-  int ix = as_int(x);
-  int ax = ix & 0x7fffffff;
-  float dx = as_float(ax);
-
-  float r0, r1;
-  int regn = __clc_argReductionS(&r0, &r1, dx);
-
-  float t = __clc_tanf_piby4(r0 + r1, regn);
-  t = as_float(as_int(t) ^ (ix ^ ax));
-
-  t = ax >= PINFBITPATT_SP32 ? as_float(QNANBITPATT_SP32) : t;
-  // Take care of subnormals
-  t = (x == 0.0f) ? x : t;
-  return t;
-}
-_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_tan, float);
-
-#ifdef cl_khr_fp64
-#include <clc/math/clc_sincos_piby4.h>
-
-_CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) {
-  double y = __clc_fabs(x);
-
-  double r, rr;
-  int regn;
-
-  if (y < 0x1.0p+30)
-    __clc_remainder_piby2_medium(y, &r, &rr, &regn);
-  else
-    __clc_remainder_piby2_large(y, &r, &rr, &regn);
-
-  double lead, tail;
-  __clc_tan_piby4(r, rr, &lead, &tail);
-
-  int2 t = as_int2(regn & 1 ? tail : lead);
-  t.hi ^= (x < 0.0) << 31;
-
-  return __clc_isnan(x) || __clc_isinf(x) ? as_double(QNANBITPATT_DP64)
-                                          : as_double(t);
-}
-_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_tan, double);
-
-#endif
diff --git a/generic/lib/math/tan.cl b/generic/lib/math/tan.cl
index ebbaa3a..883e331 100644
--- a/generic/lib/math/tan.cl
+++ b/generic/lib/math/tan.cl
@@ -7,9 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include <clc/clc.h>
+#include <clc/math/clc_tan.h>
 
-#include <math/clc_tan.h>
-
-#define __CLC_FUNC tan
-#define __CLC_BODY <clc_sw_unary.inc>
+#define FUNCTION tan
+#define __CLC_BODY <clc/shared/unary_def.inc>
 #include <clc/math/gentype.inc>
diff --git a/spirv/lib/SOURCES b/spirv/lib/SOURCES
index 20e0522..f3852eb 100644
--- a/spirv/lib/SOURCES
+++ b/spirv/lib/SOURCES
@@ -65,7 +65,6 @@
 ../../generic/lib/math/sincos.cl
 ../../generic/lib/math/sinh.cl
 ../../generic/lib/math/sinpi.cl
-../../generic/lib/math/clc_tan.cl
 ../../generic/lib/math/tan.cl
 ../../generic/lib/math/tanh.cl
 ../../generic/lib/math/tanpi.cl