Skip to content

Commit 60ff9c2

Browse files
authored
[libc] Add support for powi as an LLVM libc extension on the GPU (#98236)
Summary: This function is used by the CUDA / HIP / OpenMP headers and exists as an NVIDIA extension basically. This function is implemented in the C23 standard as `pown`, but for now we need to provide `powi` for backwards compatibility. In the future this entrypoint will just be a redirect to `pown` once that is implemented.
1 parent c2fb400 commit 60ff9c2

14 files changed

+173
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ set(TARGET_LIBM_ENTRYPOINTS
313313
libc.src.math.nexttowardf
314314
libc.src.math.pow
315315
libc.src.math.powf
316+
libc.src.math.powi
317+
libc.src.math.powif
316318
libc.src.math.remainder
317319
libc.src.math.remainderf
318320
libc.src.math.remquo

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ Higher Math Functions
320320
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
321321
| pow | |check| | | | | | 7.12.7.5 | F.10.4.5 |
322322
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
323+
| powi\* | | | | | | | |
324+
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
323325
| pown | | | | | | 7.12.7.6 | F.10.4.6 |
324326
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
325327
| powr | | | | | | 7.12.7.7 | F.10.4.7 |

libc/spec/llvm_libc_ext.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
7676
GuardedFunctionSpec<"f16sqrt", RetValSpec<Float16Type>, [ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
7777
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
7878
GuardedFunctionSpec<"f16sqrtl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
79+
80+
FunctionSpec<"powi", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
81+
FunctionSpec<"powif", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
7982
]
8083
>;
8184

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ add_math_entrypoint_object(nextupf128)
342342

343343
add_math_entrypoint_object(pow)
344344
add_math_entrypoint_object(powf)
345+
add_math_entrypoint_object(powi)
346+
add_math_entrypoint_object(powif)
345347

346348
add_math_entrypoint_object(remainder)
347349
add_math_entrypoint_object(remainderf)

libc/src/math/amdgpu/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,30 @@ add_entrypoint_object(
468468
VENDOR
469469
)
470470

471+
add_entrypoint_object(
472+
powi
473+
SRCS
474+
powi.cpp
475+
HDRS
476+
../powi.h
477+
COMPILE_OPTIONS
478+
${bitcode_link_flags}
479+
-O2
480+
VENDOR
481+
)
482+
483+
add_entrypoint_object(
484+
powif
485+
SRCS
486+
powif.cpp
487+
HDRS
488+
../powif.h
489+
COMPILE_OPTIONS
490+
${bitcode_link_flags}
491+
-O2
492+
VENDOR
493+
)
494+
471495
add_entrypoint_object(
472496
sinh
473497
SRCS

libc/src/math/amdgpu/declarations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ float __ocml_nextafter_f32(float, float);
6565
double __ocml_nextafter_f64(double, double);
6666
float __ocml_pow_f32(float, float);
6767
double __ocml_pow_f64(double, double);
68+
float __ocml_pown_f32(float, int);
69+
double __ocml_pown_f64(double, int);
6870
float __ocml_sin_f32(float);
6971
double __ocml_sin_f64(double);
7072
float __ocml_sincos_f32(float, float *);

libc/src/math/amdgpu/powi.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of the powi function for GPU -----------------------===//
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 "src/math/powi.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(double, powi, (double x, int y)) {
17+
return __ocml_pown_f64(x, y);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/math/amdgpu/powif.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of the powi function for GPU -----------------------===//
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 "src/math/powif.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(float, powif, (float x, int y)) {
17+
return __ocml_pown_f32(x, y);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/math/nvptx/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,30 @@ add_entrypoint_object(
421421
VENDOR
422422
)
423423

424+
add_entrypoint_object(
425+
powi
426+
SRCS
427+
powi.cpp
428+
HDRS
429+
../powi.h
430+
COMPILE_OPTIONS
431+
${bitcode_link_flags}
432+
-O2
433+
VENDOR
434+
)
435+
436+
add_entrypoint_object(
437+
powif
438+
SRCS
439+
powif.cpp
440+
HDRS
441+
../powif.h
442+
COMPILE_OPTIONS
443+
${bitcode_link_flags}
444+
-O2
445+
VENDOR
446+
)
447+
424448
add_entrypoint_object(
425449
sinh
426450
SRCS

libc/src/math/nvptx/declarations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ double __nv_nextafter(double, double);
6464
float __nv_nextafterf(float, float);
6565
double __nv_pow(double, double);
6666
float __nv_powf(float, float);
67+
double __nv_powi(double, int);
68+
float __nv_powif(float, int);
6769
double __nv_sin(double);
6870
float __nv_sinf(float);
6971
void __nv_sincos(double, double *, double *);

libc/src/math/nvptx/powi.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of the powi function for GPU -----------------------===//
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 "src/math/powi.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(double, powi, (double x, int y)) { return __nv_powi(x, y); }
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/math/nvptx/powif.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of the powif function for GPU ----------------------===//
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 "src/math/powif.h"
10+
#include "src/__support/common.h"
11+
12+
#include "declarations.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(float, powif, (float x, int y)) { return __nv_powif(x, y); }
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/math/powi.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for powi --------------------------*- C++ -*-===//
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 LLVM_LIBC_SRC_MATH_POWI_H
10+
#define LLVM_LIBC_SRC_MATH_POWI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
double powi(double x, int y);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_POW_H

libc/src/math/powif.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for powif -------------------------*- C++ -*-===//
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 LLVM_LIBC_SRC_MATH_POWIF_H
10+
#define LLVM_LIBC_SRC_MATH_POWIF_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
float powif(float x, int y);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_POW_H

0 commit comments

Comments
 (0)