Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,287 @@
|
||||
#! @file swar.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains words of bytes: eight bytes of text held in a `u64`, `bytes8`, and
|
||||
#! tested at once with the arithmetic every processor has, which is SIMD within
|
||||
#! a register. A test gives a `mask8`, the bytes it chose, which then says where
|
||||
#! the first of them is, the last, or how many there are, so text is searched
|
||||
#! eight bytes a step rather than one.
|
||||
#!
|
||||
#! The first byte of text is the lowest byte of its word, as x86-64 reads it.
|
||||
#! Every test is exact in every byte: each adds within a byte's low seven bits,
|
||||
#! which carry nothing into the byte above, so a mask can be counted, and read
|
||||
#! past its first byte, as well as tested.
|
||||
#!
|
||||
#! The text functions of `core`, as `index_of`, `find`, and `is_ascii`, work this
|
||||
#! way where vectors are not held in registers, as in the virtual machine, and
|
||||
#! on the last few bytes where they are, so most programs use words without
|
||||
#! naming them. A program reading text its own way, as the JSON reader does,
|
||||
#! names them here. What a loop asks of each word is `@inline`, so code built at
|
||||
#! `-O0` reads a word without a call for each test.
|
||||
|
||||
module core::swar;
|
||||
|
||||
|
||||
#! Eight bytes of text in a `u64`, the first in its lowest byte.
|
||||
pub type bytes8 = u64;
|
||||
|
||||
#! A choice of the bytes of a `bytes8`: the high bit of each byte chosen, and
|
||||
#! nothing else.
|
||||
pub type mask8 = u64;
|
||||
|
||||
# The low seven bits of each byte, and the high bit of each.
|
||||
const low7: u64 = 0x7F7F7F7F7F7F7F7F;
|
||||
const high1: u64 = 0x8080808080808080;
|
||||
# One in each byte, which a byte multiplied by repeats into each.
|
||||
const ones: u64 = 0x0101010101010101;
|
||||
|
||||
|
||||
#! A word of one byte repeated.
|
||||
#! @param byte The byte.
|
||||
#! @returns Eight of it.
|
||||
@inline
|
||||
pub def repeat(byte: u8): bytes8 {
|
||||
return (byte as u64) * ones;
|
||||
}
|
||||
|
||||
#! The eight bytes of text from a place.
|
||||
#! @param text The text.
|
||||
#! @param at Where the first is.
|
||||
#! @returns The word.
|
||||
#! @remarks Reading past the text's end stops the program, as indexing does, so
|
||||
#! eight bytes must be left; `load_last` reads fewer.
|
||||
pub def load(text: strview, at: u64): bytes8 {
|
||||
_ = text[at];
|
||||
_ = text[at + 7];
|
||||
return *((text.data + at) as *u64);
|
||||
}
|
||||
|
||||
#! The eight bytes from a pointer, which needn't be aligned.
|
||||
#! @param from The first byte.
|
||||
#! @returns The word.
|
||||
#! @remarks Nothing checks that eight bytes are there, as `*` checks nothing.
|
||||
@inline
|
||||
pub def load(from: *u8): bytes8 {
|
||||
return *(from as *u64);
|
||||
}
|
||||
|
||||
#! The bytes of text from a place to its end, eight at most, and zero bytes
|
||||
#! after them.
|
||||
#! @param text The text.
|
||||
#! @param at Where the first is, within the text.
|
||||
#! @returns The word, whose bytes past the text's end are zero.
|
||||
#! @remarks No byte past the text's end is read: the last few are read as
|
||||
#! `load_last` of a pointer reads them. Beginning past the end stops
|
||||
#! the program, as indexing does.
|
||||
pub def load_last(text: strview, at: u64): bytes8 {
|
||||
_ = text[at];
|
||||
val left := text.length - at;
|
||||
return load_last(text.data + at, left < 8 ? left : 8);
|
||||
}
|
||||
|
||||
#! A number of bytes from a pointer, eight at most, and zero bytes after them,
|
||||
#! read without reading any byte past them.
|
||||
#! @param from The first byte.
|
||||
#! @param count How many to read, from 0 to 8.
|
||||
#! @returns The word, whose bytes from `count` up are zero.
|
||||
#! @remarks Four to seven are read as two halves of four, which overlap, and two
|
||||
#! or three as two of two, each shifted into its place, as `hash_bytes`
|
||||
#! reads its last few.
|
||||
pub def load_last(from: *u8, count: u64): bytes8 {
|
||||
if count >= 8 {
|
||||
return *(from as *u64);
|
||||
}
|
||||
if count >= 4 {
|
||||
val low := *(from as *u32) as u64;
|
||||
val high := *((from + count - 4) as *u32) as u64;
|
||||
return low | (high << ((count - 4) * 8));
|
||||
}
|
||||
if count >= 2 {
|
||||
val low := *(from as *u16) as u64;
|
||||
val high := *((from + count - 2) as *u16) as u64;
|
||||
return low | (high << ((count - 2) * 8));
|
||||
}
|
||||
return count == 1 ? *from as u64 : 0;
|
||||
}
|
||||
|
||||
|
||||
# Tests, each exact in every byte.
|
||||
|
||||
#! The bytes that are zero.
|
||||
#! @param this The word.
|
||||
#! @returns Them.
|
||||
@inline
|
||||
pub def bytes8::zero(&this): mask8 {
|
||||
# Adding the low seven bits to seven ones sets a byte's high bit unless they
|
||||
# are all zero, and `| this` sets it where the byte's own is.
|
||||
val word: u64 = this;
|
||||
return ~(((word & low7) + low7) | word | low7);
|
||||
}
|
||||
|
||||
#! The bytes equal to a byte.
|
||||
#! @param this The word.
|
||||
#! @param byte The byte.
|
||||
#! @returns Them.
|
||||
@inline
|
||||
pub def bytes8::equal(&this, byte: u8): mask8 {
|
||||
val word: bytes8 = this ^ repeat(byte);
|
||||
return word.zero();
|
||||
}
|
||||
|
||||
#! The bytes below a bound, as unsigned numbers.
|
||||
#! @param this The word.
|
||||
#! @param bound The bound, which no byte chosen reaches.
|
||||
#! @returns Them: none where the bound is 0.
|
||||
@inline
|
||||
pub def bytes8::below(&this, bound: u8): mask8 {
|
||||
val word: u64 = this;
|
||||
# A byte under 128 reaches its high bit by the sum where it is at least the
|
||||
# bound, and one of 128 or more has it already. Over 128, a byte of 128 or more
|
||||
# is below where its low seven bits don't reach the bound less 128, and every
|
||||
# byte under 128 is.
|
||||
if bound <= 128 {
|
||||
return ~(((word & low7) + repeat(128 - bound)) | word) & high1;
|
||||
}
|
||||
return ~(((word & low7) + repeat((256 - bound as u64) as u8)) & word) & high1;
|
||||
}
|
||||
|
||||
#! The bytes above a bound, as unsigned numbers.
|
||||
#! @param this The word.
|
||||
#! @param bound The bound, which every byte chosen passes.
|
||||
#! @returns Them: none where the bound is 255.
|
||||
pub def bytes8::above(&this, bound: u8): mask8 {
|
||||
return 0 if bound == 255;
|
||||
return ~this.below(bound + 1) & high1;
|
||||
}
|
||||
|
||||
#! The bytes from one bound to another, both included, as unsigned numbers.
|
||||
#! @param this The word.
|
||||
#! @param low The least byte chosen.
|
||||
#! @param high The greatest.
|
||||
#! @returns Them: none where `low` is above `high`.
|
||||
@inline
|
||||
pub def bytes8::within(&this, low: u8, high: u8): mask8 {
|
||||
val under: u64 = high == 255 ? high1 : this.below(high + 1);
|
||||
return under & ~this.below(low);
|
||||
}
|
||||
|
||||
#! The bytes that are ASCII, below 128.
|
||||
#! @param this The word.
|
||||
#! @returns Them.
|
||||
pub def bytes8::ascii(&this): mask8 {
|
||||
return ~this & high1;
|
||||
}
|
||||
|
||||
|
||||
# Changes.
|
||||
|
||||
#! The word with each ASCII letter in upper case, as `u8::to_upper` has it.
|
||||
#! @param this The word.
|
||||
#! @returns The word changed, where `a` to `z` become `A` to `Z`.
|
||||
pub def bytes8::upper(&this): bytes8 {
|
||||
# A letter's cases differ by 0x20, which is a mask's high bit moved down two.
|
||||
return this ^ (this.within('a', 'z') >> 2);
|
||||
}
|
||||
|
||||
#! The word with each ASCII letter in lower case, as `u8::to_lower` has it.
|
||||
#! @param this The word.
|
||||
#! @returns The word changed, where `A` to `Z` become `a` to `z`.
|
||||
pub def bytes8::lower(&this): bytes8 {
|
||||
return this ^ (this.within('A', 'Z') >> 2);
|
||||
}
|
||||
|
||||
#! One byte of the word.
|
||||
#! @param this The word.
|
||||
#! @param index Which, from 0 for the first to 7.
|
||||
#! @returns It.
|
||||
pub def bytes8::byte(&this, index: u64): u8 {
|
||||
return ((this >> ((index & 7) * 8)) & 0xFF) as u8;
|
||||
}
|
||||
|
||||
#! The number eight ASCII digits write, the first the most significant, as
|
||||
#! `20261016` is written.
|
||||
#! @param this The word.
|
||||
#! @returns The number, or `nothing` where a byte is no digit.
|
||||
pub def bytes8::digits(&this): maybe<u64> {
|
||||
return nothing {} if this.within('0', '9') != high1;
|
||||
|
||||
# Each byte its digit, which joins its neighbor's into pairs, quads, and the
|
||||
# eight: the first of each is the more significant, and lower in the word.
|
||||
var value: u64 = this - repeat('0');
|
||||
value = (value * 10 + (value >> 8)) & 0x00FF00FF00FF00FF;
|
||||
value = (value * 100 + (value >> 16)) & 0x0000FFFF0000FFFF;
|
||||
value = (value * 10000 + (value >> 32)) & 0xFFFFFFFF;
|
||||
return value;
|
||||
}
|
||||
|
||||
#! The bytes of one word where a mask chooses them, and of another elsewhere.
|
||||
#! @param which The mask.
|
||||
#! @param then The word whose bytes the mask chooses.
|
||||
#! @param otherwise The word whose bytes it doesn't.
|
||||
#! @returns The word of both.
|
||||
pub def choose(which: mask8, then: bytes8, otherwise: bytes8): bytes8 {
|
||||
val chosen := which.spread();
|
||||
return (then & chosen) | (otherwise & ~chosen);
|
||||
}
|
||||
|
||||
|
||||
# Masks.
|
||||
|
||||
#! Whether the mask chooses any byte.
|
||||
#! @param this The mask.
|
||||
#! @returns Whether it does.
|
||||
@inline
|
||||
pub def mask8::any(&this): bool {
|
||||
return this != 0;
|
||||
}
|
||||
|
||||
#! The first byte the mask chooses, the lowest.
|
||||
#! @param this The mask.
|
||||
#! @returns Its index, from 0 to 7, or 8 where it chooses none.
|
||||
@inline
|
||||
pub def mask8::first(&this): u64 {
|
||||
return trailing_zeros::<u64>(this) as u64 >> 3;
|
||||
}
|
||||
|
||||
#! The last byte the mask chooses, the highest.
|
||||
#! @param this The mask.
|
||||
#! @returns Its index, from 0 to 7, or 8 where it chooses none.
|
||||
pub def mask8::last(&this): u64 {
|
||||
return 8 if this == 0;
|
||||
return (63 - leading_zeros::<u64>(this) as u64) >> 3;
|
||||
}
|
||||
|
||||
#! How many bytes the mask chooses.
|
||||
#! @param this The mask.
|
||||
#! @returns How many, from 0 to 8.
|
||||
@inline
|
||||
pub def mask8::count(&this): u64 {
|
||||
# Each byte's 1, multiplied by ones, is summed into the top byte.
|
||||
return ((this >> 7) * ones) >> 56;
|
||||
}
|
||||
|
||||
#! The mask without its first byte, to go on to the next.
|
||||
#! @param this The mask.
|
||||
#! @returns It.
|
||||
@inline
|
||||
pub def mask8::rest(&this): mask8 {
|
||||
return this & (this - 1);
|
||||
}
|
||||
|
||||
#! The mask of the first bytes of a word, as the end of text keeps them.
|
||||
#! @param count How many, from 0; 8 or more chooses every byte.
|
||||
#! @returns It.
|
||||
@inline
|
||||
pub def mask8::leading(count: u64): mask8 {
|
||||
return high1 if count >= 8;
|
||||
return high1 & ((1u << (count * 8)) - 1);
|
||||
}
|
||||
|
||||
#! Every bit of each byte the mask chooses.
|
||||
#! @param this The mask.
|
||||
#! @returns A word of `0xFF` in each byte chosen, and zero in the rest.
|
||||
pub def mask8::spread(&this): bytes8 {
|
||||
return (this >> 7) * 0xFF;
|
||||
}
|
||||
Reference in new issue
Block a user