comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: compile time Least Common Multiple, was Re: Ada and Internet stuff
Date: Fri, 31 Aug 2001 02:53:45 GMT
Date: 2001-08-31T02:53:45+00:00	[thread overview]
Message-ID: <Z_Cj7.33172$sa.16763963@news1.rdc1.sfba.home.com> (raw)
In-Reply-To: oDlj7.30207$sa.15327442@news1.rdc1.sfba.home.com

This code may be so bizarre as to be whimsical, but what the heck.

Problem: Given that Stream_Element'size is not necessarily 8 bits,
but can be assumed to be no greater than 128 bits, declare a
Stream_Element_Array subtype that is the shortest possible, but
a multiple of 24 bits.

-- Finding the GCD of 24 and any number from 1 .. 128 takes at most 5
-- iterations.

Larger1 : constant
  := boolean'pos(24 > Stream_Element'size) * 24
     + boolean'pos(24 <= Stream_Element'size) * Stream_Element'size;
Smaller1 : constant
  := boolean'pos(24 > Stream_Element'size) * Stream_Element'size
     + boolean'pos(24 <= Stream_Element'size) * 24;
R1 : constant := Larger1 mod Smaller1;
Is_Zero1 : constant := boolean'pos(R1 = 0);
GCD1 : constant := Is_Zero1 * Smaller1;

-- Once R(n) = 0, then Is_Zero(n) = 1 and Smaller(n+1) = 0
-- so R(n+1) = Larger(n+1) mod 1 = 0.  ie all following R(n+1 ...) = 0
-- and exactly one of Is_Zero(i) is non-zero

Larger2 : constant := Smaller1;
Smaller2 : constant := R1;
R2 : constant := Larger2 mod (Smaller2 + Is_Zero1);
Is_Zero2 : constant := boolean'pos(R2 = 0);
GCD2 : constant := Is_Zero2 * Smaller2;

Larger3 : constant := Smaller2;
Smaller3 : constant := R2;
R3 : constant := Larger3 mod (Smaller3 + Is_Zero2);
Is_Zero3 : constant := boolean'pos(R3 = 0);
GCD3 : constant := Is_Zero3 * Smaller3;

Larger4 : constant := Smaller3;
Smaller4 : constant := R3;
R4 : constant := Larger4 mod (Smaller4 + Is_Zero3);
Is_Zero4 : constant := boolean'pos(R4 = 0);
GCD4 : constant := Is_Zero4 * Smaller4;

Larger5 : constant := Smaller4;
Smaller5 : constant := R4;
R5 : constant := Larger5 mod (Smaller5 + Is_Zero4);
Is_Zero5 : constant := boolean'pos(R5 = 0);
GCD5 : constant := Is_Zero5 * Smaller5;

GCD : constant := GCD1+GCD2+GCD3+GCD4+GCD5;
-- will give the Greatest Common Divisor.

-- LCM is the least common multiple of 24 bits and Stream_Element'size
LCM : constant := 24*Stream_Element'size/GCD;

subtype To_Encode is Stream_Element_Array(1 .. LCM/Stream_Element'size);
-- To_Encode'size = LCM



  parent reply	other threads:[~2001-08-31  2:53 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-24 19:22 Ada and Internet stuff Darren New
2001-08-24 19:39 ` Ted Dennison
2001-08-24 20:30   ` Pascal Obry
2001-08-24 22:03     ` Ehud Lamm
2001-08-26 15:52       ` David Botton
2001-08-26 20:32         ` Pascal Obry
2001-08-27  1:40           ` David Botton
2001-08-27  6:31             ` Ehud Lamm
2001-08-24 20:32 ` Pascal Obry
2001-08-24 23:36   ` Darren New
2001-08-24 20:39 ` Larry Kilgallen
2001-08-24 22:08 ` Marc A. Criley
2001-08-25  7:07   ` Pascal Obry
2001-08-27  1:49 ` tmoran
2001-08-27 11:04   ` Tarjei T. Jensen
2001-08-27 11:28     ` Ehud Lamm
2001-08-27 20:09       ` Pascal Obry
2001-08-27 23:10   ` David Botton
2001-08-29  5:52     ` tmoran
2001-08-29 16:28       ` Darren New
2001-08-29 17:12         ` David Botton
2001-08-29 17:32         ` Ted Dennison
2001-08-29 18:51         ` tmoran
2001-08-29 19:00           ` Darren New
2001-08-29 19:53             ` tmoran
2001-08-29 20:03               ` Darren New
2001-08-29 20:08           ` Ted Dennison
2001-08-30  7:08             ` tmoran
2001-08-31  2:39               ` tmoran
2001-08-31 13:34                 ` Ted Dennison
2001-08-31 13:46                   ` Marin David Condic
2001-08-31 17:37                     ` tmoran
2001-08-31 17:59                       ` Marin David Condic
2001-09-01 17:56                         ` tmoran
2001-08-31 13:34                 ` Marin David Condic
2001-08-31 17:52                   ` Ted Dennison
2001-08-31 18:09                     ` Marin David Condic
2001-08-31  2:53               ` tmoran [this message]
2001-08-31 13:43                 ` compile time Least Common Multiple, was " Ted Dennison
2001-08-31 17:37                   ` tmoran
2001-08-31 18:08                     ` Ted Dennison
replies disabled

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