DEFINITION MODULE Arith64BE;

        (********************************************************)
        (*                                                      *)
        (*         64-bit arithmetic, big-endian version        *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            4 June 2024                     *)
        (*  Last edited:        4 June 2024                     *)
        (*  Status:             Working                         *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD32, INT32;
FROM Arith64 IMPORT CARD64LE;

(* Most of our 64-bit arithmetic works on numbers stored in         *)
(* little-endian format.  (See module Arith64.)  The present module *)
(* deals with the less common case where the high-order word has    *)
(* to come first.                                                   *)

TYPE
    CARD64BE =  RECORD
                    high, low: CARD32;
                END (*RECORD*);
    INT64BE =   RECORD
                    high: INT32;
                    low: CARD32;
                END (*RECORD*);

CONST
    Zero64BE = CARD64BE{0, 0};
    Max64BE = CARD64BE{MAX(CARDINAL), MAX(CARDINAL)};


PROCEDURE ToBE (val: CARD64LE): CARD64BE;

    (* Converts a CARD64LE value to a CARD64BE value. *)

PROCEDURE FromBE (val: CARD64BE): CARD64LE;

    (* Converts a CARD64BE value to a CARD64LE value. *)

PROCEDURE Div10BE (number: CARD64BE;  VAR (*OUT*) quotient: CARD64BE;
                                  VAR (*OUT*) remainder: CARDINAL);

    (* Divides number by 10, returns quotient and remainder. *)

END Arith64BE.

