How unpackDouble2x32 Works in GLSL
The unpackDouble2x32 function in the OpenGL Shading
Language (GLSL) deconstructs a 64-bit double-precision floating-point
value into two 32-bit unsigned integers. Rather than performing a
numerical conversion, it performs a raw bit-level extraction, splitting
the underlying binary representation into a two-component unsigned
integer vector (uvec2). This article details the structural
mechanics of unpackDouble2x32, the bit-significance order
of its output, and why it is essential for low-level shader
programming.
Function Signature and Core Behavior
In GLSL, the function is defined as:
uvec2 unpackDouble2x32(double v);When called, unpackDouble2x32 interprets the 64-bit IEEE
754 representation of the input parameter v and splits it
directly down the middle. It yields a uvec2 where the
original bit pattern is preserved completely without rounding,
truncation, or floating-point normalization.
Component Mapping: LSB vs. MSB
The 64 bits are distributed into the returned uvec2
vector based on strict endianness rules defined by the GLSL
specification:
.x(First Component): Contains the 32 least significant bits (LSBs) of the double-precision float..y(Second Component): Contains the 32 most significant bits (MSBs) of the double-precision float.
Under the IEEE 754 standard for 64-bit binary floating-point numbers:
- Bit 63: Sign bit.
- Bits 62–52: 11-bit biased exponent.
- Bits 51–0: 52-bit fraction (mantissa).
Because of this layout, the .y component of the
resulting uvec2 contains the sign bit, the entirety of the
11-bit exponent, and the highest 20 bits of the mantissa. The
.x component holds the remaining 32 lower-order bits of the
mantissa.
Reversing the Operation
The reverse operation is handled by packDouble2x32:
double packDouble2x32(uvec2 v);Passing the output of unpackDouble2x32 directly into
packDouble2x32 reconstructs the original 64-bit double
value bit-for-bit, including special conditions like subnormal values,
infinities, and NaNs.
Typical Use Cases
- Hardware and Buffer Interoperability: Modern graphics pipelines often interface with Shader Storage Buffer Objects (SSBOs) or uniform buffers where alignment rules or platform limitations make native 64-bit operations complex. Unpacking allows shaders to store or transmit 64-bit data as standard 32-bit unsigned integer pairs.
- Bitwise Inspection and Manipulation: GLSL does not
allow bitwise operators (
&,|,^,<<,>>) directly ondoubletypes. Deconstructing the value into unsigned integers enables bit manipulation—such as manually extracting the exponent or modifying the sign bit. - Emulated Extended Precision: Shaders performing
high-precision geometric calculations (such as deep-zoom fractals or
large-coordinate GIS rendering) can use
unpackDouble2x32to split doubles into components for customized high-precision floating-point arithmetic routines.