-
+ CA78F137EB08EDEB38BE7844D4A077A051FF6A63E8B4F294E42979C125FB3C7220EDF2DC184F788EE2872EDBA17D3C54DCC26E3F37545C6708AA1409E2F04B8C
ffa/libffa/fz_io.adb
(0 . 0)(1 . 108)
1532 ------------------------------------------------------------------------------
1533 ------------------------------------------------------------------------------
1534 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
1535 -- --
1536 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org ) --
1537 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
1538 -- --
1539 -- You do not have, nor can you ever acquire the right to use, copy or --
1540 -- distribute this software ; Should you use this software for any purpose, --
1541 -- or copy and distribute it to anyone or in any manner, you are breaking --
1542 -- the laws of whatever soi-disant jurisdiction, and you promise to --
1543 -- continue doing so for the indefinite future. In any case, please --
1544 -- always : read and understand any software ; verify any PGP signatures --
1545 -- that you use - for any purpose. --
1546 -- --
1547 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
1548 ------------------------------------------------------------------------------
1549 ------------------------------------------------------------------------------
1550
1551 with W_Pred; use W_Pred;
1552 with W_Shifts; use W_Shifts;
1553 with FZ_BitOp; use FZ_BitOp;
1554 with FZ_Shift; use FZ_Shift;
1555
1556
1557 package body FZ_IO is
1558
1559 -- Expand FZ N by nibble D, and determine whether this operation overflowed
1560 procedure FZ_Insert_Bottom_Nibble(N : in out FZ;
1561 D : in Nibble;
1562 Overflow : out WBool) is
1563
1564 -- The overflow, if any, from shifting N in-place leftward by 4 bits
1565 Shifted_N_Overflow : Word := 0;
1566
1567 begin
1568 -- Make room in N for one additional hex digit (i.e. multiply N by 16)
1569 FZ_ShiftLeft_O(N => N,
1570 ShiftedN => N,
1571 Count => 4,
1572 Overflow => Shifted_N_Overflow);
1573
1574 -- Place the new digit into the now-vacated four bits at the bottom of N.
1575 FZ_Or_W(N, D);
1576
1577 -- Record whether the above operation overflowed N:
1578 Overflow := W_NZeroP(Shifted_N_Overflow);
1579
1580 end FZ_Insert_Bottom_Nibble;
1581
1582
1583 -- Determine the number of ASCII characters required to represent N
1584 function FZ_ASCII_Length(N : in FZ) return Char_Count is
1585 begin
1586 return N'Length * Nibbleness;
1587 end FZ_ASCII_Length;
1588
1589
1590 -- Write an ASCII hex representation of N into existing string buffer S
1591 procedure FZ_To_Hex_String(N : in FZ; S : out String) is
1592
1593 -- Indices into the string S (note, String always indexes from 1)
1594 subtype SiRange is Natural range S'First .. S'Last;
1595
1596 -- Position of current character in S being written
1597 Si : SiRange; -- Walks from 1 to the string length of S
1598
1599 begin
1600
1601 -- Step through all indices of N, regardless of how it was indexed:
1602 for i in 0 .. Word_Index(N'Length - 1) loop
1603 declare
1604
1605 -- Index of current Word, walks from ~top~ Word of N to ~bottom~
1606 Wi : constant Word_Index := N'Last - i;
1607
1608 -- Currently-selected Word of N
1609 W : Word := N(Wi);
1610
1611 begin
1612
1613 -- For each nibble in the Word:
1614 for j in 1 .. Nibbleness loop
1615
1616 -- Current position in S that is to be written
1617 Si := (Natural(i) * Nibbleness) + j;
1618
1619 -- Rotate the top nibble of W into the bottom nibble.
1620 W := Rotate_Left(W, 4);
1621
1622 -- Write the ASCII representation of the bottom nibble.
1623 S(Si) := HexDigs(Natural(W and 16#F#));
1624
1625 end loop;
1626
1627 -- Barring cosmic ray, W will have rotated to its initial value
1628 pragma Assert(W = N(Wi));
1629
1630 end;
1631
1632 end loop;
1633
1634 -- Barring cosmic ray, the last char written was to the final pos in S,
1635 pragma Assert(Si = SiRange'Last); -- as S is mandatorily exactly-sized.
1636
1637 end FZ_To_Hex_String;
1638
1639 end FZ_IO;