comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: 64-bit unsigned integer?
Date: Sun, 25 Feb 2018 14:23:17 +0100
Date: 2018-02-25T14:23:17+01:00	[thread overview]
Message-ID: <p6udc8$ub2$1@gioia.aioe.org> (raw)
In-Reply-To: 63f986fd-662a-47e7-adf9-5fddc243ac45@googlegroups.com

On 2018-02-25 13:54, MM wrote:
> On Sunday, 25 February 2018 12:42:00 UTC, Dmitry Kazakov  wrote:
>> On 2018-02-25 13:30, MM wrote:
>>> type u64 is range 0 .. 2**64-1; -- this fails with "integer type definition bounds out of range".
>>
>> This is the right method alas not supported by the architecture of the
>> machine you have.
> 
> Damn. Its a 64-bit CPU; I would have thought that a Carry Or Overflow
> bit in the processor would have done the trick?
> 
>>> Is there a way to do it so can get an unsigned integer that will raise an
>>> exception if it overflows?
>>
>> You must implement it yourself, e.g. on top of a modular or integer
>> type, or use an arbitrary length integer arithmetic package. There exist
>> a few in Ada.
> 
> I don't want to go the BigNum route - too heavyweight. Implementing it
> efficiently myself may require access to the processor's condition code
> registers, so this feels like an assembly language approach?

Why, it is straightforward because you have no negatives. Something like 
this:

    package Unsigneds_64 is
       type u64 is private;
       function "+" (Left, Right : u64) return u64;
       function "-" (Left, Right : u64) return u64;
       function "*" (Left, Right : u64) return u64;
       function "/" (Left, Right : u64) return u64;
    private
       use Interfaces;
       type u64 is new Unsigned_64;
    end Unsigneds_64;

    package body Unsigneds_64 is
       function "+" (Left, Right : u64) return u64 is
          Result : constant u64 :=
                   u64 (Unsigned_64 (Left) + Unsigned_64 (Right));
       begin
          if Result - Left /= Right then
             raise Constraint_Error;
          else
             return Result;
          end if;
       end "+";

       function "-" (Left, Right : u64) return u64 is
       begin
          if Left < Right then
             raise Constraint_Error;
          else
             return u64 (Unsigned_64 (Left) - Unsigned_64 (Right));
          end if;
       end "-";

       function "*" (Left, Right : u64) return u64 is
       begin
          if Left = 0 then
             return 0;
          else
             declare
                Result : constant u64 :=
                         u64 (Unsigned_64 (Left) * Unsigned_64 (Right));
             begin
                if Result / Left /= Right then
                   raise Constraint_Error;
                else
                   return Result;
                end if;
             end;
          end if;
       end "*";

       function "/" (Left, Right : u64) return u64 is
       begin
          return u64 (Unsigned_64 (Left) / Unsigned_64 (Right));
       end "/";

    end Unsigneds_64;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2018-02-25 13:23 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-25 12:30 64-bit unsigned integer? MM
2018-02-25 12:41 ` Dmitry A. Kazakov
2018-02-25 12:54   ` MM
2018-02-25 13:23     ` Dmitry A. Kazakov [this message]
2018-02-25 13:59       ` MM
2018-02-25 14:20         ` Dmitry A. Kazakov
2018-02-25 15:34           ` MM
2018-02-25 16:35     ` Jeffrey R. Carter
2018-02-25 16:36 ` Anh Vo
2018-02-25 17:31   ` MM
2018-02-26 23:19     ` Randy Brukardt
2018-02-26 23:33       ` MM
2018-02-28 10:17       ` Paul Rubin
2018-02-28 10:39         ` J-P. Rosen
2018-02-28 10:59           ` Dmitry A. Kazakov
2018-02-28 23:20         ` Randy Brukardt
2018-03-01  5:47           ` Paul Rubin
2018-03-01  8:16             ` Niklas Holsti
2018-03-01  8:35               ` Simon Wright
2018-03-01  8:47               ` Dmitry A. Kazakov
2018-03-01 18:15                 ` Dan'l Miller
2018-03-01 19:10                   ` Dmitry A. Kazakov
2018-03-01 19:22                     ` Dan'l Miller
2018-03-01 19:59                       ` Dmitry A. Kazakov
2018-03-01 20:32                         ` Dan'l Miller
2018-03-01 21:15                           ` Dmitry A. Kazakov
2018-03-01 22:23                             ` Randy Brukardt
2018-03-01 23:50                             ` Robert Eachus
2018-03-01 20:06                     ` Niklas Holsti
2018-03-01 22:04               ` Randy Brukardt
2018-02-26  6:45 ` Robert Eachus
2018-02-27 16:40   ` Dan'l Miller
2018-02-27 17:18     ` J-P. Rosen
2018-02-27 18:17       ` Dan'l Miller
2018-02-28  3:03         ` Robert Eachus
2018-02-28  6:41         ` J-P. Rosen
replies disabled

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