numba_vulkan.mathimpl#

Lowering of the math module onto GLSL.std.450 via LLVM intrinsics.

Attributes#

Functions#

call_intrinsic(builder, name, args)

Call an LLVM floating point intrinsic.

libclc_name(context, pyfn, ty)

Decide whether a math function is taken from libclc.

reduced_exp(builder, x[, scale])

exp of a double, reduced here before calling libclc.

call_libclc(builder, name, args, ty)

Call a libclc math function, or this package's replacement for it.

double_words(builder, value)

The low and high 32-bit words of a double.

words_double(builder, low, high)

The double with the given low and high words.

lower_copysign(context, builder, sig, args)

Lower math.copysign by combining bit patterns.

lower_fmod(context, builder, sig, args)

Lower math.fmod: the remainder with the sign of the dividend.

float_int_power(context, builder, sig, args)

Lower float ** int and pow(float, int).

int_int_power(context, builder, sig, args)

Lower int ** int and pow(int, int).

power_override(fn, sig)

Select this package's implementation of an integer-exponent power.

Module Contents#

numba_vulkan.mathimpl.registry#
numba_vulkan.mathimpl.lower#
numba_vulkan.mathimpl.call_intrinsic(builder, name, args)#

Call an LLVM floating point intrinsic.

Parameters:
  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • name (str) – Intrinsic name without type suffix, for example "llvm.sin".

  • args (list of llvmlite.ir.Value) – Arguments, all of the same float type.

Returns:

The result, of the same type as the arguments.

Return type:

llvmlite.ir.Value

Notes

The SPIR-V backend maps these intrinsics to GLSL.std.450 extended instructions.

numba_vulkan.mathimpl.libclc_name(context, pyfn, ty)#

Decide whether a math function is taken from libclc.

libclc is preferred: its functions are accurate to the last digit or two, give the same results on every device, and exist in double precision. With fastmath, float32 functions use the device’s built-in versions instead, which are faster but less accurate and differ between drivers. float64 has no built-in alternative.

Parameters:
  • context (VulkanTargetContext) – The target context; its fast_math flag is consulted.

  • pyfn (callable) – The math function.

  • ty (numba.types.Float) – Type the function is evaluated in.

Returns:

The libclc name to call, or None to use another implementation.

Return type:

str or None

numba_vulkan.mathimpl.reduced_exp(builder, x, scale=0)#

exp of a double, reduced here before calling libclc.

libclc’s own float64 exp loses precision as the argument grows on some devices (up to 673 ulp on llvmpipe and 208 on Intel’s UHD 630 near 700, 1 ulp on NVIDIA), while it is accurate everywhere for small arguments. So x is split into k ln 2 + r with |r| <= ln(2)/2 and exact arithmetic, libclc computes exp(r), and the result is scaled by 2**k, built from its bit pattern in two factors so that neither overflows near the ends of the range.

Parameters:
  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • x (llvmlite.ir.Value) – The argument, a double.

  • scale (int, optional) – Power of two to multiply the result by, exactly; -1 gives exp(x) / 2 without overflowing where that is finite.

Returns:

exp(x) * 2**scale as a double.

Return type:

llvmlite.ir.Value

Notes

Arguments beyond about 746 in magnitude, infinities and NaN go to libclc unreduced; the result is then infinite, zero or NaN anyway.

numba_vulkan.mathimpl.call_libclc(builder, name, args, ty)#

Call a libclc math function, or this package’s replacement for it.

Parameters:
  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • name (str) – The libclc function, as returned by libclc_name.

  • args (list of llvmlite.ir.Value) – Arguments, of type ty.

  • ty (numba.types.Float) – Type the function is evaluated in.

Returns:

The result, of type ty.

Return type:

llvmlite.ir.Value

numba_vulkan.mathimpl.double_words(builder, value)#

The low and high 32-bit words of a double.

Working on words instead of a 64-bit integer keeps bit manipulation of float64 values intact when a kernel computes with 32-bit integers (see numba_vulkan.narrowing).

Returns:

low, high – i32 values.

Return type:

llvmlite.ir.Value

numba_vulkan.mathimpl.words_double(builder, low, high)#

The double with the given low and high words.

numba_vulkan.mathimpl.lower_copysign(context, builder, sig, args)#

Lower math.copysign by combining bit patterns.

The SPIR-V backend cannot select llvm.copysign.

Parameters:
  • context (VulkanTargetContext) – The target context.

  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • sig (numba.core.typing.Signature) – Signature the call was typed with.

  • args (sequence of llvmlite.ir.Value) – Argument values, in the types of sig.args.

Returns:

The magnitude of the first argument with the sign of the second.

Return type:

llvmlite.ir.Value

numba_vulkan.mathimpl.lower_fmod(context, builder, sig, args)#

Lower math.fmod: the remainder with the sign of the dividend.

Parameters:
  • context (VulkanTargetContext) – The target context.

  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • sig (numba.core.typing.Signature) – Signature the call was typed with.

  • args (sequence of llvmlite.ir.Value) – Argument values, in the types of sig.args.

Returns:

The remainder, as a value of the return type.

Return type:

llvmlite.ir.Value

numba_vulkan.mathimpl.float_int_power(context, builder, sig, args)#

Lower float ** int and pow(float, int).

A negative exponent gives the reciprocal of the positive power.

Parameters:
  • context (VulkanTargetContext) – The target context.

  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • sig (numba.core.typing.Signature) – Signature the call was typed with.

  • args (sequence of llvmlite.ir.Value) – Argument values, in the types of sig.args.

Returns:

The power, as a value of the return type.

Return type:

llvmlite.ir.Value

Notes

Numba’s implementation contains a float64 pow fallback for large exponents, which Vulkan lacks; this one only multiplies.

numba_vulkan.mathimpl.int_int_power(context, builder, sig, args)#

Lower int ** int and pow(int, int).

As in Numba, a negative exponent gives 0 unless the base is 1 or -1. Unlike Numba, 0 ** negative gives 0 instead of raising.

Parameters:
  • context (VulkanTargetContext) – The target context.

  • builder (llvmlite.ir.IRBuilder) – Builder positioned where the code is emitted.

  • sig (numba.core.typing.Signature) – Signature the call was typed with.

  • args (sequence of llvmlite.ir.Value) – Argument values, in the types of sig.args.

Returns:

The power, as a value of the return type.

Return type:

llvmlite.ir.Value

numba_vulkan.mathimpl.power_override(fn, sig)#

Select this package’s implementation of an integer-exponent power.

Numba registers its implementations per concrete type, which entries in a lowering registry cannot outrank, so VulkanTargetContext.get_function asks this function first.

Parameters:
  • fn (callable or numba.types.Function) – The function being resolved.

  • sig (numba.core.typing.Signature) – Signature of the call.

Returns:

float_int_power or int_int_power for powers with a non-literal integer exponent, otherwise None.

Return type:

callable or None