| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . D O U B L E _ R E A L . P R O D U C T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2021-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This is the x86/FreeBSD version of the separate package body |
| 33 |
|
| 34 |
with Interfaces; use Interfaces; |
| 35 |
|
| 36 |
separate (System.Double_Real) |
| 37 |
|
| 38 |
package body Product is |
| 39 |
|
| 40 |
procedure Split (N : Num; Hi : out Num; Lo : out Num); |
| 41 |
-- Compute high part and low part of N |
| 42 |
|
| 43 |
----------- |
| 44 |
-- Split -- |
| 45 |
----------- |
| 46 |
|
| 47 |
-- We use a bit manipulation algorithm instead of Veltkamp's splitting |
| 48 |
-- because it is faster and has the property that the magnitude of the |
| 49 |
-- high part is never larger than that of the input number, which will |
| 50 |
-- avoid spurious overflows in the Two_Prod algorithm. |
| 51 |
|
| 52 |
-- See the recent paper by Claude-Pierre Jeannerod, Jean-Michel Muller |
| 53 |
-- and Paul Zimmermann: On various ways to split a floating-point number |
| 54 |
-- ARITH 2018 - 25th IEEE Symposium on Computer Arithmetic, Jun 2018, |
| 55 |
-- Amherst (MA), United States, pages 53-60. |
| 56 |
|
| 57 |
procedure Split (N : Num; Hi : out Num; Lo : out Num) is |
| 58 |
X : Num; |
| 59 |
|
| 60 |
begin |
| 61 |
-- Spill the input into the appropriate (maybe larger) bit container, |
| 62 |
-- mask out the low bits and reload the modified value. |
| 63 |
|
| 64 |
case Num'Machine_Mantissa is |
| 65 |
when 24 => |
| 66 |
declare |
| 67 |
Rep32 : aliased Interfaces.Unsigned_32; |
| 68 |
Temp : Num := N with Address => Rep32'Address; |
| 69 |
pragma Annotate (CodePeer, Modified, Rep32); |
| 70 |
|
| 71 |
begin |
| 72 |
-- Mask out the low 12 bits |
| 73 |
|
| 74 |
Rep32 := Rep32 and 16#FFFFF000#; |
| 75 |
|
| 76 |
X := Temp; |
| 77 |
end; |
| 78 |
|
| 79 |
when 53 => |
| 80 |
declare |
| 81 |
Rep64 : aliased array (1 .. 2) of Interfaces.Unsigned_64; |
| 82 |
Temp : Num := N with Address => Rep64'Address; |
| 83 |
pragma Annotate (CodePeer, Modified, Rep64); |
| 84 |
|
| 85 |
begin |
| 86 |
-- Mask out the low 27 bits |
| 87 |
|
| 88 |
Rep64 (1) := Rep64 (1) and 16#FFFFFFFFF8000000#; |
| 89 |
|
| 90 |
X := Temp; |
| 91 |
end; |
| 92 |
|
| 93 |
when 64 => |
| 94 |
declare |
| 95 |
Rep80 : aliased array (1 .. 2) of Interfaces.Unsigned_64; |
| 96 |
Temp : Num := N with Address => Rep80'Address; |
| 97 |
pragma Annotate (CodePeer, Modified, Rep80); |
| 98 |
|
| 99 |
begin |
| 100 |
-- Mask out the low 32 bits |
| 101 |
|
| 102 |
if System.Default_Bit_Order = High_Order_First then |
| 103 |
Rep80 (1) := Rep80 (1) and 16#FFFFFFFFFFFF0000#; |
| 104 |
Rep80 (2) := Rep80 (2) and 16#0000FFFFFFFFFFFF#; |
| 105 |
else |
| 106 |
Rep80 (1) := Rep80 (1) and 16#FFFFFFFF00000000#; |
| 107 |
end if; |
| 108 |
|
| 109 |
X := Temp; |
| 110 |
end; |
| 111 |
|
| 112 |
when others => |
| 113 |
raise Program_Error; |
| 114 |
end case; |
| 115 |
|
| 116 |
-- Deal with denormalized numbers |
| 117 |
|
| 118 |
if X = 0.0 then |
| 119 |
Hi := N; |
| 120 |
Lo := 0.0; |
| 121 |
else |
| 122 |
Hi := X; |
| 123 |
Lo := N - X; |
| 124 |
end if; |
| 125 |
end Split; |
| 126 |
|
| 127 |
-------------- |
| 128 |
-- Two_Prod -- |
| 129 |
-------------- |
| 130 |
|
| 131 |
function Two_Prod (A, B : Num) return Double_T is |
| 132 |
P : constant Num := A * B; |
| 133 |
|
| 134 |
Ahi, Alo, Bhi, Blo, E : Num; |
| 135 |
|
| 136 |
begin |
| 137 |
if Is_Infinity (P) or else Is_Zero (P) then |
| 138 |
return (P, 0.0); |
| 139 |
|
| 140 |
else |
| 141 |
Split (A, Ahi, Alo); |
| 142 |
Split (B, Bhi, Blo); |
| 143 |
|
| 144 |
E := ((Ahi * Bhi - P) + Ahi * Blo + Alo * Bhi) + Alo * Blo; |
| 145 |
|
| 146 |
return (P, E); |
| 147 |
end if; |
| 148 |
end Two_Prod; |
| 149 |
|
| 150 |
------------- |
| 151 |
-- Two_Sqr -- |
| 152 |
------------- |
| 153 |
|
| 154 |
function Two_Sqr (A : Num) return Double_T is |
| 155 |
Q : constant Num := A * A; |
| 156 |
|
| 157 |
Hi, Lo, E : Num; |
| 158 |
|
| 159 |
begin |
| 160 |
if Is_Infinity (Q) or else Is_Zero (Q) then |
| 161 |
return (Q, 0.0); |
| 162 |
|
| 163 |
else |
| 164 |
Split (A, Hi, Lo); |
| 165 |
|
| 166 |
E := ((Hi * Hi - Q) + 2.0 * Hi * Lo) + Lo * Lo; |
| 167 |
|
| 168 |
return (Q, E); |
| 169 |
end if; |
| 170 |
end Two_Sqr; |
| 171 |
|
| 172 |
end Product; |