[libclc] Move (fast) length & distance to CLC library (#139701)

This commit also refactors how geometric builtins are defined and
declared, by sharing more helpers. It also removes an unnecessary
gentype-like helper in favour of the more complete math/gentype.inc.

There are no changes to the IR for any of these four builtins.

The 'normalize' builtin will follow in a subsequent commit because it
would involve the addition of missing halfn-type overloads for
completeness.
This commit is contained in:
Fraser Cormack
2025-05-13 11:45:55 +01:00
committed by GitHub
parent 49ee674e5d
commit 655151a7e0
35 changed files with 365 additions and 284 deletions

View File

@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DECL __CLC_SCALAR_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE a,
__CLC_GENTYPE b);
#endif

View File

@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// 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>
#ifndef __CLC_FUNCTION
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE FUNCTION(__CLC_GENTYPE a,
__CLC_GENTYPE b) {
return __CLC_FUNCTION(FUNCTION)(a, b);
}
#endif

View File

@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_DISTANCE_H__
#define __CLC_GEOMETRIC_CLC_DISTANCE_H__
#define __CLC_FUNCTION __clc_distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_DISTANCE_H__

View File

@@ -9,7 +9,11 @@
#ifndef __CLC_GEOMETRIC_CLC_DOT_H__
#define __CLC_GEOMETRIC_CLC_DOT_H__
#define __CLC_BODY <clc/geometric/clc_dot.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION __clc_dot
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_DOT_H__

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
#define __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
#define __FLOAT_ONLY
#define __CLC_FUNCTION __clc_fast_distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
#define __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
#define __FLOAT_ONLY
#define __CLC_FUNCTION __clc_fast_length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__

View File

@@ -6,4 +6,14 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT dot(__CLC_FLOATN p0, __CLC_FLOATN p1);
#ifndef __CLC_GEOMETRIC_CLC_LENGTH_H__
#define __CLC_GEOMETRIC_CLC_LENGTH_H__
#define __CLC_FUNCTION __clc_length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_LENGTH_H__

View File

@@ -1,98 +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/clcfunc.h>
#include <clc/clctypes.h>
#define __CLC_FLOAT float
#define __CLC_FPSIZE 32
#define __CLC_FLOATN float
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN float2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN float3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN float4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#ifndef __FLOAT_ONLY
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#define __CLC_FLOAT double
#define __CLC_FPSIZE 64
#define __CLC_FLOATN double
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN double2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN double3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN double4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#endif
#endif
#ifndef __FLOAT_ONLY
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
#define __CLC_FLOAT half
#define __CLC_FPSIZE 16
#define __CLC_FLOATN half
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN half2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN half3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN half4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#endif
#endif
#undef __CLC_BODY

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DECL
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
__CLC_GENTYPE
#else
__CLC_SCALAR_GENTYPE
#endif
__CLC_FUNCTION(__CLC_GENTYPE a);
#endif

View File

@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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>
#ifndef __CLC_FUNCTION
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
__CLC_GENTYPE
#else
__CLC_SCALAR_GENTYPE
#endif
FUNCTION(__CLC_GENTYPE a) {
return __CLC_FUNCTION(FUNCTION)(a);
}
#endif

View File

@@ -2,7 +2,11 @@ common/clc_degrees.cl
common/clc_radians.cl
common/clc_sign.cl
common/clc_smoothstep.cl
geometric/clc_distance.cl
geometric/clc_dot.cl
geometric/clc_fast_distance.cl
geometric/clc_fast_length.cl
geometric/clc_length.cl
integer/clc_abs.cl
integer/clc_abs_diff.cl
integer/clc_add_sat.cl

View File

@@ -6,4 +6,8 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT __clc_dot(__CLC_FLOATN p0, __CLC_FLOATN p1);
#include <clc/geometric/clc_length.h>
#include <clc/internal/clc.h>
#define __CLC_BODY <clc_distance.inc>
#include <clc/math/gentype.inc>

View File

@@ -6,6 +6,12 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1) {
return fast_length(p0 - p1);
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_distance(__CLC_GENTYPE p0,
__CLC_GENTYPE p1) {
return __clc_length(p0 - p1);
}
#endif

View File

@@ -6,4 +6,9 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1);
#include <clc/geometric/clc_fast_length.h>
#include <clc/internal/clc.h>
#define __FLOAT_ONLY
#define __CLC_BODY <clc_fast_distance.inc>
#include <clc/math/gentype.inc>

View File

@@ -6,6 +6,12 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1) {
return length(p0 - p1);
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
__clc_fast_distance(__CLC_GENTYPE p0, __CLC_GENTYPE p1) {
return __clc_fast_length(p0 - p1);
}
#endif

View File

@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// 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/geometric/clc_dot.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_half_sqrt.h>
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float p) {
return __clc_fabs(p);
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float2 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float3 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float4 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}

View File

@@ -6,4 +6,11 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1);
#include <clc/float/definitions.h>
#include <clc/geometric/clc_dot.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_sqrt.h>
#define __CLC_BODY <clc_length.inc>
#include <clc/math/gentype.inc>

View File

@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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_VECSIZE_OR_1 == 1
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_length(__CLC_GENTYPE p) {
return __clc_fabs(p);
}
#elif (__CLC_VECSIZE_OR_1 == 2 || __CLC_VECSIZE_OR_1 == 3 || \
__CLC_VECSIZE_OR_1 == 4)
#if __CLC_FPSIZE == 32
#define __CLC_GENTYPE_MIN FLT_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+86F
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-86F
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+65F
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-65F
#elif __CLC_FPSIZE == 64
#define __CLC_GENTYPE_MIN DBL_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+563
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-563
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+513
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-513
#elif __CLC_FPSIZE == 16
#define __CLC_GENTYPE_MIN HALF_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+12H
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-12H
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+7H
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-7H
#else
#error "Unsupported FP size"
#endif
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_length(__CLC_GENTYPE p) {
__CLC_SCALAR_GENTYPE l2 = __clc_dot(p, p);
if (l2 < __CLC_GENTYPE_MIN) {
p *= __CLC_GENTYPE_MIN_SCALE_UP;
return __clc_sqrt(__clc_dot(p, p)) * __CLC_GENTYPE_MIN_SCALE_DOWN;
} else if (l2 == INFINITY) {
p *= __CLC_GENTYPE_INF_SCALE_DOWN;
return __clc_sqrt(__clc_dot(p, p)) * __CLC_GENTYPE_INF_SCALE_UP;
}
return __clc_sqrt(l2);
}
#undef __CLC_GENTYPE_INF_SCALE_DOWN
#undef __CLC_GENTYPE_INF_SCALE_UP
#undef __CLC_GENTYPE_MIN_SCALE_DOWN
#undef __CLC_GENTYPE_MIN_SCALE_UP
#undef __CLC_GENTYPE_MIN
#endif

View File

@@ -6,5 +6,9 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/distance.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION

View File

@@ -6,5 +6,9 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/dot.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION dot
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION

View File

@@ -6,7 +6,11 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/fast_distance.inc>
#define __FLOAT_ONLY
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION fast_distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION

View File

@@ -6,7 +6,11 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/fast_length.inc>
#define __FLOAT_ONLY
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION fast_length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION

View File

@@ -1,9 +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
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT fast_length(__CLC_FLOATN p0);

View File

@@ -6,7 +6,13 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/fast_normalize.inc>
#define __FLOAT_ONLY
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION fast_normalize
#define __CLC_GEOMETRIC_RET_GENTYPE
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#undef __CLC_GEOMETRIC_RET_GENTYPE
#undef __FLOAT_ONLY

View File

@@ -1,9 +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
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN fast_normalize(__CLC_FLOATN p);

View File

@@ -6,5 +6,9 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/length.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION

View File

@@ -1,9 +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
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT length(__CLC_FLOATN p0);

View File

@@ -6,5 +6,11 @@
//
//===----------------------------------------------------------------------===//
#define __CLC_BODY <clc/geometric/normalize.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION normalize
#define __CLC_GEOMETRIC_RET_GENTYPE
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_GEOMETRIC_RET_GENTYPE
#undef __CLC_FUNCTION

View File

@@ -1,9 +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
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN normalize(__CLC_FLOATN p);

View File

@@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/geometric/clc_distance.h>
#define __CLC_BODY <distance.inc>
#include <clc/geometric/floatn.inc>
#define FUNCTION distance
#define __CLC_BODY <clc/geometric/binary_def.inc>
#include <clc/math/gentype.inc>

View File

@@ -7,8 +7,9 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/geometric/clc_fast_distance.h>
#define __CLC_BODY <fast_distance.inc>
#define __FLOAT_ONLY
#include <clc/geometric/floatn.inc>
#undef __FLOAT_ONLY
#define FUNCTION fast_distance
#define __CLC_BODY <clc/geometric/binary_def.inc>
#include <clc/math/gentype.inc>

View File

@@ -7,19 +7,9 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/geometric/clc_fast_length.h>
_CLC_OVERLOAD _CLC_DEF float fast_length(float p) {
return fabs(p);
}
_CLC_OVERLOAD _CLC_DEF float fast_length(float2 p) {
return half_sqrt(dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float fast_length(float3 p) {
return half_sqrt(dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float fast_length(float4 p) {
return half_sqrt(dot(p, p));
}
#define __FLOAT_ONLY
#define FUNCTION fast_length
#define __CLC_BODY <clc/geometric/unary_def.inc>
#include <clc/math/gentype.inc>

View File

@@ -14,5 +14,5 @@ _CLC_OVERLOAD _CLC_DEF float fast_normalize(float p) {
#define __CLC_BODY <fast_normalize.inc>
#define __FLOAT_ONLY
#include <clc/geometric/floatn.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY

View File

@@ -6,11 +6,13 @@
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_SCALAR
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
// Only handle vector implementations
_CLC_OVERLOAD _CLC_DEF __CLC_FLOATN fast_normalize(__CLC_FLOATN p) {
__CLC_FLOAT l2 = dot(p, p);
#if (__CLC_VECSIZE_OR_1 == 2 || __CLC_VECSIZE_OR_1 == 3 || \
__CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fast_normalize(__CLC_GENTYPE p) {
__CLC_SCALAR_GENTYPE l2 = dot(p, p);
return l2 == 0.0f ? p : p * half_rsqrt(l2);
}

View File

@@ -7,105 +7,8 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/geometric/clc_length.h>
_CLC_OVERLOAD _CLC_DEF float length(float p) {
return fabs(p);
}
#define V_FLENGTH(p) \
float l2 = dot(p, p); \
\
if (l2 < FLT_MIN) { \
p *= 0x1.0p+86F; \
return sqrt(dot(p, p)) * 0x1.0p-86F; \
} else if (l2 == INFINITY) { \
p *= 0x1.0p-65F; \
return sqrt(dot(p, p)) * 0x1.0p+65F; \
} \
\
return sqrt(l2);
_CLC_OVERLOAD _CLC_DEF float length(float2 p) {
V_FLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF float length(float3 p) {
V_FLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF float length(float4 p) {
V_FLENGTH(p);
}
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_OVERLOAD _CLC_DEF double length(double p){
return fabs(p);
}
#define V_DLENGTH(p) \
double l2 = dot(p, p); \
\
if (l2 < DBL_MIN) { \
p *= 0x1.0p+563; \
return sqrt(dot(p, p)) * 0x1.0p-563; \
} else if (l2 == INFINITY) { \
p *= 0x1.0p-513; \
return sqrt(dot(p, p)) * 0x1.0p+513; \
} \
\
return sqrt(l2);
_CLC_OVERLOAD _CLC_DEF double length(double2 p) {
V_DLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF double length(double3 p) {
V_DLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF double length(double4 p) {
V_DLENGTH(p);
}
#endif
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
_CLC_OVERLOAD _CLC_DEF half length(half p){
return fabs(p);
}
// Only available in CLC1.2
#ifndef HALF_MIN
#define HALF_MIN 0x1.0p-14h
#endif
#define V_HLENGTH(p) \
half l2 = dot(p, p); \
\
if (l2 < HALF_MIN) { \
p *= 0x1.0p+12h; \
return sqrt(dot(p, p)) * 0x1.0p-12h; \
} else if (l2 == INFINITY) { \
p *= 0x1.0p-7h; \
return sqrt(dot(p, p)) * 0x1.0p+7h; \
} \
\
return sqrt(l2);
_CLC_OVERLOAD _CLC_DEF half length(half2 p) {
V_HLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF half length(half3 p) {
V_HLENGTH(p);
}
_CLC_OVERLOAD _CLC_DEF half length(half4 p) {
V_HLENGTH(p);
}
#endif
#define FUNCTION length
#define __CLC_BODY <clc/geometric/unary_def.inc>
#include <clc/math/gentype.inc>