Repository navigation
[General]: The manual's requirements for the -:, *: and +: slice forms are off by one #101
Description
Problem
The language manual (ISA_LRM, "Indexing and bitslice expressions") gives each
slice form a requirement on its bounds. With markup removed:
B[i : j] (range-based bitslice) returns bits j up to i (inclusive) of the bitvector B.
Requires that i in {j-1 .. n-1} and j in {0 .. i+1}.
B[i +: j] (width-based bitslice) returns bits i up to i+j-1 (inclusive) of the
bitvector B. Requires that i in {0 .. n-1} and j in {0 .. n-i}.
B[i -: j] (width-based bitslice) returns bits i down to i-j+1 (inclusive) of the
bitvector B. Requires that i in {0 .. n-1} and j in {0 .. i}.
B[i *: j] (element bitslice) returns bits i*j up to i*j + j-1 (inclusive) of the
bitvector B. Requires that i in {0 .. n/j} and n % j == 0.
The SDM's pages for the width-based forms define each one as a range slice:
x[i +: w] "is equivalent to writing x[i+w-1 : i]", x[i -: w] "is
equivalent to writing x[i : i-w+1]", and x[i *: w] "is equivalent to writing
x[iw +: w] or x[i * w + (w-1) : iw]". The : requirement is correct, so each form's
requirement should be what : requires of its equivalent slice. For all three forms the
stated requirement is off by one.
B[i -: j] rejects slices that end at bit 0. Here i is the index of the top bit and
j is the width. The equivalent range slice B[i : i-j+1] has low index i-j+1, which
may be 0, so j may be i+1. The bound j in {0 .. i} excludes it. The SDM's own page
for this operator gives the example
0b1111_1110_1110_1011[3 -: 4] == 0b1011
which selects bits 3 down to 0 (i = 3, j = 4) and which the manual's requirement
rejects. reading-specs.md also states that "x[high -: width] is equivalent to
x[high : high-width+1]". At the other end, i in {0 .. n-1} rejects the empty slice
B[-1 -: 0], which is B[-1 : 0] and satisfies the : requirement.
B[i *: j] admits an element past the end. Here j is the element width and i is
the element number. A Bits(n) value has n/j elements, numbered 0 to n/j - 1. The
bound i in {0 .. n/j} admits i = n/j, whose bits n to n+j-1 do not exist. For
example, with B : Bits(16), B[4 *: 4] satisfies the requirement and selects bits 16 to
19. The requirement also uses n/j and n % j without requiring j > 0.
B[i +: j] rejects empty slices at the top. It requires i in {0 .. n-1} even when
j = 0. So B[n +: 0] fails, as does every +: slice of a Bits(0) value, although the
equivalent range slices B[n-1 : n] and, on Bits(0), B[-1 : 0] satisfy the :
requirement.
No current source reaches the empty +: and -: cases; they matter for consistency
between the slice forms.
Proposed fix
B[i -: j]
-Requires that i in {0 .. n-1} and j in {0 .. i}.
+Requires that i in {-1 .. n-1} and j in {0 .. i+1}. B[i *: j]
-Requires that i in {0 .. n/j} and n % j == 0.
+Requires j > 0, n % j == 0, and i in {0 .. n/j - 1}. B[i +: j]
-Requires that i in {0 .. n-1} and j in {0 .. n-i}.
+Requires that i in {0 .. n} and j in {0 .. n-i}.For nonempty slices (j >= 1), the +: change accepts the same slices as before, and the
-: change adds only the slices that end at bit 0.
One question for B[i *: j]: the range slice B[i*j + j-1 : i*j] requires only that the
element lies within B. Is n % j == 0 intended, so that an element width that does not
divide n, such as x[1 *: 3] on Bits(8), is an error?
For reference, isa-tools accepts all the empty slices above and does not require
n % j == 0. It checks 0 <= i, 0 <= j, i + j <= n for +: (tcheck.ml,
primops.ml); i < n, 0 <= j, j <= i+1 for -: (tcheck.ml); and
0 <= i, 0 <= j, i*j <= n - j for *: (tcheck.ml).
AI disclosure
Assisted-by: Claude
Claude assisted with source analysis, the proposed correction and drafting this report.
Reviewed with assistance from Codex.
Pinned source: definitions.xml.gz at revision 35eface1b7ee003a1cda95005ec5fd83de658a64.
Activity
[-][General]: The manual's requirements for `B[i -: j]` and `B[i *: j]` are off by one[/-][+][General]: The manual's requirements for the `-:`, `*:` and `+:` slice forms are off by one[/+]