-- Find the highest power of 2 which divides N. ( or 0 if N is zero. ) function FZ_Count_Bottom_Zeros(N : in FZ) return FZBit_Index is -- The result (default : 0, will remain 0 if N is in fact zero) Index : Word := 0; -- The currently-examined Word, starting from the senior-most Ni : Word; -- The most recently-seen nonzero Word, if indeed any exist W : Word := 0; -- 1 if currently-examined Word is zero, otherwise 0 NiZ : WBool; begin -- Find, in constant time, youngest non-zero Word: for i in reverse 0 .. Indices(N'Length - 1) loop Ni := N(N'First + i); -- Ni := current Word; NiZ := W_ZeroP(Ni); -- ... is this Word = 0? Index := W_Mux(Word(i), Index, NiZ); -- If NO, save its Index, W := W_Mux(Ni, W, NiZ); -- ... and save the Word. end loop; -- Set Index to be the bit-position of the youngest non-zero Word: Index := Shift_Left(Index, BitnessLog2); -- Index := Index * Bitness -- Handle degenerate case: if W is equal to 0, Index is not changed; -- Otherwise, we start by supposing that the top bit of W were set: Index := Index + ((0 - W_NZeroP(W)) and Word(Bitness)); -- Now crank back the Index by the number of high bits of W (i.e. -- starting from its top) that must be discarded for W to become zero: for b in 1 .. Bitness loop -- If W is non-zero at this time, decrement the Index: Index := Index - W_NZeroP(W); -- ... in either case, advance W, i.e. discard the top bit of it: W := Shift_Left(W, 1); end loop; -- If N = 0, result will be 0; otherwise: number of bottom zeros in N. return FZBit_Index(Index); end FZ_Count_Bottom_Zeros;