summaryrefslogtreecommitdiff
path: root/src/zisp/value.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-20 22:53:50 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-20 22:53:50 +0200
commitb84ed4f563b3536365f7d3cc4d068407e98685b3 (patch)
tree9ab7b18d712db1329b6230cb45520e7c85dc46fd /src/zisp/value.zig
parentbfaa74b19fc81dbe071d55566a78a8e329237eff (diff)
It's a revolution baby.
Diffstat (limited to 'src/zisp/value.zig')
-rw-r--r--src/zisp/value.zig521
1 files changed, 141 insertions, 380 deletions
diff --git a/src/zisp/value.zig b/src/zisp/value.zig
index 9fd9384..4ff683a 100644
--- a/src/zisp/value.zig
+++ b/src/zisp/value.zig
@@ -1,170 +1,4 @@
-//!
-//! == NaN Packing Strategy ==
-//!
-//! Format of a double, in most to least significant field order:
-//!
-//! { sign: u1, exponent: u11, fraction: u52 }
-//!
-//! When the exponent bits are all set, it's either a NaN or an Infinity.
-//!
-//! For value packing, almost all remaining 53 bits are available, giving us
-//! about 2^53 values, except for the four following bit patterns:
-//!
-//! *** FORBIDDEN VALUES ***
-//!
-//! 1. Negative cqNaN = { sign = 1, exponent = max, fraction = 2^51 }
-//!
-//! 2. Negative Infinity = { sign = 1, exponent = max, fraction = 0 }
-//!
-//! 3. Positive cqNaN = { sign = 0, exponent = max, fraction = 2^51 }
-//!
-//! 4. Positive Infinity = { sign = 0, exponent = max, fraction = 0 }
-//!
-//! The abbreviation "cqNaN" stands for canonical quiet NaN.
-//!
-//! Note that 2^51 means the MSb of the 52 fraction bits being set, and the rest
-//! being zero. The fraction MSb is also called the is_quiet flag, because it
-//! demarcates quiet NaNs. The rest being zero makes it the canonical qNaN.
-//!
-//! The positive and negative cqNaN are the *only* NaN values that can actually
-//! be returned by FP operations, which is convenient because it means we can
-//! simply use them to represent themselves in Zisp.
-//!
-//! Infinity values may also be returned by FP operations, and we want them to
-//! exist in Zisp as doubles as well, so they also represent themselves.
-//!
-//! Beyond those four bit patterns, all values with a maximum exponent (all bits
-//! set) are fair game for representing other values, so 2^53 - 4 possibilities.
-//!
-//! We split those 2^53 - 4 available values into four groups, each allowing for
-//! 2^51 - 1 different values to be encoded. (51-bit values excluding zero.)
-//!
-//! sign = 1, quiet = 1 :: Negative Fixnum from -1 to -2^51+1
-//!
-//! sign = 1, quiet = 0 :: Positive Fixnum from 0 to 2^51-2
-//!
-//! sign = 0, quiet = 1 :: Pointers and various immediates
-//!
-//! sign = 0, quiet = 0 :: Internal use by interpreter
-//!
-//!
-//! === Fixnums ===
-//!
-//! Negative fixnums actually represent themselves without needing to go through
-//! any transformation. Only the smallest 52-bit signed negative, -2^51, cannot
-//! be represented, as it would step on Forbidden Value #1, Negative cqNaN.
-//!
-//! Positive fixnums go through a bitsiwe NOT (implemented as an XOR mask to
-//! combine it with removal of NaN-related high bits) to avoid the all-zero
-//! payload value, which would step on Forbidden Value #2, Negative Infinity.
-//!
-//!
-//! === Pointers and immediates ===
-//!
-//! This region of 51-bit values is divided as follows based on the three high
-//! bits, providing a payload value of 48 bits for each.
-//!
-//! 000 :: Regular pointer to Zisp heap (type tagged)
-//!
-//! 001 :: Weak pointer to Zisp heap (type tagged)
-//!
-//! 010 :: List segment array pointer (length tagged)
-//!
-//! 011 :: Interned string pointer (untagged)
-//!
-//! 100 :: Subdivided as follows:
-//!
-//! 0....... 0....... 0....... (etc.) :: Rune
-//!
-//! 1....... :: 128 40-bit types
-//!
-//! 0....... 1....... :: 16384 32-bit types
-//!
-//! 0....... 0....... 1....... :: 2097152 24-bit types
-//!
-//! (etc.)
-//!
-//! 101 :: Short immediate string
-//!
-//! 11s :: Small rational (signed)
-//!
-//! ==== Type-tagged pointers ====
-//!
-//! Zisp heap pointers use 48-bit addresses. This is sufficient, since the
-//! address space of user-land applications is effectively 48 bits on 64-bit
-//! systems. Further, Zisp heap objects are allocated at 16-byte boundaries,
-//! meaning the lowest 4 bits are always zero; this is used for type tagging,
-//! providing immediate information about the type of object pointed to.
-//!
-//! Forbidden Value #3, Positive cqNaN, is avoided thanks to the fact that a
-//! regular Zisp heap pointer can never be null.
-//!
-//! ==== Segmented lists ====
-//!
-//! List segment arrays are also allocated at 16-byte boundaries, but use their
-//! low 4 bits for segment length; see documentation of this API for details.
-//!
-//! ==== Interned strings ====
-//!
-//! Interned string pointers don't use any low tag bits. Unlike other heap
-//! objects, they can be allocated at any address.
-//!
-//! ==== Runes and misc. small values ====
-//!
-//! Runes are symbols up to 6 ASCII characters used to implement reader syntax.
-//! They are NUL-terminated if shorter than six characters, meaning they cannot
-//! contain the NUL byte in their value.
-//!
-//! NOTE: The order in which the characters of the rune are encoded depends on
-//! endianness. On little-endian systems (i.e. most modern architectures) the
-//! characters will be in "reverse" order, with the first character in lowest
-//! position, so the terminating NUL has to be searched from low to high.
-//!
-//! The fact that runes are limited to ASCII opens up a lot of space for other
-//! small values to co-inhabit the same 48-bit range. We subdivide this space
-//! into increasingly many potential types, with smaller and smaller payloads,
-//! where the highest byte with a non-zero MSb determines which size category
-//! we're in: If the highest byte has its MSb set, then its seven non-MSb bits
-//! define a type and each type has a 40-bit value range; if the second highest
-//! byte has its MSb set, then the 14 non-MSb bits of the two high bytes define
-//! the type and each has a 32-bit value range; and so on.
-//!
-//! Unicode code points need 21 bits, so we use a 24-bit type for Characters.
-//! Miscellaneous values like true, false, nil, eof, etc. are placed into an
-//! 8-bit type, since there will never be that many of them. A virtually
-//! unlimited number of user-defined enum types can fit here.
-//!
-//! ==== Short strings ====
-//!
-//! Another 48-bit space is used for strings of zero to six bytes. Like runes,
-//! these are NUL-terminated if shorter than six bytes, meaning that NUL cannot
-//! appear in them, but otherwise they allow arbitrary bytes.
-//!
-//! ==== Small rationals ====
-//!
-//! We use a 49-bit space for small exact rational numbers, with the numerator
-//! being a two's complement 25-bit signed integer, and denominator a 24-bit
-//! unsigned integer.
-//!
-//!
-//! === Internal use values ===
-//!
-//! The final 51-bit range is used for various internal purposes by the Zisp
-//! interpreter, mostly related to transparent code optimization:
-//!
-//! 000 :: Direct function pointer aka CALL instruction
-//!
-//! 001 :: Lexically scoped variable reference by index
-//!
-//! 010 :: Pointer to heap object as constant data
-//!
-//! 011 :: Short immediate string as constant data
-//!
-//! 1.. :: TBD
-//!
-//! Forbidden Value #4, Positive Infinity, is avoided thanks to the fact that
-//! direct function pointers cannot be null.
-//!
+//! See /doc/0/0-value.md to understand the NaN-packing strategy.
const builtin = @import("builtin");
const std = @import("std");
@@ -174,17 +8,18 @@ const gc = @import("gc.zig");
pub const double = @import("value/double.zig");
pub const fixnum = @import("value/fixnum.zig");
+pub const ptr = @import("value/ptr.zig");
+pub const list = @import("value/list.zig");
+pub const istr = @import("value/istr.zig");
+pub const sstr = @import("value/sstr.zig");
+pub const srat = @import("value/srat.zig");
pub const rune = @import("value/rune.zig");
pub const char = @import("value/char.zig");
pub const misc = @import("value/misc.zig");
-pub const sstr = @import("value/sstr.zig");
-pub const boole = @import("value/boole.zig");
-pub const ptr = @import("value/ptr.zig");
-pub const pair = @import("value/pair.zig");
-pub const istr = @import("value/istr.zig");
pub const array = @import("value/array.zig");
+pub const boole = @import("value/boole.zig");
pub const string = @import("value/string.zig");
const endian = builtin.target.cpu.arch.endian();
@@ -209,11 +44,11 @@ pub fn sstrLen(x: u64) u8 {
/// Transform back and forth between rune and sstr
pub fn runeXsstr(v: Value) Value {
- return @bitCast(v.bits ^ (1 << 48));
+ return @bitCast(v.bits ^ (1 << 50));
}
// Make sure false/true only differ in LSb.
-pub const MiscValue = enum(u8) { f, t, nil, eof, none, undef };
+pub const MiscValue = enum(u8) { f, t, nil, eof, none };
// zig fmt: off
pub const f = Value{ .misc = .{ .value = .f } };
@@ -221,56 +56,56 @@ pub const t = Value{ .misc = .{ .value = .t } };
pub const nil = Value{ .misc = .{ .value = .nil } };
pub const eof = Value{ .misc = .{ .value = .eof } };
pub const none = Value{ .misc = .{ .value = .none } };
-pub const undef = Value{ .misc = .{ .value = .undef } };
// zig fmt: on
-/// A plain (unpacked, untagged) pointer into the Zisp heap. May point to any
-/// kind of object.
+/// A plain (unpacked, untagged) pointer into the Zisp heap.
pub const Zptr = *align(16) anyopaque;
/// Values for the lowest 4 bits of a heap pointer, indicating the heap type.
-pub const PtrTag = enum(u4) {
- /// Pair aka cons cell aka *[2]Value
- pair,
- /// Interned string buffer (8-bit length, then contents)
- istr,
+pub const HeapType = enum(u4) {
+ /// Unused so the 48-bit payload of a NaN-packed pointer is never zero and
+ /// cannot be confused for an actual NaN even in case of a null-index.
+ _unused = 0,
+
/// Array of various types (see `Array` type for details)
array,
- /// Procedure
- proc,
- pub fn ptype(self: PtrTag) type {
+ pub fn PtrType(self: HeapType) type {
return switch (self) {
- .pair => pair.PairPtr,
- .istr => istr.IstrPtr,
.array => array.ArrayPtr,
else => @panic("not implemented"),
};
}
};
-pub const PtrTagUIntType = @typeInfo(PtrTag).@"enum".tag_type;
+/// Make a "pointer etc." high 16-bit pattern with a 3-bit sub-range tag.
+fn ptrEtc(comptime tag: u3) u16 {
+ return @as(u16, 0x7ff8) + tag;
+}
-// "Pointer etc." high bits (sign=0,exp=MAX,quiet=1) as a u13 field.
-const ptr_etc: u13 = max(u13) - 1;
+const hi16_tag_ptr = ptrEtc(0b000);
+const hi16_tag_list = ptrEtc(0b001);
+const hi16_tag_istr = ptrEtc(0b010);
+const hi16_tag_sstr = ptrEtc(0b011);
+const hi15_tag_srat = @as(u15, @intCast(ptrEtc(0b100) >> 1));
+const hi16_tag_rune = ptrEtc(0b111);
-/// Represents a Zisp value/object.
+/// Represents a Zisp value.
pub const Value = packed union {
- /// To get the value as a regular double.
- double: f64,
-
/// To get an agnostic value for direct comparison with == i.e. eq? as well
/// as manual bit-fiddling to test for and extract packed values.
bits: u64,
+ /// Regular double, including +nan, -nan, +inf, -inf.
+ double: f64,
+
// Some of the structs below are just for inspection, whereas others are to
- // initialize a new value of that category as well as read it that way.
+ // initialize a new value of that category, as well as to read it as such.
+ // Some of these are not used at all and are merely demonstrative.
- // Note: Zig packed struct fields are ordered from LSb to MSb, contrary to
- // most diagrams used to represent IEEE 754, including our own at the top.
+ // Note: Zig packed struct fields are ordered from LSb to MSb.
/// Inspection through the lens of the general IEEE 754 double layout.
- /// (Unused because we do manual bit-fiddling for optimal results.)
ieee: packed struct {
rest: u51,
quiet: bool,
@@ -278,110 +113,84 @@ pub const Value = packed union {
sign: bool,
},
- /// For initializing and reading fixnums.
+ /// 52-bit signed fixnum
fixnum: packed struct {
code: u51,
negative: bool,
- _: u11 = max(u11),
+ _exp: u11 = max(u11),
_is_fixnum: bool = true,
},
- /// For initializing and reading pointers.
+ /// Ordinary heap pointer
ptr: packed struct {
- tagged_value: u48,
- is_weak: bool = false,
- _1: bool = false,
- _2: bool = false,
- _: u13 = ptr_etc,
- },
-
- /// List segment array pointers
- lsa: packed struct {
- tagged_value: u48,
- _id: u3 = 0b010,
- _: u13 = ptr_etc,
+ index: u44,
+ htype: HeapType,
+ _hi16_tag: u16 = hi16_tag_ptr,
},
- /// Interned string pointer
- isp: packed struct {
- pointer: u48,
- _id: u3 = 0b011,
- _: u13 = ptr_etc,
+ /// List pointer
+ list: packed struct {
+ len_tagged_ptr: u48,
+ _hi16_tag: u16 = hi16_tag_list,
},
- /// For initializing and reading runes.
- rune: packed struct {
- // actually [6]u8 but packed struct cannot contain arrays
- name: u48,
- _id: u3 = 0b100,
- _: u13 = ptr_etc,
+ /// Istr pointer
+ istr: packed struct {
+ ptr: u48,
+ _hi16_tag: u16 = hi16_tag_istr,
},
- /// For initializing and reading short strings.
+ /// Short string
sstr: packed struct {
// actually [6]u8 but packed struct cannot contain arrays
bytes: u48,
- _id: u3 = 0b101,
- _: u13 = ptr_etc,
+ _hi16_tag: u16 = hi16_tag_sstr,
},
- /// For initializing and reading small rats (rational numbers).
+ /// Small rat (rational number)
srat: packed struct {
q: u24,
p: i25,
- _tag: u2 = 0b11,
- _: u13 = ptr_etc,
+ _hi16_tag: u15 = hi15_tag_srat,
+ },
+
+ /// Rune (6-byte ASCII string)
+ rune: packed struct {
+ // actually [6]u8 but packed struct cannot contain arrays
+ name: u48,
+ _hi16_tag: u16 = hi16_tag_rune,
},
// TODO: Use a general Small Value type registration mechanism.
- /// For initializing and reading characters.
char: packed struct {
value: u24,
_sv_tag: u24 = 0x000080,
- _id: u3 = 0b100,
- _: u13 = ptr_etc,
+ _hi16_tag: u16 = hi16_tag_rune,
},
// TODO: Use a general Small Value type registration mechanism.
- /// For initializing and reading miscellaneous values.
misc: packed struct {
value: MiscValue,
_sv_tag: u40 = 0x0000000080,
- _id: u3 = 0b100,
- _: u13 = ptr_etc,
+ _hi16_tag: u16 = hi16_tag_rune,
},
- // Disjoint masks where a specific bit or bit-group are set.
- // zig fmt: off
- const mask_sign: u64 = 1 << 63 ; // 1 sign bit
- const mask_exp: u64 = max(u11) << 52 ; // 11 exponent bits
- const mask_quiet: u64 = 1 << 51 ; // 1 quiet bit of fraction
- const mask_rest: u64 = max(u51) ; // 51 rest bits of fraction
- // zig fmt: on
-
- // Mask for pointer type tag bits
- const mask_ptr_tag: u48 = max(PtrTagUIntType);
-
/// Dumps the value for inspection.
pub fn dump(v: Value) void {
- const sign: u1 = @intCast((v.bits & mask_sign) >> 63);
- const exp: u11 = @intCast((v.bits & mask_exp) >> 52);
- const quiet: u1 = @intCast((v.bits & mask_quiet) >> 51);
- const rest: u51 = @intCast(v.bits & mask_rest);
std.debug.print(
\\value: 0x{x}
\\sign: {}, exp: {b}, quiet: {}
\\rest: 0x{x}
\\
- , .{ v.bits, sign, exp, quiet, rest });
+ , .{ v.bits, v.ieee.sign, v.ieee.exp, v.ieee.quiet, v.ieee.rest });
}
- /// To treat the 64-bit value as an 8-byte array.
+ /// To treat the 64-bit value as an 8-byte const array.
pub fn bufRO(v: *const Value) *const [8]u8 {
return @ptrCast(v);
}
- /// To treat the 64-bit value as an 8-byte array.
+ /// To treat the 64-bit value as an 8-byte mutable array.
pub fn bufRW(v: *Value) *[8]u8 {
return @ptrCast(v);
}
@@ -391,19 +200,11 @@ pub const Value = packed union {
return v1.bits == v2.bits;
}
- // It would be great if we could just write the most readable code here,
- // using the packed struct definitions above, but the optimizer isn't
- // sufficiently intelligent to turn that into optimal instructions, so
- // manual bit fiddling it is.
+ // It would be great if we could just write the most readable code in the
+ // following functions, using the packed struct definitions above, but the
+ // optimizer isn't smart enough, so manual bit fiddling it is.
- // The isDouble and isFixnum checks are not as efficient as we would like
- // them to be, each requiring two 10-byte MOV instructions to load 64-bit
- // values for masking and comparison. This seems difficult to improve on
- // without sacrificing large value ranges in our NaN-packing scheme; it's
- // probably best to leave it like this and hope that type checks will be
- // hoisted outside hot-spots by a smart compiler.
-
- /// Checks for a Zisp double, including: +nan.0, -nan.0, +inf.0, -inf.0
+ /// Checks for a double, including: +nan, -nan, +inf, -inf.
pub fn isDouble(v: Value) bool {
// Readable version:
//
@@ -411,18 +212,22 @@ pub const Value = packed union {
//
// Optimized:
//
- // 1. AND away the sign and quiet bits, since they may vary.
+ // 1. Shift the 12 highest bits all the way to the right, then mask out
+ // the sign bit. Allows efficiently checking if the exponent is all
+ // set by comparing to 0x7ff.
//
- // 2. If exp is all 1, and rest non-zero, it's a packed value, so check
- // if the value is less than that.
+ // 2. Shift out 13 high bits (sign, expt, quiet) so we can easily check
+ // if the rest bits are zero.
//
- return v.bits & ~(mask_sign | mask_quiet) <= mask_exp;
+ const expt = v.bits >> 52 & 0x7ff;
+ const rest = v.bits << 13;
+ return expt != 0x7ff or rest == 0;
}
// Imagine there's an isPacked() implemented as !isDouble() here, used to
// make the following functions more readable.
- /// Checks for a fixnum.
+ /// Checks for a fixnum integer.
pub fn isFixnum(v: Value) bool {
// Readable version:
//
@@ -430,137 +235,93 @@ pub const Value = packed union {
//
// Optimized:
//
- // 1. AND away the quiet bit, since it may vary.
+ // 1. Shift the 12 highest bits all the way to the right; these need to
+ // be all set, since the sign bit (and all exponent bits) being set
+ // marks a fixnum.
//
- // 2. Check if highest 12 all 1, rest not all 0, with a greater-than.
+ // 2. But also verify that the lowest 51 bits aren't zero, since that
+ // would be a cqNaN or Infinity.
//
- return v.bits & ~mask_quiet > mask_sign | mask_exp;
+ const expt = v.bits >> 52;
+ const rest = v.bits << 13;
+ return expt == 0xfff and rest != 0;
+ }
+
+ /// Checks if the value is any type-tagged pointer. You probably want to
+ /// use getPtr() or getPtrAny() instead, so you can combine the check with
+ /// the extraction of the pointer and type tag.
+ pub fn isPtrAny(v: Value) bool {
+ const hi: u16 = @intCast(v.bits >> 48);
+ const lo: u48 = @truncate(v.bits);
+ return hi == hi16_tag_ptr and lo != 0;
}
/// Checks for a pointer with a given type tag, returning null on failure
- /// and the pointer value otherwise. This function is for when a value is
- /// assumed to be of a heap type, such as when `car` is called on it, and
- /// the "high" pointer tags (49-51) that encode properties like weakness
- /// don't matter; we only care if it's some kind of valid heap pointer of
- /// the given type, so we only check the type tag in the low bits (0-2).
- pub fn getPtr(v: Value, comptime tag: PtrTag) ?tag.ptype() {
- // Readable version:
- //
- // if (v.isPacked() and !v.ieee.sign and v.ieee.quiet ...)
- //
- // Optimized:
- //
- // I'm not explaining that shit. Figure it out yourself.
- //
- // Jokes aside, the code should be self-explanatory, except for:
- //
- // Intermediate values should be given explicit types here, for optimal
- // code-gen. And it should be noted that the null pointer, with no
- // other high bits set (49-51), which would represent +cqNaN, also
- // results in a null return, so it can't be mistaken for a pointer.
- //
- // This function currently assumes that bits 50 and 51 being set, which
- // has no defined semantics right now, doesn't change the fact that the
- // pointer is a Zisp heap pointer. If we decide to use those bits for
- // something else, simply change the hi_bits check to include them, so
- // that for example bits 50 and 51 must be zero, or 51 must be zero
- // while 50 is still ignored, and so on, as appropriate.
- //
- const hi_bits: u14 = @intCast(v.bits >> 50);
- const ptr_val: u48 = @intCast(v.bits & ~mask_ptr_tag);
- const tag_bits: PtrTagUIntType = @intCast(v.bits & mask_ptr_tag);
- const is_ptr = hi_bits == 0b01111111111110;
- const is_tag = tag_bits == @intFromEnum(tag);
- return if (is_ptr and is_tag) @ptrFromInt(ptr_val) else null;
+ /// and the pointer value otherwise.
+ pub fn getPtr(v: Value, comptime ht: HeapType) ?ht.PtrType() {
+ // Check if the 20 highest bits equal the combination of the 16-bit
+ // pattern marking a pointer, followed by the 4-bit heap type tag.
+ const hi20_bits: u20 = @intCast(v.bits >> 44);
+ const ptr_bits: u20 = hi16_tag_ptr;
+ const tag_bits: u20 = @intFromEnum(ht);
+ if (hi20_bits != ptr_bits << 4 | tag_bits) return null;
+
+ // A zero payload value here would actually indicate that the value is
+ // not a pointer at all, but the Positive cqNaN value; this is totally
+ // fine, because @ptrFromInt() turns zero into null anyway.
+ return @ptrFromInt(v.bits << 20 >> 16);
}
/// Checks for a pointer and returns the value and tag separately, or null
- /// if this isn't a pointer at all. This could be useful for a dispatch
- /// table based on type.
- pub fn getPtrAny(v: Value) ?struct { Zptr, PtrTag } {
- // See last function, which is almost identical.
- const hi_bits: u14 = @intCast(v.bits >> 50);
- const ptr_val: u48 = @intCast(v.bits & ~mask_ptr_tag);
- const tag_bits: PtrTagUIntType = @intCast(v.bits & mask_ptr_tag);
- const is_ptr = hi_bits == 0b01111111111110;
- const zptr: Zptr = @ptrFromInt(ptr_val);
- const tag: PtrTag = @enumFromInt(tag_bits);
- return if (is_ptr) .{ zptr, tag } else null;
+ /// if this isn't a pointer at all. Could be useful for a dispatch table.
+ pub fn getPtrAny(v: Value) ?struct { Zptr, HeapType } {
+ const hi16_bits: u16 = @intCast(v.bits >> 48);
+ if (hi16_bits != hi16_tag_ptr) return null;
+
+ const ptr_val: u48 = @intCast(v.bits << 20 >> 16);
+ if (ptr_val == 0) return null;
+
+ const ht_val: u4 = @intCast(v.bits << 16 >> 60);
+ return .{ @ptrFromInt(ptr_val), @enumFromInt(ht_val) };
}
- /// Checks whether the value is a pointer with certain property bits, not
- /// caring about the type tag bits. NOTE: This function doesn't check for
- /// +cqNaN, which will be mis-identified as a pointer with zero props and
- /// address. Therefore, don't expose this function to Zisp! Parameter
- /// `props_mask` specifies props bits to mask away with a NAND, so it's
- /// possible to only check for bits you care for. For example, to check
- /// weakness, pass `0b110` for `props_mask` and `0b001` for `props`.
- pub fn isPtrProps(v: Value, props_mask: u3, props: u3) bool {
- const hi_bits: u16 = @intCast(v.bits >> 48 & ~@as(u16, props_mask));
- return hi_bits == 0b0111111111111000 | @as(u16, props);
+ /// Checks if the value is a list pointer.
+ pub fn isList(v: Value) bool {
+ return v.bits >> 48 == hi16_tag_list;
}
- /// Checks whether the value is any kind of pointer, making sure not to
- /// mis-identify +cqNaN as a null pointer with zero property bits, which
- /// makes this a bit slower than `isPtrProps()`.
- pub fn isPtrAny(v: Value) bool {
- const hi: u14 = @intCast(v.bits >> 50);
- const lo: u50 = @truncate(v.bits);
- return hi == 0b01111111111110 and lo != 0;
+ /// Checks if the value is an istr pointer.
+ pub fn isIstr(v: Value) bool {
+ return v.bits >> 48 == hi16_tag_istr;
+ }
+
+ /// Checks if the value is a short string.
+ pub fn isSstr(v: Value) bool {
+ return v.bits >> 48 == hi16_tag_sstr;
}
- // TODO: Needs update from here on, and test the above
+ /// Checks if the value is a small rat (rational number).
+ pub fn isSrat(v: Value) bool {
+ return v.bits >> 49 == hi15_tag_srat;
+ }
/// Checks for a rune.
pub fn isRune(v: Value) bool {
- //
- // 1. AND away non-MSb bits of the low 6 bytes, since they may vary.
- //
- // 2. Check if the rest is the exact pattern we expect, with sign = 0,
- // exp all 1, quiet = 0, 3-bit tag = 0, and MSb of remaining bytes 0,
- // i.e., only the exp bits are set.
- //
- // 3. Ensnure that the "first" (based on endianness) of the 6 lowest
- // bytes is not NUL, which would mean it's the illegal "empty string"
- // rune corresponding to +Infinity.
- //
- // Note that, while this is a rather complicated check for a type that
- // appears ubiquitously in parser output, we rarely use this type check,
- // instead using direct comparison to specific rune values, so it's not
- // important if this is a little inefficient.
- //
- const msb_mask = 0xffff808080808080;
- const nul_mask = switch (endian) {
- .big => 127 << 40,
- .little => 127,
- };
- return v.bits & msb_mask == mask_exp and v.bits & nul_mask != 0;
+ // This check isn't used much, since one typically checks for specific
+ // runes via direct equality, so the efficiency here is unimportant.
+ const hi = v.bits >> 48;
+ const lo = v.bits << 16;
+ const msb_mask = 0x7f7f7f7f7f7f0000;
+ return hi == 0x7fff and lo != 0 and lo & msb_mask == lo;
}
- // The rest are straightforward compared to the above, since the cqNaN and
- // Infinity special-cases are out of the way and we just need to compare N
- // high bits to a specific pattern.
-
- /// Checks for a 24-bit character value. This makes no guarantees related
- /// to Unicode, such as being a valid Code Point or Unicode Scalar Value.
+ // TODO: Use a general Small Value type registration mechanism.
pub fn isChar(v: Value) bool {
- // TODO: Use a general Small Value type registration mechanism.
- return v.bits >> 24 == 0x7ff0000080;
+ return v.bits >> 24 == 0x7fff000080;
}
- /// Checks for an 8-bit miscellaneous value.
+ // TODO: Use a general Small Value type registration mechanism.
pub fn isMisc(v: Value) bool {
- // TODO: Use a general Small Value type registration mechanism.
- return v.bits >> 8 == 0x7ff00000000080;
- }
-
- /// Checks if the value is a short string of either kind.
- pub fn isSstr(v: Value) bool {
- return v.bits >> 48 == 0x7ff1;
- }
-
- /// Checks if the value is a small rat (rational number).
- pub fn isSrat(v: Value) bool {
- return v.bits >> 49 == 0x7ff2 >> 1;
+ return v.bits >> 8 == 0x7fff0000000080;
}
};