comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: CRC in Ada?
Date: 1997/03/05
Date: 1997-03-05T00:00:00+00:00	[thread overview]
Message-ID: <01bc2962$13e77140$018c71a5@dhoossr.iquest.com> (raw)
In-Reply-To: 1997Mar2.220652@nova.wright.edu


This is a multi-part message in MIME format.

------=_NextPart_000_01BC292F.C954A260
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Here is an implementation of a 16-bit CRC in both C and Ada.  The C code is
perhaps ten years old, and is from an implementation of Xmodem.  The Ada
code was translated from that.

The test driver tests both the  C code (as an imported set of functions),
and the Ada code.

The form of the test driver is not suitable for performance testing, as the
CRC generation is swamped by the file reads.  I'd like to modify this in
the next couple of days so as to be able to compare the speeds of the two
implementations.  The test driver does serve to demonstrate the equivalence
of the two implementations, though.

The generating polynomial is one frequently used (x1021), but the value can
be changed as one likes.

-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

Dr. John B. Matthews <jmatthews@nova.wright.edu> wrote in article
<1997Mar2.220652@nova.wright.edu>...
> Hi! Can anyone point me to 16-bit CRC code in Ada? I've checked the
> PAL and several other archives without luck. Any help appreciated.
> 
> Thanks,
> 
> John
> ----------------------------------------------------------------
> John B. Matthews, M.D.
> jmatthews@nova.wright.edu; john_matthews@ccmail.dayton.saic.com
> "Whom the gods would destroy, they first invite to program in C"
> 
> 
------=_NextPart_000_01BC292F.C954A260
Content-Type: application/octet-stream; name="crc.c"
Content-Transfer-Encoding: 7bit
Content-Description: crc.c (C source code file)
Content-Disposition: attachment; filename="crc.c"

/* crc.c */

#include "crc.h"

#define POLY 0x1021

static unsigned short CRCtable[256];

/* calculate CRC table entry */

unsigned short CalcTable(data,genpoly,accum)
unsigned short data;
unsigned short genpoly;
unsigned short accum;
{static int i;
 data <<= 8;
 for(i=8;i>0;i--)
         {
          if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly;
          else accum <<= 1;
          data <<= 1;
         }
 return(accum);
}

/* initialize CRC table */

void InitCRC(void)
{int i;
 for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0);
}

/* compute updated CRC */

unsigned short UpdateCRC(crc,byte)
unsigned short crc;
unsigned char  byte;
{
 return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] );
}

------=_NextPart_000_01BC292F.C954A260
Content-Type: application/octet-stream; name="crc.h"
Content-Transfer-Encoding: 7bit
Content-Description: crc.h (C Header File)
Content-Disposition: attachment; filename="crc.h"

void InitCRC(void);
unsigned short UpdateCRC(unsigned short,unsigned char);

------=_NextPart_000_01BC292F.C954A260
Content-Type: application/octet-stream; name="crc_pkg.adb"
Content-Transfer-Encoding: 7bit
Content-Description: crc_pkg.adb (ObjectAda File)
Content-Disposition: attachment; filename="crc_pkg.adb"

package body CRC_pkg is
  type Lookup_Table_Type is
    array (Interfaces.Unsigned_8) of Interfaces.Unsigned_16;
  Lookup_Table : Lookup_Table_Type;
  procedure Update
      (The_CRC_Value : in out Interfaces.Unsigned_16;
       With_The_Byte : in     Interfaces.Unsigned_8) is
    use Interfaces;
  begin
    The_CRC_Value := Shift_Left 
	     (Value  => The_CRC_Value,
	      Amount => 8) xor Lookup_Table 
	(Interfaces.Unsigned_8 (Shift_Right
	 (Value  => The_CRC_Value,
	  Amount => 8)) xor With_The_Byte);
  end Update;

begin -- CRC package initialization
  declare
    function Lookup_Table_Entry
	(For_The_Index      : in Interfaces.Unsigned_8;
	 And_The_Polynomial : in Interfaces.Unsigned_16)
	 return Interfaces.Unsigned_16 is
      use Interfaces;
      Temporary_Data        : Interfaces.Unsigned_16 := Shift_Left
	  (Value  => Interfaces.Unsigned_16 (For_The_Index),
	   Amount => 8);
      Temporary_Accumulator : Interfaces.Unsigned_16 := 0; 
    begin
      for I in reverse 1 .. 8 loop
	if ((Temporary_Data xor Temporary_Accumulator) and 
	    16#8000#) > 0 then
	  Temporary_Accumulator := Shift_Left 
	      (Value => Temporary_Accumulator, 
	       Amount => 1) xor And_The_Polynomial;
	else
	  Temporary_Accumulator := Shift_Left
	      (Value  => Temporary_Accumulator,
	       Amount => 1);
	end if;
	Temporary_Data := Shift_Left
	    (Value  => Temporary_Data,
	     Amount => 1);
      end loop;
      return Temporary_Accumulator;
    end Lookup_Table_Entry;
  begin
    for I in Lookup_Table'Range loop
      Lookup_Table (I) := Lookup_Table_Entry 
	  (For_The_Index => I,
	   And_The_Polynomial => Polynomial);
    end loop;
  end;
end CRC_pkg;


------=_NextPart_000_01BC292F.C954A260
Content-Type: application/octet-stream; name="crc_pkg.ads"
Content-Transfer-Encoding: 7bit
Content-Description: crc_pkg.ads (ObjectAda File)
Content-Disposition: attachment; filename="crc_pkg.ads"

with Interfaces;

package CRC_pkg is

  use type Interfaces.Unsigned_8;
  use type Interfaces.Unsigned_16;
  Polynomial : constant Interfaces.Unsigned_16 := 16#1021#;

  procedure Update
      (The_CRC_Value : in out Interfaces.Unsigned_16;
       With_The_Byte : in     Interfaces.Unsigned_8);

end CRC_pkg;

------=_NextPart_000_01BC292F.C954A260
Content-Type: application/octet-stream; name="test_crc.adb"
Content-Transfer-Encoding: quoted-printable
Content-Description: test_crc.adb (ObjectAda File)
Content-Disposition: attachment; filename="test_crc.adb"

with Ada.Command_Line;
with Ada.Sequential_IO;
with Ada.Text_IO;
with CRC_pkg;
with Interfaces.C;
procedure Test_CRC is
  package C renames Interfaces.C;
  package Command_Line renames Ada.Command_Line;
  package Text_IO renames Ada.Text_IO;
  use type C.Unsigned_Char;
  use type C.Unsigned_Short;
  procedure Init_CRC;
  function Updated_CRC=20
      (Previous_CRC_Accumulation : C.Unsigned_Short;
       Current_Byte		 : C.Unsigned_char)
       return C.Unsigned_Short;
  pragma Import (C, Init_CRC, "InitCRC");
  pragma Import (C, Updated_CRC, "UpdateCRC");
  pragma Linker_Options ("crc.o");
  Current_CRC_Accumulation : C.Unsigned_Short;
  package Unsigned_Short_IO is new Ada.Text_IO.Modular_IO =
(C.Unsigned_Short);
  package Unsigned_Char_IO is new Ada.Sequential_IO (C.Unsigned_Char);
  The_File : Unsigned_Char_IO.File_Type;
  The_Unsigned_Char : C.Unsigned_Char;
begin
  Text_Io.Put_Line (Integer'Image (C.Unsigned_Char'Size));
  Text_Io.Put_Line ("16-bit CRCs (x1021) by file:");
  Text_Io.Put_Line ("(Using imported C subprograms)");
  Init_CRC;
  for F in 1 .. Command_Line.Argument_Count loop
    Text_IO.Put=20
	("File """ &
	 Command_Line.Argument (F));
    begin
      Unsigned_Char_IO.Open
	  (File =3D> The_File,
	   Name =3D> Command_Line.Argument (F),
	   Mode =3D> Unsigned_Char_IO.In_File);
    exception
       when Text_Io.Name_Error =3D>
	 Text_IO.Put_Line (""": -- NOT FOUND!");
    end;
    if Unsigned_Char_IO.Is_Open (The_File) then
      Current_CRC_Accumulation :=3D 0;
      while not Unsigned_Char_IO.End_Of_File (The_File) loop
	Unsigned_Char_IO.Read=20
	    (Item =3D> The_Unsigned_Char,
	     File =3D> The_File);
	Current_CRC_Accumulation :=3D Updated_CRC
	    (Previous_CRC_Accumulation =3D> Current_CRC_Accumulation,
	     Current_Byte              =3D> The_Unsigned_Char);
      end loop;
      Unsigned_Char_IO.Close (The_File);
      Text_IO.Put (""":");
      Unsigned_Short_IO.Put
	  (Item  =3D> Current_CRC_Accumulation,
	   Width =3D> 10,
	   Base  =3D> 16);
      Text_IO.New_Line;
    end if;
  end loop;
 =20
  Text_Io.Put_Line ("(Using Ada subprogram)");
  for F in 1 .. Command_Line.Argument_Count loop
    Text_IO.Put=20
	("File """ &
	 Command_Line.Argument (F));
    begin
      Unsigned_Char_IO.Open
	  (File =3D> The_File,
	   Name =3D> Command_Line.Argument (F),
	   Mode =3D> Unsigned_Char_IO.In_File);
    exception
       when Text_Io.Name_Error =3D>
	 Text_IO.Put_Line (""": -- NOT FOUND!");
    end;
    if Unsigned_Char_IO.Is_Open (The_File) then
      Current_CRC_Accumulation :=3D 0;
      while not Unsigned_Char_IO.End_Of_File (The_File) loop
	Unsigned_Char_IO.Read=20
	    (Item =3D> The_Unsigned_Char,
	     File =3D> The_File);
	CRC_Pkg.Update
	    (The_Crc_Value =3D> Interfaces.Unsigned_16 =
(Current_CRC_Accumulation),
	     With_The_Byte =3D> Interfaces.Unsigned_8 (The_Unsigned_Char));
      end loop;
      Unsigned_Char_IO.Close (The_File);
      Text_IO.Put (""":");
      Unsigned_Short_IO.Put
	  (Item  =3D> Current_CRC_Accumulation,
	   Width =3D> 10,
	   Base  =3D> 16);
      Text_IO.New_Line;
    end if;
  end loop;
 =20
end Test_CRC;

------=_NextPart_000_01BC292F.C954A260--





  parent reply	other threads:[~1997-03-05  0:00 UTC|newest]

Thread overview: 143+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-03-02  0:00 CRC in Ada? Dr. John B. Matthews
1997-03-03  0:00 ` David Brown
1997-03-03  0:00 ` Tom Moran
1997-03-04  0:00 ` Jon S Anthony
1997-03-05  0:00   ` Robert Dewar
1997-03-04  0:00 ` Stephen Garriga
1997-03-04  0:00   ` Robert Dewar
1997-03-05  0:00     ` Stephen Garriga
1997-03-15  0:00     ` Michael & Amy Hartsough
1997-03-16  0:00       ` Robert Dewar
1997-03-16  0:00         ` Michael & Amy Hartsough
1997-03-16  0:00           ` Robert Dewar
1997-03-18  0:00             ` Michael & Amy Hartsough
1997-03-19  0:00               ` Robert Dewar
1997-03-20  0:00                 ` Michael & Amy Hartsough
1997-03-22  0:00                   ` Robert Dewar
1997-03-24  0:00                     ` Sequential IO on OpenVMS (was: CRC in Ada?) Ken Garlington
1997-03-24  0:00                       ` Larry Kilgallen
1997-03-22  0:00                   ` CRC in Ada? Mark & Zurima McKinney
1997-03-22  0:00                     ` Robert Dewar
1997-03-21  0:00                 ` Tom Moran
1997-03-23  0:00                   ` Robert Dewar
1997-03-23  0:00                     ` Tom Moran
1997-03-24  0:00                       ` Portable Code (was: CRC in Ada?) Larry Kilgallen
1997-03-24  0:00                     ` CRC in Ada? Robert A Duff
1997-03-24  0:00                       ` Larry Kilgallen
1997-03-24  0:00                         ` Robert A Duff
1997-03-24  0:00                       ` Robert Dewar
1997-03-24  0:00                       ` Robert A Duff
1997-03-25  0:00                     ` Robert I. Eachus
1997-04-01  0:00                 ` David Emery
1997-03-04  0:00   ` Tom Moran
1997-03-04  0:00     ` Stephen Garriga
1997-03-04  0:00   ` Matthew Heaney
1997-03-07  0:00   ` John Apa
1997-03-04  0:00 ` David L Brown
1997-03-04  0:00   ` Robert Dewar
1997-03-05  0:00   ` Stephen Garriga
1997-03-05  0:00     ` Larry Kilgallen
1997-03-05  0:00       ` Robert A Duff
1997-03-05  0:00         ` Larry Kilgallen
1997-03-06  0:00           ` Fergus Henderson
1997-03-06  0:00             ` Robert Dewar
1997-03-06  0:00             ` Larry Kilgallen
1997-03-06  0:00               ` Robert A Duff
1997-03-07  0:00                 ` Larry Kilgallen
1997-03-07  0:00                   ` Robert A Duff
1997-03-07  0:00                   ` Tom Moran
     [not found]                     ` <1997Mar7.202252.1@eisner>
1997-03-08  0:00                       ` Robert Dewar
1997-03-09  0:00                         ` Geert Bosch
1997-03-11  0:00                           ` Robert Dewar
1997-03-12  0:00                             ` Mats Weber
1997-03-12  0:00                               ` Robert Dewar
1997-03-10  0:00                     ` Dr. John B. Matthews
1997-03-10  0:00                     ` Robert Dewar
1997-03-10  0:00                       ` Tom Moran
1997-03-12  0:00                         ` Robert Dewar
1997-03-10  0:00                   ` Jim Balter
1997-03-07  0:00                 ` Robert Dewar
1997-03-10  0:00               ` Jim Balter
1997-03-06  0:00           ` Robert A Duff
1997-03-06  0:00             ` Larry Kilgallen
1997-03-06  0:00             ` Robert Dewar
1997-03-06  0:00             ` Robert Dewar
1997-03-09  0:00               ` Dr. John B. Matthews
1997-03-06  0:00             ` Robert Dewar
1997-03-10  0:00           ` Tarjei Jensen
1997-03-10  0:00             ` Robert Dewar
1997-03-10  0:00               ` Graham Hughes
1997-03-11  0:00                 ` Robert Dewar
1997-03-11  0:00                   ` Graham Hughes
1997-03-12  0:00                     ` Robert Dewar
1997-03-10  0:00             ` Robert Dewar
1997-03-10  0:00           ` David Brown
1997-03-12  0:00             ` Robert Dewar
1997-03-11  0:00           ` Mark & Zurima McKinney
1997-03-12  0:00           ` Robert I. Eachus
1997-03-12  0:00           ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-13  0:00           ` Jon S Anthony
1997-03-13  0:00             ` Robert Dewar
1997-03-14  0:00               ` Jim Balter
1997-03-14  0:00           ` Jon S Anthony
1997-03-15  0:00             ` Dr. John B. Matthews
1997-03-15  0:00             ` Robert Dewar
1997-03-17  0:00           ` Jon S Anthony
1997-03-06  0:00       ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-07  0:00       ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-07  0:00         ` Robert Dewar
1997-03-08  0:00           ` Robert A Duff
1997-03-10  0:00           ` Jim Balter
1997-03-11  0:00             ` Robert Dewar
1997-03-11  0:00               ` Jim Balter
1997-03-11  0:00                 ` Robert Dewar
1997-03-12  0:00                   ` Jim Balter
1997-03-14  0:00                     ` Richard A. O'Keefe
1997-03-14  0:00                       ` Jim Balter
1997-03-06  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-06  0:00     ` Graham Hughes
1997-03-06  0:00       ` Robert Dewar
1997-03-06  0:00     ` Larry Kilgallen
1997-03-09  0:00       ` Robert Dewar
1997-03-07  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-07  0:00     ` Robert Dewar
1997-03-08  0:00       ` Fergus Henderson
1997-03-10  0:00       ` Jim Balter
1997-03-11  0:00         ` Robert Dewar
1997-03-11  0:00           ` Jim Balter
1997-03-12  0:00             ` Robert Dewar
1997-03-12  0:00               ` Jim Balter
1997-03-14  0:00                 ` Samuel Mize
1997-03-11  0:00           ` Jim Balter
1997-03-11  0:00             ` Robert Dewar
1997-03-12  0:00               ` Jim Balter
1997-03-07  0:00   ` David Brown
1997-03-07  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-10  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-10  0:00     ` Robert Dewar
1997-03-10  0:00       ` Jim Balter
1997-03-11  0:00         ` Robert Dewar
1997-03-11  0:00           ` Jim Balter
1997-03-11  0:00             ` Robert Dewar
1997-03-12  0:00               ` Jim Balter
1997-03-11  0:00       ` Fergus Henderson
1997-03-10  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-11  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-12  0:00   ` Jim Balter
1997-03-12  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-12  0:00   ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-03-05  0:00 ` David C. Hoos, Sr. [this message]
1997-03-05  0:00 ` Laurent Pautet
1997-03-06  0:00 ` Jon S Anthony
2013-11-14 17:39 ` david
2013-11-14 18:55   ` Adam Beneschan
2013-11-14 19:35   ` tmoran
2013-11-14 20:09   ` Dmitry A. Kazakov
2013-11-14 20:40   ` John B. Matthews
  -- strict thread matches above, loose matches on Subject: below --
1997-03-13  0:00 tmoran
1997-03-13  0:00 ` Robert Dewar
1997-03-14  0:00   ` Joakim Olsson
1997-03-14  0:00   ` Tom Moran
1997-03-15  0:00   ` Tom Moran
1997-03-14  0:00 ` Jon S Anthony
1997-03-14  0:00 ` Richard A. O'Keefe
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox