Skip to content

Commit

Permalink
[libclc][NFC] Move key math headers to CLC (llvm#124739)
Browse files Browse the repository at this point in the history
  • Loading branch information
frasercrmck authored Jan 28, 2025
1 parent 3a975d6 commit 78b5bb7
Show file tree
Hide file tree
Showing 66 changed files with 178 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __CLC_MATH_CLC_SUBNORMAL_CONFIG_H__
#define __CLC_MATH_CLC_SUBNORMAL_CONFIG_H__

#include <clc/clcfunc.h>

_CLC_DECL bool __clc_subnormals_disabled();
_CLC_DECL bool __clc_fp16_subnormals_supported();
_CLC_DECL bool __clc_fp32_subnormals_supported();
_CLC_DECL bool __clc_fp64_subnormals_supported();

#endif // __CLC_MATH_CLC_SUBNORMAL_CONFIG_H__
108 changes: 54 additions & 54 deletions libclc/generic/lib/math/math.h → libclc/clc/include/clc/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
* THE SOFTWARE.
*/

#ifndef __CLC_MATH_H_
#define __CLC_MATH_H_
#ifndef __CLC_MATH_MATH_H__
#define __CLC_MATH_MATH_H__

#include "clc/clcfunc.h"
#include "clc/as_type.h"
#include "config.h"
#include <clc/clc_as_type.h>
#include <clc/clcfunc.h>
#include <clc/math/clc_subnormal_config.h>

#define SNAN 0x001
#define QNAN 0x002
Expand Down Expand Up @@ -54,64 +54,64 @@ bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
#define MATH_RECIP(X) (1.0f / (X))
#define MATH_SQRT(X) sqrt(X)

#define SIGNBIT_SP32 0x80000000
#define EXSIGNBIT_SP32 0x7fffffff
#define EXPBITS_SP32 0x7f800000
#define MANTBITS_SP32 0x007fffff
#define ONEEXPBITS_SP32 0x3f800000
#define TWOEXPBITS_SP32 0x40000000
#define HALFEXPBITS_SP32 0x3f000000
#define IMPBIT_SP32 0x00800000
#define QNANBITPATT_SP32 0x7fc00000
#define SIGNBIT_SP32 0x80000000
#define EXSIGNBIT_SP32 0x7fffffff
#define EXPBITS_SP32 0x7f800000
#define MANTBITS_SP32 0x007fffff
#define ONEEXPBITS_SP32 0x3f800000
#define TWOEXPBITS_SP32 0x40000000
#define HALFEXPBITS_SP32 0x3f000000
#define IMPBIT_SP32 0x00800000
#define QNANBITPATT_SP32 0x7fc00000
#define INDEFBITPATT_SP32 0xffc00000
#define PINFBITPATT_SP32 0x7f800000
#define NINFBITPATT_SP32 0xff800000
#define EXPBIAS_SP32 127
#define PINFBITPATT_SP32 0x7f800000
#define NINFBITPATT_SP32 0xff800000
#define EXPBIAS_SP32 127
#define EXPSHIFTBITS_SP32 23
#define BIASEDEMIN_SP32 1
#define EMIN_SP32 -126
#define BIASEDEMAX_SP32 254
#define EMAX_SP32 127
#define LAMBDA_SP32 1.0e30
#define MANTLENGTH_SP32 24
#define BASEDIGITS_SP32 7
#define BIASEDEMIN_SP32 1
#define EMIN_SP32 -126
#define BIASEDEMAX_SP32 254
#define EMAX_SP32 127
#define LAMBDA_SP32 1.0e30
#define MANTLENGTH_SP32 24
#define BASEDIGITS_SP32 7

_CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x)
{
int ix = as_int(x);
if (!__clc_fp32_subnormals_supported() &&
((ix & EXPBITS_SP32) == 0) && ((ix & MANTBITS_SP32) != 0)) {
ix &= SIGNBIT_SP32;
x = as_float(ix);
}
return x;
_CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
int ix = __clc_as_int(x);
if (!__clc_fp32_subnormals_supported() && ((ix & EXPBITS_SP32) == 0) &&
((ix & MANTBITS_SP32) != 0)) {
ix &= SIGNBIT_SP32;
x = __clc_as_float(ix);
}
return x;
}

#ifdef cl_khr_fp64

#define SIGNBIT_DP64 0x8000000000000000L
#define EXSIGNBIT_DP64 0x7fffffffffffffffL
#define EXPBITS_DP64 0x7ff0000000000000L
#define MANTBITS_DP64 0x000fffffffffffffL
#define ONEEXPBITS_DP64 0x3ff0000000000000L
#define TWOEXPBITS_DP64 0x4000000000000000L
#define HALFEXPBITS_DP64 0x3fe0000000000000L
#define IMPBIT_DP64 0x0010000000000000L
#define QNANBITPATT_DP64 0x7ff8000000000000L
#define SIGNBIT_DP64 0x8000000000000000L
#define EXSIGNBIT_DP64 0x7fffffffffffffffL
#define EXPBITS_DP64 0x7ff0000000000000L
#define MANTBITS_DP64 0x000fffffffffffffL
#define ONEEXPBITS_DP64 0x3ff0000000000000L
#define TWOEXPBITS_DP64 0x4000000000000000L
#define HALFEXPBITS_DP64 0x3fe0000000000000L
#define IMPBIT_DP64 0x0010000000000000L
#define QNANBITPATT_DP64 0x7ff8000000000000L
#define INDEFBITPATT_DP64 0xfff8000000000000L
#define PINFBITPATT_DP64 0x7ff0000000000000L
#define NINFBITPATT_DP64 0xfff0000000000000L
#define EXPBIAS_DP64 1023
#define PINFBITPATT_DP64 0x7ff0000000000000L
#define NINFBITPATT_DP64 0xfff0000000000000L
#define EXPBIAS_DP64 1023
#define EXPSHIFTBITS_DP64 52
#define BIASEDEMIN_DP64 1
#define EMIN_DP64 -1022
#define BIASEDEMAX_DP64 2046 /* 0x7fe */
#define EMAX_DP64 1023 /* 0x3ff */
#define LAMBDA_DP64 1.0e300
#define MANTLENGTH_DP64 53
#define BASEDIGITS_DP64 15
#define BIASEDEMIN_DP64 1
#define EMIN_DP64 -1022
#define BIASEDEMAX_DP64 2046 /* 0x7fe */
#define EMAX_DP64 1023 /* 0x3ff */
#define LAMBDA_DP64 1.0e300
#define MANTLENGTH_DP64 53
#define BASEDIGITS_DP64 15

#endif // cl_khr_fp64

#define ALIGNED(x) __attribute__((aligned(x)))
#endif // __CLC_MATH_H_
#define ALIGNED(x) __attribute__((aligned(x)))

#endif // __CLC_MATH_MATH_H__
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,30 @@
* THE SOFTWARE.
*/

#ifndef __CLC_MATH_TABLES_H__
#define __CLC_MATH_TABLES_H__

#include <clc/clctypes.h>

#define TABLE_SPACE __constant

#define TABLE_MANGLE(NAME) __clc_##NAME

#define DECLARE_TABLE(TYPE,NAME,LENGTH) \
TABLE_SPACE TYPE NAME [ LENGTH ]
#define DECLARE_TABLE(TYPE, NAME, LENGTH) TABLE_SPACE TYPE NAME[LENGTH]

#define TABLE_FUNCTION(TYPE,TABLE,NAME) \
TYPE TABLE_MANGLE(NAME)(size_t idx) { \
return TABLE[idx]; \
}
#define TABLE_FUNCTION(TYPE, TABLE, NAME) \
TYPE TABLE_MANGLE(NAME)(size_t idx) { return TABLE[idx]; }

#define TABLE_FUNCTION_DECL(TYPE, NAME) \
TYPE TABLE_MANGLE(NAME)(size_t idx);
#define TABLE_FUNCTION_DECL(TYPE, NAME) TYPE TABLE_MANGLE(NAME)(size_t idx);

#define USE_TABLE(NAME, IDX) \
TABLE_MANGLE(NAME)(IDX)
#define USE_TABLE(NAME, IDX) TABLE_MANGLE(NAME)(IDX)

TABLE_FUNCTION_DECL(float2, loge_tbl);
TABLE_FUNCTION_DECL(float, log_inv_tbl);
TABLE_FUNCTION_DECL(float2, log_inv_tbl_ep);
TABLE_FUNCTION_DECL(float2, log2_tbl);
TABLE_FUNCTION_DECL(float2, log10_tbl);
TABLE_FUNCTION_DECL(uint4, pibits_tbl);
TABLE_FUNCTION_DECL(uint4, pibits_tbl);
TABLE_FUNCTION_DECL(float2, sinhcosh_tbl);
TABLE_FUNCTION_DECL(float2, cbrt_tbl);
TABLE_FUNCTION_DECL(float, exp_tbl);
Expand All @@ -67,3 +65,5 @@ TABLE_FUNCTION_DECL(double2, powlog_tbl);
TABLE_FUNCTION_DECL(double2, log_f_inv_tbl);

#endif // cl_khr_fp64

#endif // __CLC_MATH_TABLES_H__
2 changes: 1 addition & 1 deletion libclc/clspv/lib/math/fma.cl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
// (__clc_sw_fma), but avoids the use of ulong in favor of uint2. The logic has
// been updated as appropriate.

#include "../../../generic/lib/math/math.h"
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/math.h>

struct fp {
uint2 mantissa;
Expand Down
3 changes: 1 addition & 2 deletions libclc/clspv/lib/subnormal_config.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
*/

#include <clc/clc.h>

#include "config.h"
#include <clc/math/clc_subnormal_config.h>

_CLC_DEF bool __clc_fp16_subnormals_supported() { return false; }

Expand Down
2 changes: 2 additions & 0 deletions libclc/generic/include/math/clc_ldexp.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <clc/clcfunc.h>

_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float, int);

#ifdef cl_khr_fp64
Expand Down
4 changes: 2 additions & 2 deletions libclc/generic/lib/math/acos.cl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float acos(float x) {
// Computes arccos(x).
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/acosh.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
* THE SOFTWARE.
*/

#include "ep_log.h"
#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "ep_log.h"
#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float acosh(float x) {
uint ux = as_uint(x);
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/acospi.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float acospi(float x) {
// Computes arccos(x).
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/asin.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float asin(float x) {
// Computes arcsin(x).
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/asinh.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
* THE SOFTWARE.
*/

#include "ep_log.h"
#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include "ep_log.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float asinh(float x) {
uint ux = as_uint(x);
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/asinpi.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float asinpi(float x) {
// Computes arcsin(x).
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/atan.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float atan(float x)
{
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/atan2.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include "tables.h"
#include <clc/math/math.h>
#include <clc/math/tables.h>

_CLC_OVERLOAD _CLC_DEF float atan2(float y, float x)
{
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/atan2pi.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include "tables.h"
#include <clc/math/math.h>
#include <clc/math/tables.h>

_CLC_OVERLOAD _CLC_DEF float atan2pi(float y, float x) {
const float pi = 0x1.921fb6p+1f;
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/atanh.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float atanh(float x) {
uint ux = as_uint(x);
Expand Down
3 changes: 1 addition & 2 deletions libclc/generic/lib/math/atanpi.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include <clc/math/math.h>

_CLC_OVERLOAD _CLC_DEF float atanpi(float x) {
const float pi = 3.1415926535897932f;
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/cbrt.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

#include <clc/clc.h>
#include <clc/clcmacro.h>

#include "math.h"
#include "tables.h"
#include <clc/math/math.h>
#include <clc/math/tables.h>

_CLC_OVERLOAD _CLC_DEF float cbrt(float x) {

Expand Down
7 changes: 3 additions & 4 deletions libclc/generic/lib/math/clc_exp10.cl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_isnan.h>

#include "config.h"
#include "math.h"
#include "tables.h"

// Algorithm:
//
// e^x = 2^(x/ln(2)) = 2^(x*(64/ln(2))/64)
Expand Down
5 changes: 2 additions & 3 deletions libclc/generic/lib/math/clc_fma.cl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/integer/clc_abs.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/relational/clc_isinf.h>
#include <clc/relational/clc_isnan.h>
#include <clc/shared/clc_max.h>

#include "config.h"
#include "math.h"

struct fp {
ulong mantissa;
int exponent;
Expand Down
Loading

0 comments on commit 78b5bb7

Please sign in to comment.