From 668e82b70bca7682f0859cd165d028095f8e4cd4 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 27 Oct 2021 08:30:51 -0700 Subject: [PATCH] Update function-bit32-clz-ctz.md Rename to countlz/countrz --- rfcs/function-bit32-clz-ctz.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/rfcs/function-bit32-clz-ctz.md b/rfcs/function-bit32-clz-ctz.md index f76da9ea..93f96ac2 100644 --- a/rfcs/function-bit32-clz-ctz.md +++ b/rfcs/function-bit32-clz-ctz.md @@ -1,8 +1,8 @@ -# bit32.clz/ctz +# bit32.countlz/countrz ## Summary -Add bit32.clz (count leading zeroes) and bit32.ctz (count trailing zeroes) to accelerate bit scanning +Add bit32.countlz (count left zeroes) and bit32.countrz (count right zeroes) to accelerate bit scanning ## Motivation @@ -12,28 +12,27 @@ All CPUs have instructions to determine the position of first/last set bit in an - Scanning set bits in an integer, which allows efficient traversal of compact representation of bitmaps - Allocating bits out of a bitmap quickly -Today it's possible to approximate `clz` using `floor` and `log` but this approximation is relatively slow; approximating `ctz` is difficult without iterating through each bit. +Today it's possible to approximate `countlz` using `floor` and `log` but this approximation is relatively slow; approximating `ccountrz` is difficult without iterating through each bit. ## Design -`bit32` library will gain two new functions, `clz` and `ctz`: +`bit32` library will gain two new functions, `countlz` and `countrz`: ``` -function bit32.clz(n: number): number -function bit32.ctz(n: number): number +function bit32.countlz(n: number): number +function bit32.countrz(n: number): number ``` -`clz` takes an integer number (converting the input number to a 32-bit unsigned integer as all other `bit32` functions do), and returns the number of leading zero bits - that is, -the number of most significant zero bits in a 32-bit number until the first 1. The result is in `[0, 32]` range. +`countlz` takes an integer number (converting the input number to a 32-bit unsigned integer as all other `bit32` functions do), and returns the number of consecutive left-most zero bits - that is, the number of most significant zero bits in a 32-bit number until the first 1. The result is in `[0, 32]` range. For example, when the input number is `0`, it's `32`. When the input number is `2^k`, the result is `31-k`. -`ctz` takes an integer number (converting the input number to a 32-bit unsigned integer as all other `bit32` functions do), and returns the number of trailing zero bits - that is, +`countrz` takes an integer number (converting the input number to a 32-bit unsigned integer as all other `bit32` functions do), and returns the number of consecutive right-most zero bits - that is, the number of least significant zero bits in a 32-bit number until the first 1. The result is in `[0, 32]` range. For example, when the input number is `0`, it's `32`. When the input number is `2^k`, the result is `k`. -> Non-normative: a proof of concept implementation shows that a polyfill for `clz` takes ~34 ns per loop iteration when computing `clz` for an increasing number sequence, whereas +> Non-normative: a proof of concept implementation shows that a polyfill for `countlz` takes ~34 ns per loop iteration when computing `countlz` for an increasing number sequence, whereas > a builtin implementation takes ~4 ns. ## Drawbacks @@ -46,4 +45,6 @@ These functions can be alternatively specified as "find the position of the most can be more immediately useful since the bit position is usually more important than the number of bits. However, the bit position is undefined when the input number is zero, returning a sentinel such as -1 seems non-idiomatic, and returning `nil` seems awkward for calling code. Counting functions don't have this problem. -Of the two functions, `clz` is vastly more useful than `ctz`; we could implement just `clz`, but having both is nice for symmetry. +An early version of this proposal suggested `clz`/`ctz` (leading/trailing) as names; however, using a full verb is more consistent with other operations like shift/rotate, and left/right may be easier to understand intuitively compared to leading/trailing. left/right are used by C++20. + +Of the two functions, `countlz` is vastly more useful than `countrz`; we could implement just `countlz`, but having both is nice for symmetry.