Number64ShaderCode

Package: Libraries.Compute.Hardware.Shaders · Inherits from: Libraries.Language.Object

Number64ShaderCode gives the GLSL a compute shader needs to work with Quorum's 64-bit numbers held as real 64-bit numbers on the graphics card (HardwareCompute's NUMBER_64 format: one 64-bit number per element of the buffer, as Quorum and the CPU hold them). Three ways, with the same function names, so a kernel works with any (SetArithmetic): native GLSL double: for graphics cards that have 64-bit numbers (HardwareComputeLimits:HasNumber64; NVIDIA and AMD, not Apple). Vulkan requires 64-bit adds and multiplies to be correctly rounded, and every one here is marked precise so none is fused into a multiply-add, so each step rounds exactly as Java does on the CPU. WITH_INTEGER_64 IEEE 754 64-bit add and multiply worked out with 64-bit integers (shaderInt64): Apple's cards and most others. Each result is correctly rounded (round to nearest, ties to even), with subnormal numbers, zeros, infinities and NaN, so it too rounds exactly as Java does. On an Apple M-series GPU it ran in 0.54 to 0.63 times double-float's time. WITH_INTEGER_32 the same IEEE arithmetic with 32-bit integers only, for cards without 64-bit integers (some Android) and, written in WGSL later, WebGPU, which has no 64-bit types at all. A number is a uvec2 (low word, high word) over the same 8 bytes; the 53 x 53-bit product is four umulExtended 32 x 32-bit products. The same answers, bit for bit. GetHeader the extension lines the code needs GetBufferDeclaration the storage buffer, "buff", of 64-bit numbers GetLoadAndStore loadNumber(i) and storeNumber(i, value) GetArithmetic multiplyNumbers(a, b) and addNumbers(a, b) GetNumberType the GLSL type of a number: double, uint64_t or uvec2 GetZero that type's zero Both emulated arithmetics were checked, before they were written as GLSL, in line-for-line models against real 64-bit doubles: 600,000 random pairs each (subnormals, numbers near overflow, infinities, zeros, near-cancelling pairs) and 100,000 exact ties, with no difference.

Example Code

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
numbers:SetArithmetic(numbers:WITH_INTEGER_32)
text code = "#version 450
" + numbers:GetHeader() + numbers:GetBufferDeclaration(0) + "
layout (local_size_x = 64) in;
" + numbers:GetLoadAndStore() + numbers:GetArithmetic() + "
void main()
{
uint i = gl_GlobalInvocationID.x;
storeNumber(i, multiplyNumbers(loadNumber(i), loadNumber(i)));
}
"

Variables

  • integer WITH_INTEGER_32: IEEE arithmetic with 32-bit integers: every card.
  • integer NATIVE: GLSL double: cards with 64-bit numbers.
  • integer WITH_INTEGER_64: IEEE arithmetic with 64-bit integers: cards with shaderInt64.

Actions

GetArithmetic()

This action returns multiplyNumbers(a, b) and addNumbers(a, b), each correctly rounded.

Returns

text: the GLSL functions.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetArithmetic()

GetArithmeticKind()

This action returns the arithmetic chosen.

Returns

integer: NATIVE, WITH_INTEGER_64 or WITH_INTEGER_32.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetArithmeticKind()

GetBufferDeclaration(integer binding)

This action returns the storage buffer, named buff, of 64-bit numbers.

Parameters

  • binding integer: the buffer's binding in descriptor set 0.

Returns

text: the GLSL declaration.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetBufferDeclaration(0)

GetHeader()

This action returns the extension lines, to follow #version.

Returns

text: the lines, which may be empty.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetHeader()

GetLoadAndStore()

This action returns loadNumber(i) and storeNumber(i, value): number i of the buffer.

Returns

text: the GLSL functions.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetLoadAndStore()

GetNumberType()

This action returns the GLSL type of a number.

Returns

text: "double", "uint64_t" or "uvec2".

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetNumberType()

GetZero()

This action returns the GLSL for a zero of the number type (the bits of +0.0).

Returns

text: "0.0LF", "0UL" or "uvec2(0u, 0u)".

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:GetZero()

IsEmulated()

This action says whether the arithmetic is emulated (with 64-bit or 32-bit integers).

Returns

boolean: true if it emulates.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
output numbers:IsEmulated()

SetArithmetic(integer kind)

This action chooses the arithmetic: NATIVE, WITH_INTEGER_64 or WITH_INTEGER_32.

Parameters

  • kind integer: NATIVE, WITH_INTEGER_64 or WITH_INTEGER_32.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
numbers:SetArithmetic(numbers:WITH_INTEGER_32)

SetEmulated(boolean yes)

This action chooses between GLSL double (false) and the 64-bit-integer emulation (true). SetArithmetic chooses among all three.

Parameters

  • yes boolean: true to emulate with 64-bit integers.

Example

use Libraries.Compute.Hardware.Shaders.Number64ShaderCode

Number64ShaderCode numbers
numbers:SetEmulated(true)

Inherited Actions

3 actions from Libraries.Language.Object