Skip to content

Instantly share code, notes, and snippets.

@JeremyGrosser
Created May 4, 2022 02:08
Show Gist options
  • Save JeremyGrosser/acba8ef7c5493670a2b7cd4a4fc54c61 to your computer and use it in GitHub Desktop.
Save JeremyGrosser/acba8ef7c5493670a2b7cd4a4fc54c61 to your computer and use it in GitHub Desktop.
with Ada.Text_IO; use Ada.Text_IO;
procedure Test is
type UInt16 is mod 2 ** 16
with Size => 16;
type UInt32 is mod 2 ** 32
with Size => 32;
type Words is array (Positive range <>) of UInt16
with Component_Size => 16;
function Checksum (Data : Words)
return UInt16
is
Sum : UInt32 := 0;
begin
for D of Data loop
Sum := Sum + UInt32 (D);
if Sum > 16#FFFF# then
Sum := Sum - 16#FFFF#;
end if;
end loop;
return UInt16 (Sum);
end Checksum;
function Checksum (Data : String)
return UInt16
is
D : Words (1 .. Data'Length / 2)
with Address => Data'Address;
begin
return Checksum (D);
end Checksum;
Sum : UInt16;
begin
Sum := Checksum ("Test");
Put_Line (Sum'Image);
end Test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment