Skip to content

[General]: The language manual's Count_* result ranges exclude the operand size that LZCNT, TZCNT and POPCNT return #100

Open

Description

@shuklaayush

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:

Manual reference

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions