Commit aae1113b authored by François Bobot's avatar François Bobot
Browse files

Fix bad history import

parent 0d13c6bd
(**************************************************************************)
(* *)
(* Copyright (C) 2007-2015 *)
(* CEA (Commissariat a l'energie atomique et aux energies *)
(* alternatives) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
(* Foundation, version 2.1. *)
(* *)
(* It is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU Lesser General Public License for more details. *)
(* *)
(* See the GNU Lesser General Public License version 2.1 *)
(* (enclosed file LGPLv2.1). *)
(* *)
(**************************************************************************)
(* -------------------------------------------------------------------------- *)
(* --- Constructors --- *)
(* -------------------------------------------------------------------------- *)
type t = Z.t * int
let mantissa = fst
let exponent = snd
let zero = Z.zero,0
let one = Z.one,0
let two = Z.one,1
let m8 = Z.of_int 255 (* mask for 8 1-bits 0b11111111 *)
let m4 = Z.of_int 15 (* mask for 4 1-bits 0b00001111 *)
let m2 = Z.of_int 3 (* mask for 2 1-bits 0b00000011 *)
(* 0 is uniquely represented by [zero] *)
let is_0mask a mask = Z.equal (Z.logand a mask) Z.zero
let oddify a p =
if Z.equal a Z.zero then zero else
let reduce_1 a p = (* 1 zero-bit or less *)
if is_0mask a Z.one
then (Z.shift_right_trunc a 1 , p+1 )
else (a,p) in
let reduce_2 a p = (* 3 zero-bits or less *)
if is_0mask a m2
then reduce_1 (Z.shift_right_trunc a 2) (p+2)
else reduce_1 a p in
let reduce_4 a p = (* 7 zero-bits or less *)
if is_0mask a m4
then reduce_2 (Z.shift_right_trunc a 4) (p+4)
else reduce_2 a p in
let rec reduce_8 a p = (* any number of zero-bits *)
if is_0mask a m8
then reduce_8 (Z.shift_right_trunc a 8) (p+8)
else reduce_4 a p
in reduce_8 a p
let equal (a,p) (b,q) = Z.equal a b && p = q
let compare (a,p) (b,q) =
if a==b then compare p q else
let sa = Z.sign a in
let sb = Z.sign b in
if sa < 0 && sb > 0 then (-1) else
if sa > 0 && sb < 0 then 1 else
if p > q then Z.compare (Z.shift_left a (p-q)) b else
if p < q then Z.compare a (Z.shift_left b (q-p)) else
Z.compare a b
let make = oddify
(* -------------------------------------------------------------------------- *)
(* --- Ring Operations --- *)
(* -------------------------------------------------------------------------- *)
let neg (a,p) = if Z.equal a Z.zero then zero else Z.neg a,p
let add u v =
if u == zero then v else
if v == zero then u else
let (a,p) = u in
let (b,q) = v in
(* shift is even, mantissa is odd,
result is necessary odd (and non-null) *)
if p > q then Z.add (Z.shift_left a (p-q)) b , q else
if p < q then Z.add (Z.shift_left b (q-p)) a , p else
oddify (Z.add a b) p
let sub u v =
if v == zero then u else
let (b,q) = v in
if u == zero then (Z.neg b,q) else
let (a,p) = u in
(* shift is even, mantissa is odd,
result is necessary odd (and non-null) *)
if p > q then Z.sub (Z.shift_left a (p-q)) b , q else
if p < q then Z.sub a (Z.shift_left b (q-p)) , p else
oddify (Z.sub a b) p
let mul u v =
if u == zero || v == zero then zero else
let (a,p) = u in
let (b,q) = v in
(* multiplying two odds returns an odd *)
Z.mul a b , p+q
let shift_left u n = if u == zero then zero else (fst u,snd u+n)
let shift_right u n = if u == zero then zero else (fst u,snd u-n)
let power2 n = (Z.one,n)
let cache = 128
let powers = Array.init cache (Z.shift_left Z.one)
let zp2 k = if k < cache then powers.(k) else Z.shift_left Z.one k
let b32 = 24
let b64 = 53
let m32 = powers.(b32)
let m64 = powers.(b64)
let rec log2_dicho a p q =
let r = (p + q) / 2 in
if r = p then q else
if Z.lt a (zp2 r)
then log2_dicho a p r
else log2_dicho a r q
let log2_mantissa a =
let n = Z.size a in
let p = (n-1) lsl 6 in (* x64 *)
let q = n lsl 6 in
log2_dicho a p q
let log2 (a,p) =
if Z.equal a Z.zero then 0 else
if Z.equal a Z.one then p else
p + log2_mantissa (Z.abs a)
(* -------------------------------------------------------------------------- *)
(* --- Rounding Mode --- *)
(* -------------------------------------------------------------------------- *)
let is_even z = is_0mask z Z.one
type mode =
| NE (* Nearest to even *)
| ZR (* Toward zero *)
| DN (* Toward minus infinity *)
| UP (* Toward plus infinity *)
| NA (* Nearest away from zero *)
type tie = Floor | Tie | Ceil (* positive tie-break *)
[@@ deriving show]
let sign s a = if s then a else Z.neg a
(* s:sign, a: absolute truncated *)
let choose mode tie s a =
match mode , tie with
| ZR , _ -> sign s a
| DN , _ -> if s then a else Z.neg (Z.succ a)
| UP , _ -> if s then Z.succ a else Z.neg a
| NA , Tie -> sign s (Z.succ a)
| NE , Tie -> sign s (if is_even a then a else Z.succ a)
| (NE|NA) , Floor -> sign s a
| (NE|NA) , Ceil -> sign s (Z.succ a)
(* requires 2^n <= a *)
let truncate n a p tie =
let d = log2_mantissa a - n in
let mask = Z.pred (zp2 d) in
let half = zp2 (d-1) in
let r = Z.logand a mask in
let tie =
if Z.lt r half then Floor else
if Z.lt half r then Ceil else tie in
let a0 = Z.shift_right_trunc a d in
a0 , p + d , tie
(* requires m = 2^n *)
let rounding mode n m ((a,p) as u) =
let a0 = Z.abs a in
(* a is odd, m is even *)
if Z.lt a0 m then u (* EXACT *)
else
let a0,p,tie = truncate n a0 p Tie in
let a = choose mode tie (0 < Z.sign a) a0 in
oddify a p
let round ?(mode=NE) ?(bits=80) u =
rounding mode bits (zp2 bits) u
let round_f32 ?(mode=NE) u = rounding mode b32 m32 u
let round_f64 ?(mode=NE) u = rounding mode b64 m64 u
let seizing n m ((a,p) as u) =
let a0 = Z.abs a in
(* a is odd, m is even *)
if Z.lt a0 m then u,u (* EXACT *)
else
let a0,p,_ = truncate n a0 p Tie in
if 0 < Z.sign a then
oddify a0 p , oddify (Z.succ a0) p
else
let a0 = Z.neg a0 in
oddify (Z.pred a0) p , oddify a0 p
let seize ?(bits=80) u = seizing bits (zp2 bits) u
let seize_f32 = seizing b32 m32
let seize_f64 = seizing b64 m64
(* -------------------------------------------------------------------------- *)
(* --- Z Conversion --- *)
(* -------------------------------------------------------------------------- *)
let of_zint a = oddify a 0
let to_zint (a,n) =
if n > 0 then Z.shift_left a n else
if n < 0 then Z.shift_right_trunc a n else a
let of_int a = oddify (Z.of_int a) 0
let to_int a = Z.to_int (to_zint a)
(* -------------------------------------------------------------------------- *)
(* --- Q conversion --- *)
(* -------------------------------------------------------------------------- *)
let to_qint (a,n) =
if n > 0 then Q.mul_2exp (Q.of_bigint a) n else
if n < 0 then Q.div_2exp (Q.of_bigint a) (-n) else
Q.of_bigint a
let q_two = Q.of_int 2
(* invariant q.2^n, ensures 1 <= q < 2 *)
let rec magnitude q n =
if Q.lt q Q.one then magnitude (Q.mul_2exp q 1) (pred n) else
if Q.geq q q_two then magnitude (Q.div_2exp q 1) (succ n) else
(q,n)
(* invariant m + q.2^n && 0 <= q < 1 *)
let rec decompose bits m q n =
if Q.equal q Q.zero || bits=0 then m,q else
let q = Q.mul_2exp q 1 in
let n = pred n in
let bits = pred bits in
if Q.lt q Q.one
then
decompose bits m q n
else
let m = add m (power2 n) in
let q = Q.sub q Q.one in
decompose bits m q n
exception Undefined
let half = Q.make Z.one (Z.of_int 2)
let of_qexp ~mode ~bits r n =
(* having one more bit for further rounding *)
let q,n = magnitude (Q.abs r) n in
(* have r = q.2^n /\ 1 <= q < 2 *)
let ((a,p) as m),e = decompose bits (power2 n) (Q.sub q Q.one) n in
(* returns (m,e) such that m + e = 2^n + (q-1).2^n = r, 0<=e<1 *)
let sign = 0 < Q.sign r in
let tie =
if Q.lt e half then Floor else
if Q.gt e half then Ceil else Tie in
let m = zp2 bits in
let a,p,tie =
if Z.lt a m then a,p,tie
else truncate bits a p tie in
oddify (choose mode tie sign a) p
let of_qint ?(mode=NE) ?(bits=80) r =
match Q.classify r with
| Q.ZERO -> zero
| Q.INF | Q.MINF | Q.UNDEF -> raise Undefined
| Q.NZERO -> of_qexp ~mode ~bits r 0
let div ?(mode=NE) ?(bits=80) a b =
if a == zero then zero else
if b == zero then raise Undefined else
if b = one then a else
let p,n = a in let q,m = b in
of_qexp ~mode ~bits (Q.make p q) (n-m)
(* -------------------------------------------------------------------------- *)
(* --- Float Conversion --- *)
(* -------------------------------------------------------------------------- *)
let f_sign = Int64.shift_left 1L 63
let f_unit = Int64.shift_left 1L 52
let f_mantissa = Int64.(sub f_unit 1L)
let f_exponent = Int64.(sub (shift_left 1L 11) 1L)
let of_float u =
match classify_float u with
| FP_zero -> zero
| FP_nan | FP_infinite -> raise Undefined
| FP_normal ->
let a = Int64.bits_of_float u in
let m = Z.of_int64 (Int64.(add f_unit (logand a f_mantissa))) in
let e = Int64.(sub (logand (shift_right_logical a 52) f_exponent) 1075L) in
let s = Int64.(logand a f_sign) <> 0L in
oddify (if s then Z.neg m else m) (Int64.to_int e)
| FP_subnormal ->
let a = Int64.bits_of_float u in
let m = Z.of_int64 (Int64.(logand a f_mantissa)) in
let s = Int64.(logand a f_sign) <> 0L in
oddify (if s then Z.neg m else m) (-1074)
let to_float ?(mode=NE) u =
if u == zero then 0.0 else
let (a,n) = rounding NE b64 m64 u in
Stdlib.ldexp (Z.to_float a) n
(* -------------------------------------------------------------------------- *)
(* --- String Conversion --- *)
(* -------------------------------------------------------------------------- *)
let pretty fmt (a,n) =
let s = Z.sign a in
if s = 0 then Format.pp_print_char fmt '0' else
begin
if s > 0 then Format.pp_print_char fmt '+' ;
Z.pp_print fmt a ;
if n <> 0 then
( Format.pp_print_char fmt 'p' ;
Format.pp_print_int fmt n )
end
let to_string (a,n) =
let s = Z.sign a in
if s = 0 then "0" else
let buffer = Buffer.create 80 in
if s > 0 then Buffer.add_char buffer '-' ;
Z.bprint buffer a ;
if n <> 0 then
( Buffer.add_char buffer 'p' ;
Buffer.add_string buffer (string_of_int n) ) ;
Buffer.contents buffer
let of_string s =
try
let k = String.index s 'p' in
let m = String.sub s 0 k in
let e = String.sub s (succ k) (String.length s - k - 1) in
oddify (Z.of_string m) (int_of_string e)
with Not_found ->
oddify (Z.of_string s) 0
(* -------------------------------------------------------------------------- *)
(* --- Deriving --- *)
(* -------------------------------------------------------------------------- *)
let pp = pretty
let show = to_string
(* -------------------------------------------------------------------------- *)
(**************************************************************************)
(* *)
(* Copyright (C) 2007-2015 *)
(* CEA (Commissariat a l'energie atomique et aux energies *)
(* alternatives) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
(* Foundation, version 2.1. *)
(* *)
(* It is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU Lesser General Public License for more details. *)
(* *)
(* See the GNU Lesser General Public License version 2.1 *)
(* (enclosed file LGPLv2.1). *)
(* *)
(**************************************************************************)
(** Float Arithmetics (based on [Zarith]) *)
(** Rational numbers [m.2^n] where [m] is a [Zarith] integer and [n] an OCaml integer. *)
type t
val zero : t
val one : t
val two : t
val make : Z.t -> int -> t
(** The number [p.2^n]. *)
val mantissa : t -> Z.t
val exponent : t -> int
val equal : t -> t -> bool
val compare : t -> t -> int
exception Undefined (** for undefined operations *)
(** {3 Rounding} *)
(** Supported rounding modes *)
type mode =
| NE (** Nearest to even *)
| ZR (** Toward zero *)
| DN (** Toward minus infinity *)
| UP (** Toward plus infinity *)
| NA (** Nearest away from zero *)
val b32 : int (** mantissa of IEEE-32 [~bits:b32=24] *)
val b64 : int (** mantissa of IEEE-64 [~bits:b64=53] *)
val round : ?mode:mode -> ?bits:int -> t -> t
(** Rounded to [bits] binary significant digits, twoard zero (defaults to 80).
Default rounding mode is [NE]. *)
val round_f32 : ?mode:mode -> t -> t
(** Round to an IEEE float. Same as [round ~bits:28]. *)
val round_f64 : ?mode:mode -> t -> t
(** Round to an IEEE double. Same as [round ~bits:54]. *)
val seize : ?bits:int -> t -> t*t
(** Returns the two consecutive floats with [bits] precision around the given float. *)
(** {3 Arithmetics} *)
val neg : t -> t
val add : t -> t -> t
val sub : t -> t -> t
val mul : t -> t -> t
val div : ?mode:mode -> ?bits:int -> t -> t -> t
(** Division rounded to [bits] digits (default is 80).
The default rounding mode is [NE].
@raise Undefined for division by zero.
*)
val shift_left : t -> int -> t (** Multiply with a positive or negative power of 2. *)
val shift_right : t -> int -> t (** Divide by a positive or negative power of 2. *)
val power2 : int -> t (** Return the positive or negative power of 2. *)
val log2 : t -> int (** Return the smallest upper bound in power of 2. *)
(** {3 Conversion with Z} *)
val of_zint : Z.t -> t (** Exact. *)
val to_zint : t -> Z.t (** Rounded towards 0. *)
(** {3 Conversion with Q} *)
val of_qint : ?mode:mode -> ?bits:int -> Q.t -> t
(** Rounded to [bits] binary digits (default 80). *)
val to_qint : t -> Q.t (** Exact. *)
(** {3 Conversion with OCaml integers} *)
val of_int : int -> t (** Exact. *)
val to_int : t -> int (** Fractional part is truncated. *)
(** {3 Conversion with OCaml floats} *)
val of_float : float -> t (** Exact. @raise Undefined for NaN and infinites. *)
val to_float : ?mode:mode -> t -> float (** Rounded with default mode [NE]. *)
(** {3 Formatting}
Format is [<m>[p<e>]] where [<m>] is a signed decimal integer
and [p<e>] an optional exponent in power of 2.
For instance [to_string (of_string "24p-1")] is ["3p2"].
*)
val of_string : string -> t
val to_string : t -> string
val pretty : Format.formatter -> t -> unit
val pp : Format.formatter -> t -> unit (** Alias for pretty (deriving) *)
val show : t -> string (** Alias for to_string (deriving) *)
This diff is collapsed.
This diff is collapsed.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.