Class GcdCodecStage
- All Implemented Interfaces:
NumericCodecStage,TransformDecoder,TransformEncoder
Effectiveness
Applied when all values share a common divisor greater than 1. Divides all values by the GCD, reducing their magnitude and therefore the number of bits required for bit-packing. Skipped when the GCD is 0 or 1 (unsigned comparison).
Example
Values [100, 200, 300, 400] with GCD=100
produces [1, 2, 3, 4].
Metadata layout
Written to the stage metadata section (see BlockFormat):
+---------------------+ | VLong(gcd - 2) | 1-9 bytes, unsigned variable-length +---------------------+
The GCD is always >= 2 when the stage applies, so subtracting 2
before encoding as unsigned VLong saves one byte for small divisors
(e.g., GCD=2 stores as 0, which is a single byte).
Power-of-two optimization
When the GCD is a power of two, division and multiplication are replaced by
arithmetic shifts. The JIT cannot optimize division by a runtime variable (it always
emits idiv, ~20-90 cycles), while shifts are single-cycle and SIMD-friendly.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoiddecode(long[] values, int valueCount, DecodingContext context) Reverses the transformation on values in-place using metadata from the context.static voiddecodeStatic(GcdCodecStage stage, long[] values, int valueCount, DecodingContext context) voidencode(long[] values, int valueCount, EncodingContext context) Transforms values in-place and writes any metadata to the context.static voidencodeStatic(GcdCodecStage stage, long[] values, int valueCount, EncodingContext context) byteid()Returns the unique stage identifier.
-
Field Details
-
INSTANCE
Singleton instance.
-
-
Method Details
-
id
public byte id()Description copied from interface:TransformEncoderReturns the unique stage identifier.- Specified by:
idin interfaceTransformDecoder- Specified by:
idin interfaceTransformEncoder- Returns:
- the stage ID byte
-
encode
Description copied from interface:TransformEncoderTransforms values in-place and writes any metadata to the context.If the stage determines that the transformation would not be effective, it may return without modifying the values or writing metadata. The pipeline checks
EncodingContext.isStageApplied(int)to detect this.- Specified by:
encodein interfaceTransformEncoder- Parameters:
values- the values to transform in-placevalueCount- the number of valid values in the arraycontext- the encoding context for metadata and stage tracking
-
decode
Description copied from interface:TransformDecoderReverses the transformation on values in-place using metadata from the context.- Specified by:
decodein interfaceTransformDecoder- Parameters:
values- the values to reverse-transform in-placevalueCount- the number of valid values in the arraycontext- the decoding context for reading stage metadata- Throws:
IOException- if an I/O error occurs while reading metadata
-
encodeStatic
public static void encodeStatic(GcdCodecStage stage, long[] values, int valueCount, EncodingContext context) throws IOException - Throws:
IOException
-
decodeStatic
public static void decodeStatic(GcdCodecStage stage, long[] values, int valueCount, DecodingContext context) throws IOException - Throws:
IOException
-