-
+ 5235054F1A2FFC6F2560122D8623A3ACDD486B15F85A930CB6099622008571EC59B56A423AF2D061FC39931BF624B231425212D28BBD06E9FAEA148EBE3A5148
ffa/libffa/fz_measr.adb
(0 . 0)(1 . 68)
392 ------------------------------------------------------------------------------
393 ------------------------------------------------------------------------------
394 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
395 -- --
396 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org ) --
397 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
398 -- --
399 -- You do not have, nor can you ever acquire the right to use, copy or --
400 -- distribute this software ; Should you use this software for any purpose, --
401 -- or copy and distribute it to anyone or in any manner, you are breaking --
402 -- the laws of whatever soi-disant jurisdiction, and you promise to --
403 -- continue doing so for the indefinite future. In any case, please --
404 -- always : read and understand any software ; verify any PGP signatures --
405 -- that you use - for any purpose. --
406 -- --
407 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
408 ------------------------------------------------------------------------------
409 ------------------------------------------------------------------------------
410
411 with Word_Ops; use Word_Ops;
412 with W_Pred; use W_Pred;
413 with W_Shifts; use W_Shifts;
414
415
416 package body FZ_Measr is
417
418 -- Find the index of eldest nonzero bit ( 0 if none, or 1 .. FZBitness )
419 function FZ_Measure(N : in FZ) return Word is
420
421 -- The result (default : 0, will remain 0 if N is in fact zero)
422 Index : Word := 0;
423
424 -- The currently-examined Word, starting from the junior-most
425 Ni : Word;
426
427 -- The most recently-seen nonzero Word, if indeed any exist
428 W : Word := 0;
429
430 -- 1 if currently-examined Word is zero, otherwise 0
431 NiZ : WBool;
432
433 begin
434
435 -- Find, in constant time, eldest non-zero Word:
436 for i in 0 .. Indices(N'Length - 1) loop
437 Ni := N(N'First + i); -- Ni := current Word;
438 NiZ := W_ZeroP(Ni); -- ... is this Word = 0?
439 Index := W_Mux(Word(i), Index, NiZ); -- If NO, save the Index,
440 W := W_Mux(Ni, W, NiZ); -- ... and save that Word.
441 end loop;
442
443 -- Set Index to be the bit-position of the eldest non-zero Word:
444 Index := Shift_Left(Index, BitnessLog2); -- Index := Index * Bitness
445
446 -- Find, in constant time, eldest non-zero bit in that Word:
447 for b in 1 .. Bitness loop
448 -- If W is non-zero, advance the Index...
449 Index := W_Mux(Index + 1, Index, W_ZeroP(W));
450 -- ... in either case, advance W:
451 W := Shift_Right(W, 1);
452 end loop;
453
454 -- If N = 0, result will be 0; otherwise: index of the eldest 1 bit.
455 return Index;
456
457 end FZ_Measure;
458
459 end FZ_Measr;