Skip to content

[libc] Add lgamma and lgamma_r stubs for the GPU #102019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.tanhf
libc.src.math.tgamma
libc.src.math.tgammaf
libc.src.math.lgamma
libc.src.math.lgamma_r
libc.src.math.trunc
libc.src.math.truncf
)
Expand Down
39 changes: 39 additions & 0 deletions libc/newhdrgen/yaml/math.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2153,3 +2153,42 @@ functions:
- type: int
- type: unsigned int
guard: LIBC_TYPES_HAS_FLOAT128
- name: lgamma
standards:
- stdc
return_type: double
arguments:
- type: double
- name: lgammaf
standards:
- stdc
return_type: float
arguments:
- type: float
- name: lgammal
standards:
- stdc
return_type: long double
arguments:
- type: long double
- name: lgamma_r
standards:
- gnu
return_type: double
arguments:
- type: double
- type: int *
- name: lgammaf_r
standards:
- gnu
return_type: float
arguments:
- type: float
- type: int *
- name: lgammal_r
standards:
- gnu
return_type: long double
arguments:
- type: long double
- type: int *
15 changes: 15 additions & 0 deletions libc/spec/gnu_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
RetValSpec<VoidType>,
[ArgSpec<FloatType>, ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]
>,
FunctionSpec<
"lgamma_r",
RetValSpec<DoubleType>,
[ArgSpec<DoubleType, IntPtr>]
>,
FunctionSpec<
"lgammaf_r",
RetValSpec<FloatType>,
[ArgSpec<FloatType, IntPtr>]
>,
FunctionSpec<
"lgammal_r",
RetValSpec<LongDoubleType>,
[ArgSpec<LongDoubleType, IntPtr>]
>,
]
>;

Expand Down
4 changes: 4 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,10 @@ def StdC : StandardSpec<"stdc"> {
GuardedFunctionSpec<"f16divf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,

GuardedFunctionSpec<"f16sqrtf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,

FunctionSpec<"lgamma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lgammaf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lgammal", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
]
>;

Expand Down
2 changes: 2 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ add_math_entrypoint_object(tanhf)

add_math_entrypoint_object(tgamma)
add_math_entrypoint_object(tgammaf)
add_math_entrypoint_object(lgamma)
add_math_entrypoint_object(lgamma_r)
Comment on lines +451 to +452
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: only type variants of the same function are grouped together (except a few exceptions that should be fixed), so tgamma*, lgamma* and lgamma_r* should be in three separate groups. Definitions are also sorted alphabetically, by function prefix/base name without type suffix.


add_math_entrypoint_object(totalorder)
add_math_entrypoint_object(totalorderf)
Expand Down
24 changes: 24 additions & 0 deletions libc/src/math/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,27 @@ add_entrypoint_object(
-O2
VENDOR
)

add_entrypoint_object(
lgamma
SRCS
lgamma.cpp
HDRS
../lgamma.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
lgamma_r
SRCS
lgamma_r.cpp
HDRS
../lgamma_r.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)
2 changes: 2 additions & 0 deletions libc/src/math/amdgpu/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ float __ocml_remquo_f32(float, float, gpu::Private<int> *);
double __ocml_remquo_f64(double, double, gpu::Private<int> *);
double __ocml_tgamma_f64(double);
float __ocml_tgamma_f32(float);
double __ocml_lgamma_f64(double);
double __ocml_lgamma_r_f64(double, gpu::Private<int> *);
}

} // namespace LIBC_NAMESPACE_DECL
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/amdgpu/lgamma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of the lgamma function for GPU ---------------------===//
//
// 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 "src/math/lgamma.h"
#include "src/__support/common.h"

#include "declarations.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(double, lgamma, (double x)) { return __ocml_lgamma_f64(x); }

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/math/amdgpu/lgamma_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation of the lgamma_r function for GPU -------------------===//
//
// 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 "src/math/lgamma_r.h"
#include "src/__support/common.h"

#include "declarations.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(double, lgamma_r, (double x, int *signp)) {
int tmp = *signp;
double r = __ocml_lgamma_r_f64(x, (gpu::Private<int> *)&tmp);
*signp = tmp;
return r;
}

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/math/lgamma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for lgamma ------------------------*- C++ -*-===//
//
// 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 LLVM_LIBC_SRC_MATH_LGAMMA_H
#define LLVM_LIBC_SRC_MATH_LGAMMA_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

double lgamma(double x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_LGAMMA_H
20 changes: 20 additions & 0 deletions libc/src/math/lgamma_r.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for lgamma_r-----------------------*- C++ -*-===//
//
// 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 LLVM_LIBC_SRC_MATH_LGAMMA_R_H
#define LLVM_LIBC_SRC_MATH_LGAMMA_R_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

double lgamma_r(double x, int *signp);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_LGAMMA_R_H
24 changes: 24 additions & 0 deletions libc/src/math/nvptx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,27 @@ add_entrypoint_object(
-O2
VENDOR
)

add_entrypoint_object(
lgamma
SRCS
lgamma.cpp
HDRS
../lgamma.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)

add_entrypoint_object(
lgamma_r
SRCS
lgamma_r.cpp
HDRS
../lgamma_r.h
COMPILE_OPTIONS
${bitcode_link_flags}
-O2
VENDOR
)
1 change: 1 addition & 0 deletions libc/src/math/nvptx/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ double __nv_remquo(double, double, int *);
float __nv_remquof(float, float, int *);
double __nv_tgamma(double);
float __nv_tgammaf(float);
float __nv_lgamma(double);
}

} // namespace LIBC_NAMESPACE_DECL
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/nvptx/lgamma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of the lgamma function for GPU ---------------------===//
//
// 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 "src/math/lgamma.h"
#include "src/__support/common.h"

#include "declarations.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(double, lgamma, (double x)) { return __nv_lgamma(x); }

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/math/nvptx/lgamma_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of the lgamma_r function for GPU -------------------===//
//
// 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 "src/math/lgamma_r.h"
#include "src/__support/common.h"

#include "declarations.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(double, lgamma_r, (double x, int *signp)) {
double result = __nv_lgamma(x);
*signp = (result < 0.0) ? -1 : 1;
return result;
}

} // namespace LIBC_NAMESPACE_DECL
Loading