Skip to content

Commit 655151a

Browse files
authored
[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.
1 parent 49ee674 commit 655151a

35 files changed

+365
-284
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
10+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
11+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
12+
13+
_CLC_OVERLOAD _CLC_DECL __CLC_SCALAR_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE a,
14+
__CLC_GENTYPE b);
15+
16+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/utils.h>
10+
11+
#ifndef __CLC_FUNCTION
12+
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
13+
#endif
14+
15+
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
16+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
17+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
18+
19+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE FUNCTION(__CLC_GENTYPE a,
20+
__CLC_GENTYPE b) {
21+
return __CLC_FUNCTION(FUNCTION)(a, b);
22+
}
23+
24+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_GEOMETRIC_CLC_DISTANCE_H__
10+
#define __CLC_GEOMETRIC_CLC_DISTANCE_H__
11+
12+
#define __CLC_FUNCTION __clc_distance
13+
#define __CLC_BODY <clc/geometric/binary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_FUNCTION
18+
19+
#endif // __CLC_GEOMETRIC_CLC_DISTANCE_H__

libclc/clc/include/clc/geometric/clc_dot.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#ifndef __CLC_GEOMETRIC_CLC_DOT_H__
1010
#define __CLC_GEOMETRIC_CLC_DOT_H__
1111

12-
#define __CLC_BODY <clc/geometric/clc_dot.inc>
13-
#include <clc/geometric/floatn.inc>
12+
#define __CLC_FUNCTION __clc_dot
13+
#define __CLC_BODY <clc/geometric/binary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_FUNCTION
1418

1519
#endif // __CLC_GEOMETRIC_CLC_DOT_H__
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
10+
#define __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
11+
12+
#define __FLOAT_ONLY
13+
#define __CLC_FUNCTION __clc_fast_distance
14+
#define __CLC_BODY <clc/geometric/binary_decl.inc>
15+
16+
#include <clc/math/gentype.inc>
17+
18+
#undef __FLOAT_ONLY
19+
#undef __CLC_FUNCTION
20+
21+
#endif // __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
10+
#define __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
11+
12+
#define __FLOAT_ONLY
13+
#define __CLC_FUNCTION __clc_fast_length
14+
#define __CLC_BODY <clc/geometric/unary_decl.inc>
15+
16+
#include <clc/math/gentype.inc>
17+
18+
#undef __FLOAT_ONLY
19+
#undef __CLC_FUNCTION
20+
21+
#endif // __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__

libclc/generic/include/clc/geometric/dot.inc renamed to libclc/clc/include/clc/geometric/clc_length.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT dot(__CLC_FLOATN p0, __CLC_FLOATN p1);
9+
#ifndef __CLC_GEOMETRIC_CLC_LENGTH_H__
10+
#define __CLC_GEOMETRIC_CLC_LENGTH_H__
11+
12+
#define __CLC_FUNCTION __clc_length
13+
#define __CLC_BODY <clc/geometric/unary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_FUNCTION
18+
19+
#endif // __CLC_GEOMETRIC_CLC_LENGTH_H__

libclc/clc/include/clc/geometric/floatn.inc

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
10+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
11+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
12+
13+
_CLC_OVERLOAD _CLC_DECL
14+
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
15+
__CLC_GENTYPE
16+
#else
17+
__CLC_SCALAR_GENTYPE
18+
#endif
19+
__CLC_FUNCTION(__CLC_GENTYPE a);
20+
21+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/utils.h>
10+
11+
#ifndef __CLC_FUNCTION
12+
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
13+
#endif
14+
15+
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
16+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
17+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
18+
19+
_CLC_OVERLOAD _CLC_DEF
20+
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
21+
__CLC_GENTYPE
22+
#else
23+
__CLC_SCALAR_GENTYPE
24+
#endif
25+
FUNCTION(__CLC_GENTYPE a) {
26+
return __CLC_FUNCTION(FUNCTION)(a);
27+
}
28+
29+
#endif

libclc/clc/lib/generic/SOURCES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ common/clc_degrees.cl
22
common/clc_radians.cl
33
common/clc_sign.cl
44
common/clc_smoothstep.cl
5+
geometric/clc_distance.cl
56
geometric/clc_dot.cl
7+
geometric/clc_fast_distance.cl
8+
geometric/clc_fast_length.cl
9+
geometric/clc_length.cl
610
integer/clc_abs.cl
711
integer/clc_abs_diff.cl
812
integer/clc_add_sat.cl

libclc/clc/include/clc/geometric/clc_dot.inc renamed to libclc/clc/lib/generic/geometric/clc_distance.cl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT __clc_dot(__CLC_FLOATN p0, __CLC_FLOATN p1);
9+
#include <clc/geometric/clc_length.h>
10+
#include <clc/internal/clc.h>
11+
12+
#define __CLC_BODY <clc_distance.inc>
13+
#include <clc/math/gentype.inc>

libclc/generic/lib/geometric/fast_distance.inc renamed to libclc/clc/lib/generic/geometric/clc_distance.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1) {
10-
return fast_length(p0 - p1);
9+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
10+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_distance(__CLC_GENTYPE p0,
13+
__CLC_GENTYPE p1) {
14+
return __clc_length(p0 - p1);
1115
}
16+
17+
#endif

libclc/generic/include/clc/geometric/fast_distance.inc renamed to libclc/clc/lib/generic/geometric/clc_fast_distance.cl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1);
9+
#include <clc/geometric/clc_fast_length.h>
10+
#include <clc/internal/clc.h>
11+
12+
#define __FLOAT_ONLY
13+
#define __CLC_BODY <clc_fast_distance.inc>
14+
#include <clc/math/gentype.inc>

libclc/generic/lib/geometric/distance.inc renamed to libclc/clc/lib/generic/geometric/clc_fast_distance.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1) {
10-
return length(p0 - p1);
9+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
10+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
13+
__clc_fast_distance(__CLC_GENTYPE p0, __CLC_GENTYPE p1) {
14+
return __clc_fast_length(p0 - p1);
1115
}
16+
17+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/geometric/clc_dot.h>
10+
#include <clc/internal/clc.h>
11+
#include <clc/math/clc_fabs.h>
12+
#include <clc/math/clc_half_sqrt.h>
13+
14+
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float p) {
15+
return __clc_fabs(p);
16+
}
17+
18+
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float2 p) {
19+
return __clc_half_sqrt(__clc_dot(p, p));
20+
}
21+
22+
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float3 p) {
23+
return __clc_half_sqrt(__clc_dot(p, p));
24+
}
25+
26+
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float4 p) {
27+
return __clc_half_sqrt(__clc_dot(p, p));
28+
}

libclc/generic/include/clc/geometric/distance.inc renamed to libclc/clc/lib/generic/geometric/clc_length.cl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1);
9+
#include <clc/float/definitions.h>
10+
#include <clc/geometric/clc_dot.h>
11+
#include <clc/internal/clc.h>
12+
#include <clc/math/clc_fabs.h>
13+
#include <clc/math/clc_sqrt.h>
14+
15+
#define __CLC_BODY <clc_length.inc>
16+
#include <clc/math/gentype.inc>

0 commit comments

Comments
 (0)