Skip to content

Commit

Permalink
add amd support for scalbln
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryaSharf committed Jul 23, 2024
1 parent b38ec2a commit 470a684
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions libclc/amdgcn-amdhsa/libspirv/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ math/pow.cl
math/remainder.cl
math/round.cl
math/rsqrt.cl
math/scalbln.cl
math/sin.cl
math/sincos.cl
math/sinh.cl
Expand Down
30 changes: 30 additions & 0 deletions libclc/amdgcn-amdhsa/libspirv/math/scalbln.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// 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 <clcmacro.h>
#include <spirv/spirv.h>

#define __CLC_FUNCTION __spirv_ocl_scalbln
#define __CLC_BUILTIN __ocml_scalbln

float __ocml_scalbln_f32(float, int);
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, _f32)

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
double __ocml_scalbln_f64(double, int);
#define __CLC_BUILTIN_D __CLC_XCONCAT(__CLC_BUILTIN, _f64)
#endif // cl_khr_fp64

#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
half __ocml_scalbln_f16(half, int);
#define __CLC_BUILTIN_H __CLC_XCONCAT(__CLC_BUILTIN, _f16)
#endif // cl_khr_fp16

#include <math/binary_builtin.inc>
1 change: 1 addition & 0 deletions libclc/generic/libspirv/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ math/remquo.cl
math/rint.cl
math/rootn.cl
math/round.cl
math/scalbln.cl
math/sin.cl
math/sincos.cl
math/sincos_helpers.cl
Expand Down
61 changes: 61 additions & 0 deletions libclc/generic/libspirv/math/scalbln.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define _USE_MATH_DEFINES

#include <clcmacro.h>
#include <math/math.h>
#include <spirv/spirv.h>

int __ocml_scalbln_f64(double, int);
int __ocml_scalbln_f32(float, int);

//_CLC_DEFINE_BINARY_BUILTIN(int, __spirv_ocl_scalbln, __ocml_scalbln_f32, float, int)

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEFINE_BINARY_BUILTIN(int, __spirv_ocl_scalbln, __ocml_scalbln_f64, double, int)
#endif

#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable

_CLC_OVERLOAD _CLC_DEF int __spirv_ocl_scalbln(float x, int y) {
union {
float f;
unsigned int i;
} u;
u.f = x;

int exponent = ((u.i >> 23) & 0xFF) - 127;

if (exponent == -127 && (u.i & 0x7FFFFF) == 0) {
return 0;
} else if (exponent == 128) {
return (int)x;
}

exponent += y;

if (exponent > 127) {
return (int)(u.i & 0x80000000 ? -INFINITY : INFINITY);
} else if (exponent < -126) {
return 0;
}

u.i = (u.i & 0x807FFFFF) | ((exponent + 127) << 23);

return (int)u.f;
}

_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, __spirv_ocl_scalbln, float, int);
#endif

#undef __CLC_BUILTIN
#undef __CLC_BUILTIN_F
#undef __CLC_FUNCTION

0 comments on commit 470a684

Please sign in to comment.