core/math/bits

math_bits

Constants

28

Procedures

77

add_u32

add_u32 :: proc(x: u32, y: u32, carry: u32) -> (sum: u32, carry_out: u32)Source

Add with carry

  • x: The unsigned integer
  • y: Another unsigned integer
  • carry: Carry in
  • sum: The sum
  • carry_out: Carry out

add_u64

add_u64 :: proc(x: u64, y: u64, carry: u64) -> (sum: u64, carry_out: u64)Source

Add with carry

  • x: The unsigned integer
  • y: Another unsigned integer
  • carry: Carry in
  • sum: The sum
  • carry_out: Carry out

add_uint

add_uint :: proc(x: uint, y: uint, carry: uint) -> (sum: uint, carry_out: uint)Source

Add with carry

  • x: The unsigned integer
  • y: Another unsigned integer
  • carry: Carry in
  • sum: The sum
  • carry_out: Carry out

bitfield_extract_i8

bitfield_extract_i8 :: proc(value: i8, offset: uint, bits: uint) -> (i8)Source

Extracts bits from a signed integer

  • value: Signed integer
  • offset: Offset (counting from LSB) at which to extract
  • bits: Number of bits to extract
  • res: bits bits starting at offset offset

bitfield_extract_u16

bitfield_extract_u16 :: proc(value: u16, offset: uint, bits: uint) -> (res: u16)Source

Extracts bits from an unsigned integer

  • value: Unsigned integer
  • offset: Offset (counting from LSB) at which to extract
  • bits: Number of bits to extract
  • res: bits bits starting at offset offset

bitfield_extract_u32

bitfield_extract_u32 :: proc(value: u32, offset: uint, bits: uint) -> (res: u32)Source

Extracts bits from an unsigned integer

  • value: Unsigned integer
  • offset: Offset (counting from LSB) at which to extract
  • bits: Number of bits to extract
  • res: bits bits starting at offset offset

bitfield_extract_u64

bitfield_extract_u64 :: proc(value: u64, offset: uint, bits: uint) -> (res: u64)Source

Extracts bits from an unsigned integer

  • value: Unsigned integer
  • offset: Offset (counting from LSB) at which to extract
  • bits: Number of bits to extract
  • res: bits bits starting at offset offset

bitfield_extract_u8

bitfield_extract_u8 :: proc(value: u8, offset: uint, bits: uint) -> (res: u8)Source

Extracts bits from an unsigned integer

  • value: Unsigned integer
  • offset: Offset (counting from LSB) at which to extract
  • bits: Number of bits to extract
  • res: bits bits starting at offset offset

bitfield_insert_i128

bitfield_insert_i128 :: proc(base: i128, insert: i128, offset: uint, bits: uint) -> (res: i128)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_i16

bitfield_insert_i16 :: proc(base: i16, insert: i16, offset: uint, bits: uint) -> (res: i16)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_i32

bitfield_insert_i32 :: proc(base: i32, insert: i32, offset: uint, bits: uint) -> (res: i32)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_i64

bitfield_insert_i64 :: proc(base: i64, insert: i64, offset: uint, bits: uint) -> (res: i64)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_i8

bitfield_insert_i8 :: proc(base: i8, insert: i8, offset: uint, bits: uint) -> (res: i8)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_int

bitfield_insert_int :: proc(base: int, insert: int, offset: uint, bits: uint) -> (res: int)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_u128

bitfield_insert_u128 :: proc(base: u128, insert: u128, offset: uint, bits: uint) -> (res: u128)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_u16

bitfield_insert_u16 :: proc(base: u16, insert: u16, offset: uint, bits: uint) -> (res: u16)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_u32

bitfield_insert_u32 :: proc(base: u32, insert: u32, offset: uint, bits: uint) -> (res: u32)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_u64

bitfield_insert_u64 :: proc(base: u64, insert: u64, offset: uint, bits: uint) -> (res: u64)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_u8

bitfield_insert_u8 :: proc(base: u8, insert: u8, offset: uint, bits: uint) -> (res: u8)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

bitfield_insert_uint

bitfield_insert_uint :: proc(base: uint, insert: uint, offset: uint, bits: uint) -> (res: uint)Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

div_u32

div_u32 :: proc(hi: u32, lo: u32, y: u32) -> (quo: u32, rem: u32)Source

Divide a 64-bit unsigned integer (in two 32-bit words) by a 32-bit divisor

  • hi: High word of 64-bit integer
  • lo: Low word of 64-bit integer
  • y: Divisor
  • quo: 32-bit quotient
  • rem: 32-bit remainder

div_u64

div_u64 :: proc(hi: u64, lo: u64, y: u64) -> (quo: u64, rem: u64)Source

Divide a 128-bit unsigned integer (in two 64-bit words) by a 64-bit divisor

  • hi: High word of 128-bit integer
  • lo: Low word of 128-bit integer
  • y: Divisor
  • quo: 64-bit quotient
  • rem: 64-bit Remainder

div_uint

div_uint :: proc(hi: uint, lo: uint, y: uint) -> (quo: uint, rem: uint)Source

Divide an unsigned integer (in two words) by a divisor

  • hi: High word of input
  • lo: Low word of input
  • y: Divisor
  • quo: Quotient
  • rem: Remainder

from_be_u16

from_be_u16 :: proc(i: u16) -> (u16)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_be_u32

from_be_u32 :: proc(i: u32) -> (u32)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_be_u64

from_be_u64 :: proc(i: u64) -> (u64)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_be_u8

from_be_u8 :: proc(i: u8) -> (u8)Source

Returns unsigned integer i

NOTE: A byte has no endianness, so from_be_u8 exists to be complementary to from_be_*.

  • i: The unsigned integer
  • res: i

from_be_uint

from_be_uint :: proc(i: uint) -> (uint)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_le_u16

from_le_u16 :: proc(i: u16) -> (u16)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_le_u32

from_le_u32 :: proc(i: u32) -> (u32)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_le_u64

from_le_u64 :: proc(i: u64) -> (u64)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

from_le_u8

from_le_u8 :: proc(i: u8) -> (u8)Source

Returns unsigned integer i

NOTE: A byte has no endianness, so from_le_u8 exists to be complementary to from_le_*.

  • i: The unsigned integer
  • res: i

from_le_uint

from_le_uint :: proc(i: uint) -> (uint)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

log2

log2 :: proc(x: T) -> (res: T)Source

Returns the base-2 logarithm of an unsigned integer x

Another way to say this is that log2(x) is the position of its leading 1 bit.

NOTE: This is ill-defined for 0 as it has no 1 bits, and log2(0) will return max(T).

  • x: The unsigned integer
  • res: The base-2 logarithm of x

Example:

import "core:fmt"
import "core:math/bits"

log2_example :: proc() {
	for i in u8(1)..=8 {
		fmt.printfln("{0} ({0:4b}): {1}", i, bits.log2(i))
	}
	assert(bits.log2(  u8(0)) == max(u8))
	assert(bits.log2( u16(0)) == max(u16))
	assert(bits.log2( u32(0)) == max(u32))
	assert(bits.log2( u64(0)) == max(u64))
	assert(bits.log2(u128(0)) == max(u128))
}

Output:

1 (0001): 0
2 (0010): 1
3 (0011): 1
4 (0100): 2
5 (0101): 2
6 (0110): 2
7 (0111): 2
8 (1000): 3

mul_u32

mul_u32 :: proc(x: u32, y: u32) -> (hi: u32, lo: u32)Source

Multiply two words and return the result in high and low word

  • x: The unsigned integer
  • y: Another unsigned integer
  • hi: The result's high word
  • lo: The result's low word

mul_u64

mul_u64 :: proc(x: u64, y: u64) -> (hi: u64, lo: u64)Source

Multiply two words and return the result in high and low word

  • x: The unsigned integer
  • y: Another unsigned integer
  • hi: The result's high word
  • lo: The result's low word

mul_uint

mul_uint :: proc(x: uint, y: uint) -> (hi: uint, lo: uint)Source

Multiply two words and return the result in high and low word

  • x: The unsigned integer
  • y: Another unsigned integer
  • hi: The result's high word
  • lo: The result's low word

rotate_left

rotate_left :: proc(x: uint, k: int) -> (uint)Source

Returns unsigned integer x rotated left by k bits

Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

This is equivalent to the ROL CPU instruction.

  • x: The unsigned integer
  • k: Number of bits to rotate left by
  • res: x rotated left by k bits

rotate_left16

rotate_left16 :: proc(x: u16, k: int) -> (u16)Source

Returns unsigned integer x rotated left by k bits

Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

This is equivalent to the ROL CPU instruction.

  • x: The unsigned integer
  • k: Number of bits to rotate left by
  • res: x rotated left by k bits

rotate_left32

rotate_left32 :: proc(x: u32, k: int) -> (u32)Source

Returns unsigned integer x rotated left by k bits

Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

This is equivalent to the ROL CPU instruction.

  • x: The unsigned integer
  • k: Number of bits to rotate left by
  • res: x rotated left by k bits

rotate_left64

rotate_left64 :: proc(x: u64, k: int) -> (u64)Source

Returns unsigned integer x rotated left by k bits

Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

This is equivalent to the ROL CPU instruction.

  • x: The unsigned integer
  • k: Number of bits to rotate left by
  • res: x rotated left by k bits

rotate_left8

rotate_left8 :: proc(x: u8, k: int) -> (u8)Source

Returns unsigned integer x rotated left by k bits

Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

This is equivalent to the ROL CPU instruction.

  • x: The unsigned integer
  • k: Number of bits to rotate left by
  • res: x rotated left by k bits

Example:

import "core:fmt"
import "core:math/bits"

rotate_left8_example :: proc() {
	x := u8(13)
	for k in 0..<8 {
		fmt.printfln("{0:8b}: {1}", bits.rotate_left8(x, k), k)
	}
}

Output:

00001101: 0
00011010: 1
00110100: 2
01101000: 3
11010000: 4
10100001: 5
01000011: 6
10000110: 7

sub_u32

sub_u32 :: proc(x: u32, y: u32, borrow: u32) -> (diff: u32, borrow_out: u32)Source

Subtract with borrow

  • x: The unsigned integer
  • y: Another unsigned integer
  • borrow: Borrow in
  • diff: The difference
  • borrow_out: Borrow out

sub_u64

sub_u64 :: proc(x: u64, y: u64, borrow: u64) -> (diff: u64, borrow_out: u64)Source

Subtract with borrow

  • x: The unsigned integer
  • y: Another unsigned integer
  • borrow: Borrow in
  • diff: The difference
  • borrow_out: Borrow out

sub_uint

sub_uint :: proc(x: uint, y: uint, borrow: uint) -> (diff: uint, borrow_out: uint)Source

Subtract with borrow

  • x: The unsigned integer
  • y: Another unsigned integer
  • borrow: Borrow in
  • diff: The difference
  • borrow_out: Borrow out

to_be_u16

to_be_u16 :: proc(i: u16) -> (u16)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_be_u32

to_be_u32 :: proc(i: u32) -> (u32)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_be_u64

to_be_u64 :: proc(i: u64) -> (u64)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_be_u8

to_be_u8 :: proc(i: u8) -> (u8)Source

Returns unsigned integer i

NOTE: A byte has no endianness, so to_be_u8 exists to be complementary to to_be_*.

  • i: The unsigned integer
  • res: i

to_be_uint

to_be_uint :: proc(i: uint) -> (uint)Source

Returns unsigned integer i, byte-swapped if we're on a little endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_le_u16

to_le_u16 :: proc(i: u16) -> (u16)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_le_u32

to_le_u32 :: proc(i: u32) -> (u32)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_le_u64

to_le_u64 :: proc(i: u64) -> (u64)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

to_le_u8

to_le_u8 :: proc(i: u8) -> (u8)Source

Returns unsigned integer i

NOTE: A byte has no endianness, so to_le_u8 exists to be complementary to to_le_*.

  • i: The unsigned integer
  • res: i

to_le_uint

to_le_uint :: proc(i: uint) -> (uint)Source

Returns unsigned integer i, byte-swapped if we're on a big endian target.

  • i: The unsigned integer
  • res: i, optionally byte-swapped

Procedure Groups

8

add

add :: proc{add_u32, add_u64, add_uint}Source

Add with carry

  • x: The unsigned integer
  • y: Another unsigned integer
  • carry: Carry in
  • sum: The sum
  • carry_out: Carry out

bitfield_insert

bitfield_insert :: proc{bitfield_insert_u8, bitfield_insert_u16, bitfield_insert_u32, bitfield_insert_u64, bitfield_insert_u128, bitfield_insert_uint, bitfield_insert_i8, bitfield_insert_i16, bitfield_insert_i32, bitfield_insert_i64, bitfield_insert_i128, bitfield_insert_int}Source

Insert a subset of bits from one integer into another integer

Copies bits number of insert's lower bits to base at offset.

  • base: Original integer to insert bits into
  • insert: Integer to copy bits from
  • offset: Bit offset in base at which to place insert's bits
  • bits: Number of bits to copy
  • res: base with bits bits at offset replaced with insert's

div

div :: proc{div_u32, div_u64, div_uint}Source

Divide an unsigned integer (in two words) by a divisor

  • hi: High word of input
  • lo: Low word of input
  • y: Divisor
  • quo: Quotient
  • rem: Remainder

mul

mul :: proc{mul_u32, mul_u64, mul_uint}Source

Multiply two words and return the result in high and low word

  • x: The unsigned integer
  • y: Another unsigned integer
  • hi: The result's high word
  • lo: The result's low word

sub

sub :: proc{sub_u32, sub_u64, sub_uint}Source

Subtract with borrow

  • x: The unsigned integer
  • y: Another unsigned integer
  • borrow: Borrow in
  • diff: The difference
  • borrow_out: Borrow out

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.