Top Description Fields Constructors Methods
ch.randelshofer.fastdoubleparser

pack-priv Class FastDoubleSwar

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.nio.ByteOrder

This class provides methods for parsing multiple characters at once using the "SIMD with a register" (SWAR) technique.

References:

Leslie Lamport, Multiple Byte Processing with Full-Word Instructions
azurewebsites.net
Daniel Lemire, fast_double_parser, 4x faster than strtod. Apache License 2.0 or Boost Software License.
github.com
Daniel Lemire, fast_float number parsing library: 4x faster than strtod. Apache License 2.0.
github.com
Daniel Lemire, Number Parsing at a Gigabyte per Second, Software: Practice and Experience 51 (8), 2021. arXiv.2101.11408v3 [cs.DS] 24 Feb 2021
arxiv.org

Field Summary

Modifier and TypeField and Description
private static final VarHandle
private static final VarHandle
private static final VarHandle
private static final VarHandle

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static double
fma(double a, double b, double c)

protected static boolean

Returns:

true if c is a digit
isDigit
(char
a character
c
)

Checks if '0' <= c && c <= '9'.

protected static boolean

Returns:

true if c is a digit
isDigit
(byte
a character
c
)

Checks if '0' <= c && c <= '9'.

public static boolean
isEightDigits(byte[] a, int offset)

public static boolean

Returns:

true if eight digits
isEightDigits
(char[]
a string
a
,
int
offset into string
offset
)

Checks if the string contains eight digits at the specified offset.

public static boolean
isEightDigits(CharSequence a, int offset)

public static boolean
isEightDigitsUtf16(long first, long second)

public static boolean
isEightDigitsUtf8(long chunk)

public static boolean
isEightZeroes(byte[] a, int offset)

public static boolean
isEightZeroes(CharSequence a, int offset)

public static boolean

Returns:

true if eight digits
isEightZeroes
(char[]
a string
a
,
int
offset into string
offset
)

Checks if the string contains eight zeroes at the specified offset.

public static boolean
isEightZeroesUtf16(long first, long second)

public static boolean
isEightZeroesUtf8(long chunk)

public static int
readIntBE(byte[] a, int offset)

public static long
readLongBE(byte[] a, int offset)

public static long
readLongLE(byte[] a, int offset)

public static int

Returns:

the parsed number, returns a negative value if value does not contain 8 hex digits
tryToParseEightDigits
(char[]
contains 8 utf-16 characters starting at offset
a
,
int
the offset into the array
offset
)

Tries to parse eight decimal digits from a char array using the 'SIMD within a register technique' (SWAR).

public static int
tryToParseEightDigits(byte[] a, int offset)

public static int

Returns:

the parsed digits or -1
tryToParseEightDigits
(CharSequence
a character sequence
str
,
int
the index of the first character in the character sequence
offset
)

Tries to parse eight digits at once using the 'SIMD within a register technique' (SWAR).

public static int

Returns:

the parsed digits or -1
tryToParseEightDigitsUtf16
(long
the first four characters in big endian order
first
,
long
the second four characters in big endian order
second
)

Tries to parse eight decimal digits at once using the 'SIMD within a register technique' (SWAR).

public static int

Returns:

the parsed number, returns a negative value if value does not contain 8 digits
tryToParseEightDigitsUtf8
(byte[]
contains 8 ascii characters
a
,
int
the offset of the first character in a
offset
)

Tries to parse eight decimal digits from a byte array using the 'SIMD within a register technique' (SWAR).

public static int

Returns:

the parsed number, or a value < 0 if not all characters are digits.
tryToParseEightDigitsUtf8
(long
contains 8 ascii characters in little endian order
chunk
)

Tries to parse eight digits from a long using the 'SIMD within a register technique' (SWAR).

public static long

Returns:

the parsed digits or -1
tryToParseEightHexDigits
(CharSequence
a character sequence
str
,
int
the index of the first character in the character sequence
offset
)

Tries to parse eight digits at once using the 'SIMD within a register technique' (SWAR).

public static long

Returns:

the parsed number, returns a negative value if value does not contain 8 hex digits
tryToParseEightHexDigits
(char[]
contains 8 utf-16 characters starting at offset
chars
,
int
the offset into the array
offset
)

Tries to parse eight hex digits from a char array using the 'SIMD within a register technique' (SWAR).

public static long
tryToParseEightHexDigits(byte[]
contains 8 ascii characters
a
,
int
the offset of the first character in a returns a negative value if value does not contain 8 digits
offset
)

Tries to parse eight hex digits from a byte array using the 'SIMD within a register technique' (SWAR).

public static long

Returns:

the parsed number, returns a negative value if the two longs do not contain 8 hex digits
tryToParseEightHexDigitsUtf16
(long
contains 4 utf-16 characters in big endian order
first
,
long
contains 4 utf-16 characters in big endian order
second
)

Tries to parse eight hex digits from two longs using the 'SIMD within a register technique' (SWAR).

public static long

Returns:

the parsed number, returns a negative value if value does not contain 8 digits
tryToParseEightHexDigitsUtf8
(long
contains 8 ascii characters in big endian order
chunk
)

Tries to parse eight digits from a long using the 'SIMD within a register technique' (SWAR).

public static int
tryToParseFourDigits(char[] a, int offset)

public static int
public static int
tryToParseFourDigits(byte[] a, int offset)

public static int
public static int
public static int
tryToParseUpTo7Digits(byte[] str, int from, int to)

public static int
tryToParseUpTo7Digits(char[] str, int from, int to)

public static int
tryToParseUpTo7Digits(CharSequence str, int from, int to)

public static void
writeIntBE(byte[] a, int offset, int value)

public static void
writeLongBE(byte[] a, int offset, long value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

readIntBEback to summary
private static final VarHandle readIntBE
readIntLEback to summary
private static final VarHandle readIntLE
readLongBEback to summary
private static final VarHandle readLongBE
readLongLEback to summary
private static final VarHandle readLongLE

Constructor Detail

FastDoubleSwarback to summary
pack-priv FastDoubleSwar()

Method Detail

fmaback to summary
public static double fma(double a, double b, double c)
isDigitback to summary
protected static boolean isDigit(char c)

Checks if '0' <= c && c <= '9'.

Parameters
c:char

a character

Returns:boolean

true if c is a digit

isDigitback to summary
protected static boolean isDigit(byte c)

Checks if '0' <= c && c <= '9'.

Parameters
c:byte

a character

Returns:boolean

true if c is a digit

isEightDigitsback to summary
public static boolean isEightDigits(byte[] a, int offset)
isEightDigitsback to summary
public static boolean isEightDigits(char[] a, int offset)

Checks if the string contains eight digits at the specified offset.

Parameters
a:char[]

a string

offset:int

offset into string

Returns:boolean

true if eight digits

Exceptions
IndexOutOfBoundsException:
if offset is larger than 2^29.
isEightDigitsback to summary
public static boolean isEightDigits(CharSequence a, int offset)
isEightDigitsUtf16back to summary
public static boolean isEightDigitsUtf16(long first, long second)
isEightDigitsUtf8back to summary
public static boolean isEightDigitsUtf8(long chunk)
isEightZeroesback to summary
public static boolean isEightZeroes(byte[] a, int offset)
isEightZeroesback to summary
public static boolean isEightZeroes(CharSequence a, int offset)
isEightZeroesback to summary
public static boolean isEightZeroes(char[] a, int offset)

Checks if the string contains eight zeroes at the specified offset.

Parameters
a:char[]

a string

offset:int

offset into string

Returns:boolean

true if eight digits

Exceptions
IndexOutOfBoundsException:
if offset is larger than 2^29.
isEightZeroesUtf16back to summary
public static boolean isEightZeroesUtf16(long first, long second)
isEightZeroesUtf8back to summary
public static boolean isEightZeroesUtf8(long chunk)
readIntBEback to summary
public static int readIntBE(byte[] a, int offset)
readLongBEback to summary
public static long readLongBE(byte[] a, int offset)
readLongLEback to summary
public static long readLongLE(byte[] a, int offset)
tryToParseEightDigitsback to summary
public static int tryToParseEightDigits(char[] a, int offset)

Tries to parse eight decimal digits from a char array using the 'SIMD within a register technique' (SWAR).

Parameters
a:char[]

contains 8 utf-16 characters starting at offset

offset:int

the offset into the array

Returns:int

the parsed number, returns a negative value if value does not contain 8 hex digits

Exceptions
IndexOutOfBoundsException:
if offset is larger than 2^
tryToParseEightDigitsback to summary
public static int tryToParseEightDigits(byte[] a, int offset)
tryToParseEightDigitsback to summary
public static int tryToParseEightDigits(CharSequence str, int offset)

Tries to parse eight digits at once using the 'SIMD within a register technique' (SWAR).

Parameters
str:CharSequence

a character sequence

offset:int

the index of the first character in the character sequence

Returns:int

the parsed digits or -1

tryToParseEightDigitsUtf16back to summary
public static int tryToParseEightDigitsUtf16(long first, long second)

Tries to parse eight decimal digits at once using the 'SIMD within a register technique' (SWAR).

char[] chars = ...;
long first  = chars[0]|(chars[1]<<16)|(chars[2]<<32)|(chars[3]<<48);
long second = chars[4]|(chars[5]<<16)|(chars[6]<<32)|(chars[7]<<48);
Parameters
first:long

the first four characters in big endian order

second:long

the second four characters in big endian order

Returns:int

the parsed digits or -1

tryToParseEightDigitsUtf8back to summary
public static int tryToParseEightDigitsUtf8(byte[] a, int offset)

Tries to parse eight decimal digits from a byte array using the 'SIMD within a register technique' (SWAR).

Parameters
a:byte[]

contains 8 ascii characters

offset:int

the offset of the first character in a

Returns:int

the parsed number, returns a negative value if value does not contain 8 digits

tryToParseEightDigitsUtf8back to summary
public static int tryToParseEightDigitsUtf8(long chunk)

Tries to parse eight digits from a long using the 'SIMD within a register technique' (SWAR).

byte[] bytes = ...;
long value  = ((bytes[7]&0xffL)<<56)
            | ((bytes[6]&0xffL)<<48)
            | ((bytes[5]&0xffL)<<40)
            | ((bytes[4]&0xffL)<<32)
            | ((bytes[3]&0xffL)<<24)
            | ((bytes[2]&0xffL)<<16)
            | ((bytes[1]&0xffL)<< 8)
            |  (bytes[0]&0xffL);
Parameters
chunk:long

contains 8 ascii characters in little endian order

Returns:int

the parsed number, or a value < 0 if not all characters are digits.

tryToParseEightHexDigitsback to summary
public static long tryToParseEightHexDigits(CharSequence str, int offset)

Tries to parse eight digits at once using the 'SIMD within a register technique' (SWAR).

Parameters
str:CharSequence

a character sequence

offset:int

the index of the first character in the character sequence

Returns:long

the parsed digits or -1

tryToParseEightHexDigitsback to summary
public static long tryToParseEightHexDigits(char[] chars, int offset)

Tries to parse eight hex digits from a char array using the 'SIMD within a register technique' (SWAR).

Parameters
chars:char[]

contains 8 utf-16 characters starting at offset

offset:int

the offset into the array

Returns:long

the parsed number, returns a negative value if value does not contain 8 hex digits

tryToParseEightHexDigitsback to summary
public static long tryToParseEightHexDigits(byte[] a, int offset)

Tries to parse eight hex digits from a byte array using the 'SIMD within a register technique' (SWAR).

Parameters
a:byte[]

contains 8 ascii characters

offset:int

the offset of the first character in a returns a negative value if value does not contain 8 digits

tryToParseEightHexDigitsUtf16back to summary
public static long tryToParseEightHexDigitsUtf16(long first, long second)

Tries to parse eight hex digits from two longs using the 'SIMD within a register technique' (SWAR).

char[] chars = ...;
long first  = (long) chars[0] << 48
            | (long) chars[1] << 32
            | (long) chars[2] << 16
            | (long) chars[3];

long second = (long) chars[4] << 48
            | (long) chars[5] << 32
            | (long) chars[6] << 16
            | (long) chars[7];
Parameters
first:long

contains 4 utf-16 characters in big endian order

second:long

contains 4 utf-16 characters in big endian order

Returns:long

the parsed number, returns a negative value if the two longs do not contain 8 hex digits

tryToParseEightHexDigitsUtf8back to summary
public static long tryToParseEightHexDigitsUtf8(long chunk)

Tries to parse eight digits from a long using the 'SIMD within a register technique' (SWAR).

Parameters
chunk:long

contains 8 ascii characters in big endian order

Returns:long

the parsed number, returns a negative value if value does not contain 8 digits

tryToParseFourDigitsback to summary
public static int tryToParseFourDigits(char[] a, int offset)
tryToParseFourDigitsback to summary
public static int tryToParseFourDigits(CharSequence str, int offset)
tryToParseFourDigitsback to summary
public static int tryToParseFourDigits(byte[] a, int offset)
tryToParseFourDigitsUtf16back to summary
public static int tryToParseFourDigitsUtf16(long first)
tryToParseFourDigitsUtf8back to summary
public static int tryToParseFourDigitsUtf8(int chunk)
tryToParseUpTo7Digitsback to summary
public static int tryToParseUpTo7Digits(byte[] str, int from, int to)
tryToParseUpTo7Digitsback to summary
public static int tryToParseUpTo7Digits(char[] str, int from, int to)
tryToParseUpTo7Digitsback to summary
public static int tryToParseUpTo7Digits(CharSequence str, int from, int to)
writeIntBEback to summary
public static void writeIntBE(byte[] a, int offset, int value)
writeLongBEback to summary
public static void writeLongBE(byte[] a, int offset, long value)