Repository navigation
Description
Problem
The language manual's standard library (ISA_LRM, "Bitvector functions") declares:
function Std::Bits::Count_Set_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
function Std::Bits::Count_Leading_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
function Std::Bits::Count_Trailing_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
function Std::Bits::Count_Leading_Sign_Bits(implicit size : {1..}, x : Bits(size)) -> {1..size-1}
For an all-zero input, Count_Leading_Zero_Bits and Count_Trailing_Zero_Bits return
size. For an all-ones input, Count_Set_Bits returns size. size is not in
{0..size-1}.
Instructions in the specification produce these results:
- LZCNT.xml line 136:
let count := Count_Leading_Zero_Bits(src); - TZCNT.xml line 136:
let result := Count_Trailing_Zero_Bits(src)[0 +: operand_size]; - POPCNT.xml line 138:
let count := Count_Set_Bits(src);
Manual reference
- Intel SDM, 325462-093, Vol. 2A, LZCNT (PDF page 1282): "when the source operand
is zero, LZCNT outputs the operand size to the destination operand". - Intel SDM, 325462-093, Vol. 2B, TZCNT (PDF page 2020): "when the source operand
is zero, TZCNT outputs the operand size to the destination operand". - Intel SDM, 325462-093, Vol. 2B, POPCNT (PDF page 1694): POPCNT "calculates the
number of bits set to 1 in the second operand", which is the operand size when every bit
is set.
The SDM library pages for Count_Set_Bits, Count_Leading_Zero_Bits and
Count_Trailing_Zero_Bits already declare {0..size}.
Count_Leading_Sign_Bits, which no instruction calls, has the same problem: the manual's
examples (0b0011_0000 gives 2, 0b1110_0000 gives 3) count the top bit, so an all-zero
input gives size.
Proposed fix
-function Std::Bits::Count_Set_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
+function Std::Bits::Count_Set_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size}
-function Std::Bits::Count_Leading_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
+function Std::Bits::Count_Leading_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size}
-function Std::Bits::Count_Trailing_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size-1}
+function Std::Bits::Count_Trailing_Zero_Bits(implicit size : {0..}, x : Bits(size)) -> {0..size}
-function Std::Bits::Count_Leading_Sign_Bits(implicit size : {1..}, x : Bits(size)) -> {1..size-1}
+function Std::Bits::Count_Leading_Sign_Bits(implicit size : {1..}, x : Bits(size)) -> {1..size}On the Count_Leading_Sign_Bits library page, change the result type from {0..size}
to {1..size}, since its examples (0b0010_0110 gives 2) also count the top bit.
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