raw
ffa_ch11_tuning_a...    1 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 2 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
ffa_ch11_tuning_a... 4 -- --
ffa_ch11_tuning_a... 5 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org ) --
ffa_ch11_tuning_a... 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
ffa_ch11_tuning_a... 7 -- --
ffa_ch11_tuning_a... 8 -- You do not have, nor can you ever acquire the right to use, copy or --
ffa_ch11_tuning_a... 9 -- distribute this software ; Should you use this software for any purpose, --
ffa_ch11_tuning_a... 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
ffa_ch11_tuning_a... 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
ffa_ch11_tuning_a... 12 -- continue doing so for the indefinite future. In any case, please --
ffa_ch11_tuning_a... 13 -- always : read and understand any software ; verify any PGP signatures --
ffa_ch11_tuning_a... 14 -- that you use - for any purpose. --
ffa_ch11_tuning_a... 15 -- --
ffa_ch11_tuning_a... 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
ffa_ch11_tuning_a... 17 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 18 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 19
ffa_ch11_tuning_a... 20 with W_Pred; use W_Pred;
ffa_ch11_tuning_a... 21 with W_Shifts; use W_Shifts;
ffa_ch11_tuning_a... 22 with FZ_BitOp; use FZ_BitOp;
ffa_ch11_tuning_a... 23 with FZ_Shift; use FZ_Shift;
ffa_ch11_tuning_a... 24
ffa_ch11_tuning_a... 25
ffa_ch11_tuning_a... 26 package body FZ_IO is
ffa_ch11_tuning_a... 27
ffa_ch11_tuning_a... 28 -- Expand FZ N by nibble D, and determine whether this operation overflowed
ffa_ch11_tuning_a... 29 procedure FZ_Insert_Bottom_Nibble(N : in out FZ;
ffa_ch11_tuning_a... 30 D : in Nibble;
ffa_ch11_tuning_a... 31 Overflow : out WBool) is
ffa_ch11_tuning_a... 32
ffa_ch11_tuning_a... 33 -- The overflow, if any, from shifting N in-place leftward by 4 bits
ffa_ch11_tuning_a... 34 Shifted_N_Overflow : Word := 0;
ffa_ch11_tuning_a... 35
ffa_ch11_tuning_a... 36 begin
ffa_ch11_tuning_a... 37 -- Make room in N for one additional hex digit (i.e. multiply N by 16)
ffa_ch11_tuning_a... 38 FZ_ShiftLeft_O(N => N,
ffa_ch11_tuning_a... 39 ShiftedN => N,
ffa_ch11_tuning_a... 40 Count => 4,
ffa_ch11_tuning_a... 41 Overflow => Shifted_N_Overflow);
ffa_ch11_tuning_a... 42
ffa_ch11_tuning_a... 43 -- Place the new digit into the now-vacated four bits at the bottom of N.
ffa_ch11_tuning_a... 44 FZ_Or_W(N, D);
ffa_ch11_tuning_a... 45
ffa_ch11_tuning_a... 46 -- Record whether the above operation overflowed N:
ffa_ch11_tuning_a... 47 Overflow := W_NZeroP(Shifted_N_Overflow);
ffa_ch11_tuning_a... 48
ffa_ch11_tuning_a... 49 end FZ_Insert_Bottom_Nibble;
ffa_ch11_tuning_a... 50
ffa_ch11_tuning_a... 51
ffa_ch11_tuning_a... 52 -- Determine the number of ASCII characters required to represent N
ffa_ch11_tuning_a... 53 function FZ_ASCII_Length(N : in FZ) return Char_Count is
ffa_ch11_tuning_a... 54 begin
ffa_ch11_tuning_a... 55 return N'Length * Nibbleness;
ffa_ch11_tuning_a... 56 end FZ_ASCII_Length;
ffa_ch11_tuning_a... 57
ffa_ch11_tuning_a... 58
ffa_ch11_tuning_a... 59 -- Write an ASCII hex representation of N into existing string buffer S
ffa_ch11_tuning_a... 60 procedure FZ_To_Hex_String(N : in FZ; S : out String) is
ffa_ch11_tuning_a... 61
ffa_ch11_tuning_a... 62 -- Indices into the string S (note, String always indexes from 1)
ffa_ch11_tuning_a... 63 subtype SiRange is Natural range S'First .. S'Last;
ffa_ch11_tuning_a... 64
ffa_ch11_tuning_a... 65 -- Position of current character in S being written
ffa_ch11_tuning_a... 66 Si : SiRange; -- Walks from 1 to the string length of S
ffa_ch11_tuning_a... 67
ffa_ch11_tuning_a... 68 begin
ffa_ch11_tuning_a... 69
ffa_ch11_tuning_a... 70 -- Step through all indices of N, regardless of how it was indexed:
ffa_ch11_tuning_a... 71 for i in 0 .. Word_Index(N'Length - 1) loop
ffa_ch11_tuning_a... 72 declare
ffa_ch11_tuning_a... 73
ffa_ch11_tuning_a... 74 -- Index of current Word, walks from ~top~ Word of N to ~bottom~
ffa_ch11_tuning_a... 75 Wi : constant Word_Index := N'Last - i;
ffa_ch11_tuning_a... 76
ffa_ch11_tuning_a... 77 -- Currently-selected Word of N
ffa_ch11_tuning_a... 78 W : Word := N(Wi);
ffa_ch11_tuning_a... 79
ffa_ch11_tuning_a... 80 begin
ffa_ch11_tuning_a... 81
ffa_ch11_tuning_a... 82 -- For each nibble in the Word:
ffa_ch11_tuning_a... 83 for j in 1 .. Nibbleness loop
ffa_ch11_tuning_a... 84
ffa_ch11_tuning_a... 85 -- Current position in S that is to be written
ffa_ch11_tuning_a... 86 Si := (Natural(i) * Nibbleness) + j;
ffa_ch11_tuning_a... 87
ffa_ch11_tuning_a... 88 -- Rotate the top nibble of W into the bottom nibble.
ffa_ch11_tuning_a... 89 W := Rotate_Left(W, 4);
ffa_ch11_tuning_a... 90
ffa_ch11_tuning_a... 91 -- Write the ASCII representation of the bottom nibble.
ffa_ch11_tuning_a... 92 S(Si) := HexDigs(Natural(W and 16#F#));
ffa_ch11_tuning_a... 93
ffa_ch11_tuning_a... 94 end loop;
ffa_ch11_tuning_a... 95
ffa_ch11_tuning_a... 96 -- Barring cosmic ray, W will have rotated to its initial value
ffa_ch11_tuning_a... 97 pragma Assert(W = N(Wi));
ffa_ch11_tuning_a... 98
ffa_ch11_tuning_a... 99 end;
ffa_ch11_tuning_a... 100
ffa_ch11_tuning_a... 101 end loop;
ffa_ch11_tuning_a... 102
ffa_ch11_tuning_a... 103 -- Barring cosmic ray, the last char written was to the final pos in S,
ffa_ch11_tuning_a... 104 pragma Assert(Si = SiRange'Last); -- as S is mandatorily exactly-sized.
ffa_ch11_tuning_a... 105
ffa_ch11_tuning_a... 106 end FZ_To_Hex_String;
ffa_ch11_tuning_a... 107
ffa_ch11_tuning_a... 108 end FZ_IO;