Ascii.Int32 (Ascii v0.3.0)

Copy Markdown View Source

JavaScript's 32-bit integer operators.

JS converts both operands of |, &, ^, <<, >> and >>> to 32 bits and gives a signed 32-bit result (unsigned for >>>); Math.imul multiplies modulo 2^32. Elixir integers never overflow, so the pieces' hashes and PRNGs go through these functions rather than Bitwise directly.

Every function accepts any integer, or a float, which is truncated first as JS's ToInt32 does.

Summary

Types

A signed 32-bit integer.

An unsigned 32-bit integer.

Functions

a & b.

a | b.

a ^ b.

Math.imul(a, b), without building a 64-bit product.

One draw from mulberry32, the PRNG most pieces seed: given the state, the next number in [0, 1) and the next state. Start from to_int32(seed).

a >> n.

a << n.

x | 0.

x >>> 0.

x >>> 0 for a value already known to be an integer or a float, without the sign handling: the fast path for hashes, which stay unsigned.

Math.imul(a, b) >>> 0 for two unsigned 32-bit integers.

a >>> n.

Types

int32()

@type int32() :: -2_147_483_648..2_147_483_647

A signed 32-bit integer.

uint32()

@type uint32() :: 0..4_294_967_295

An unsigned 32-bit integer.

Functions

band(a, b)

@spec band(a :: number(), b :: number()) :: int32()

a & b.

bor(a, b)

@spec bor(a :: number(), b :: number()) :: int32()

a | b.

bxor(a, b)

@spec bxor(a :: number(), b :: number()) :: int32()

a ^ b.

imul(a, b)

@spec imul(a :: number(), b :: number()) :: int32()

Math.imul(a, b), without building a 64-bit product.

mulberry32(state)

@spec mulberry32(state :: int32()) :: {float(), int32()}

One draw from mulberry32, the PRNG most pieces seed: given the state, the next number in [0, 1) and the next state. Start from to_int32(seed).

a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;

sar(a, n)

@spec sar(a :: number(), n :: number()) :: int32()

a >> n.

shl(a, n)

@spec shl(a :: number(), n :: number()) :: int32()

a << n.

to_int32(x)

@spec to_int32(x :: number()) :: int32()

x | 0.

to_uint32(x)

@spec to_uint32(x :: number()) :: uint32()

x >>> 0.

u32(x)

@spec u32(x :: number()) :: uint32()

x >>> 0 for a value already known to be an integer or a float, without the sign handling: the fast path for hashes, which stay unsigned.

umul(a, b)

@spec umul(a :: uint32(), b :: uint32()) :: uint32()

Math.imul(a, b) >>> 0 for two unsigned 32-bit integers.

A hash that only ever feeds ^, >>>, + and Math.imul with its intermediate values can work in unsigned arithmetic throughout, since all four agree modulo 2^32 whatever the signs; this skips imul/2's conversions and is several times faster.

ushr(a, n)

@spec ushr(a :: number(), n :: number()) :: uint32()

a >>> n.