| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . D O U B L E _ R E A L -- |
| 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 |
package body System.Double_Real is |
| 33 |
|
| 34 |
function Is_NaN (N : Num) return Boolean is (N /= N); |
| 35 |
-- Return True if N is a NaN |
| 36 |
|
| 37 |
function Is_Infinity (N : Num) return Boolean is (Is_NaN (N - N)); |
| 38 |
-- Return True if N is an infinity. Used to avoid propagating meaningless |
| 39 |
-- errors when the result of a product is an infinity. |
| 40 |
|
| 41 |
function Is_Zero (N : Num) return Boolean is (N = -N); |
| 42 |
-- Return True if N is a Zero. Used to preserve the sign when the result of |
| 43 |
-- a product is a zero. |
| 44 |
|
| 45 |
package Product is |
| 46 |
function Two_Prod (A, B : Num) return Double_T; |
| 47 |
function Two_Sqr (A : Num) return Double_T; |
| 48 |
end Product; |
| 49 |
-- The low-level implementation of multiplicative operations |
| 50 |
|
| 51 |
package body Product is separate; |
| 52 |
-- This is a separate body because the implementation depends on whether a |
| 53 |
-- Fused Multiply-Add instruction is available on the target. |
| 54 |
|
| 55 |
------------------- |
| 56 |
-- Quick_Two_Sum -- |
| 57 |
------------------- |
| 58 |
|
| 59 |
function Quick_Two_Sum (A, B : Num) return Double_T is |
| 60 |
S : constant Num := A + B; |
| 61 |
V : constant Num := S - A; |
| 62 |
E : constant Num := B - V; |
| 63 |
|
| 64 |
begin |
| 65 |
return (S, E); |
| 66 |
end Quick_Two_Sum; |
| 67 |
|
| 68 |
------------- |
| 69 |
-- Two_Sum -- |
| 70 |
------------- |
| 71 |
|
| 72 |
function Two_Sum (A, B : Num) return Double_T is |
| 73 |
S : constant Num := A + B; |
| 74 |
V : constant Num := S - A; |
| 75 |
E : constant Num := (A - (S - V)) + (B - V); |
| 76 |
|
| 77 |
begin |
| 78 |
return (S, E); |
| 79 |
end Two_Sum; |
| 80 |
|
| 81 |
-------------- |
| 82 |
-- Two_Diff -- |
| 83 |
-------------- |
| 84 |
|
| 85 |
function Two_Diff (A, B : Num) return Double_T is |
| 86 |
S : constant Num := A - B; |
| 87 |
V : constant Num := S - A; |
| 88 |
E : constant Num := (A - (S - V)) - (B + V); |
| 89 |
|
| 90 |
begin |
| 91 |
return (S, E); |
| 92 |
end Two_Diff; |
| 93 |
|
| 94 |
-------------- |
| 95 |
-- Two_Prod -- |
| 96 |
-------------- |
| 97 |
|
| 98 |
function Two_Prod (A, B : Num) return Double_T renames Product.Two_Prod; |
| 99 |
|
| 100 |
------------- |
| 101 |
-- Two_Sqr -- |
| 102 |
------------- |
| 103 |
|
| 104 |
function Two_Sqr (A : Num) return Double_T renames Product.Two_Sqr; |
| 105 |
|
| 106 |
--------- |
| 107 |
-- "+" -- |
| 108 |
--------- |
| 109 |
|
| 110 |
function "+" (A : Double_T; B : Num) return Double_T is |
| 111 |
S : constant Double_T := Two_Sum (A.Hi, B); |
| 112 |
|
| 113 |
begin |
| 114 |
return Quick_Two_Sum (S.Hi, S.Lo + A.Lo); |
| 115 |
end "+"; |
| 116 |
|
| 117 |
function "+" (A, B : Double_T) return Double_T is |
| 118 |
S1 : constant Double_T := Two_Sum (A.Hi, B.Hi); |
| 119 |
S2 : constant Double_T := Two_Sum (A.Lo, B.Lo); |
| 120 |
S3 : constant Double_T := Quick_Two_Sum (S1.Hi, S1.Lo + S2.Hi); |
| 121 |
|
| 122 |
begin |
| 123 |
return Quick_Two_Sum (S3.Hi, S3.Lo + S2.Lo); |
| 124 |
end "+"; |
| 125 |
|
| 126 |
--------- |
| 127 |
-- "-" -- |
| 128 |
--------- |
| 129 |
|
| 130 |
function "-" (A : Double_T; B : Num) return Double_T is |
| 131 |
D : constant Double_T := Two_Diff (A.Hi, B); |
| 132 |
|
| 133 |
begin |
| 134 |
return Quick_Two_Sum (D.Hi, D.Lo + A.Lo); |
| 135 |
end "-"; |
| 136 |
|
| 137 |
function "-" (A, B : Double_T) return Double_T is |
| 138 |
D1 : constant Double_T := Two_Diff (A.Hi, B.Hi); |
| 139 |
D2 : constant Double_T := Two_Diff (A.Lo, B.Lo); |
| 140 |
D3 : constant Double_T := Quick_Two_Sum (D1.Hi, D1.Lo + D2.Hi); |
| 141 |
|
| 142 |
begin |
| 143 |
return Quick_Two_Sum (D3.Hi, D3.Lo + D2.Lo); |
| 144 |
end "-"; |
| 145 |
|
| 146 |
--------- |
| 147 |
-- "*" -- |
| 148 |
--------- |
| 149 |
|
| 150 |
function "*" (A : Double_T; B : Num) return Double_T is |
| 151 |
P : constant Double_T := Two_Prod (A.Hi, B); |
| 152 |
|
| 153 |
begin |
| 154 |
if Is_Infinity (P.Hi) or else Is_Zero (P.Hi) then |
| 155 |
return (P.Hi, 0.0); |
| 156 |
else |
| 157 |
return Quick_Two_Sum (P.Hi, P.Lo + A.Lo * B); |
| 158 |
end if; |
| 159 |
end "*"; |
| 160 |
|
| 161 |
function "*" (A, B : Double_T) return Double_T is |
| 162 |
P : constant Double_T := Two_Prod (A.Hi, B.Hi); |
| 163 |
|
| 164 |
begin |
| 165 |
if Is_Infinity (P.Hi) or else Is_Zero (P.Hi) then |
| 166 |
return (P.Hi, 0.0); |
| 167 |
else |
| 168 |
return Quick_Two_Sum (P.Hi, P.Lo + A.Hi * B.Lo + A.Lo * B.Hi); |
| 169 |
end if; |
| 170 |
end "*"; |
| 171 |
|
| 172 |
--------- |
| 173 |
-- "/" -- |
| 174 |
--------- |
| 175 |
|
| 176 |
function "/" (A : Double_T; B : Num) return Double_T is |
| 177 |
Q1, Q2 : Num; |
| 178 |
P, R : Double_T; |
| 179 |
|
| 180 |
begin |
| 181 |
if Is_Infinity (B) or else Is_Zero (B) then |
| 182 |
return (A.Hi / B, 0.0); |
| 183 |
end if; |
| 184 |
pragma Annotate (CodePeer, Intentional, "test always false", |
| 185 |
"code deals with infinity"); |
| 186 |
|
| 187 |
Q1 := A.Hi / B; |
| 188 |
|
| 189 |
-- Compute R = A - B * Q1 |
| 190 |
|
| 191 |
P := Two_Prod (B, Q1); |
| 192 |
R := Two_Diff (A.Hi, P.Hi); |
| 193 |
R.Lo := (R.Lo + A.Lo) - P.Lo; |
| 194 |
|
| 195 |
Q2 := (R.Hi + R.Lo) / B; |
| 196 |
|
| 197 |
return Quick_Two_Sum (Q1, Q2); |
| 198 |
end "/"; |
| 199 |
|
| 200 |
function "/" (A, B : Double_T) return Double_T is |
| 201 |
Q1, Q2, Q3 : Num; |
| 202 |
R, S : Double_T; |
| 203 |
|
| 204 |
begin |
| 205 |
if Is_Infinity (B.Hi) or else Is_Zero (B.Hi) then |
| 206 |
return (A.Hi / B.Hi, 0.0); |
| 207 |
end if; |
| 208 |
pragma Annotate (CodePeer, Intentional, "test always false", |
| 209 |
"code deals with infinity"); |
| 210 |
|
| 211 |
Q1 := A.Hi / B.Hi; |
| 212 |
R := A - B * Q1; |
| 213 |
|
| 214 |
Q2 := R.Hi / B.Hi; |
| 215 |
R := R - B * Q2; |
| 216 |
|
| 217 |
Q3 := R.Hi / B.Hi; |
| 218 |
|
| 219 |
S := Quick_Two_Sum (Q1, Q2); |
| 220 |
return Quick_Two_Sum (S.Hi, S.Lo + Q3); |
| 221 |
end "/"; |
| 222 |
|
| 223 |
--------- |
| 224 |
-- Sqr -- |
| 225 |
--------- |
| 226 |
|
| 227 |
function Sqr (A : Double_T) return Double_T is |
| 228 |
Q : constant Double_T := Two_Sqr (A.Hi); |
| 229 |
|
| 230 |
begin |
| 231 |
if Is_Infinity (Q.Hi) or else Is_Zero (Q.Hi) then |
| 232 |
return (Q.Hi, 0.0); |
| 233 |
else |
| 234 |
return Quick_Two_Sum (Q.Hi, Q.Lo + 2.0 * A.Hi * A.Lo + A.Lo * A.Lo); |
| 235 |
end if; |
| 236 |
end Sqr; |
| 237 |
|
| 238 |
------------------- |
| 239 |
-- From_Unsigned -- |
| 240 |
------------------- |
| 241 |
|
| 242 |
function From_Unsigned (U : Uns) return Double_T is |
| 243 |
begin |
| 244 |
return To_Double (Num (U)); |
| 245 |
end From_Unsigned; |
| 246 |
|
| 247 |
----------------- |
| 248 |
-- To_Unsigned -- |
| 249 |
----------------- |
| 250 |
|
| 251 |
function To_Unsigned (D : Double_T) return Uns is |
| 252 |
Hi : constant Num := Num'Truncation (D.Hi); |
| 253 |
|
| 254 |
begin |
| 255 |
-- If the high part is already an integer, add Floor of the low part, |
| 256 |
-- which means subtract Ceiling of its opposite if it is negative. |
| 257 |
|
| 258 |
if Hi = D.Hi then |
| 259 |
if D.Lo < 0.0 then |
| 260 |
return Uns (Hi) - Uns (Num'Ceiling (-D.Lo)); |
| 261 |
else |
| 262 |
return Uns (Hi) + Uns (Num'Floor (D.Lo)); |
| 263 |
end if; |
| 264 |
|
| 265 |
else |
| 266 |
return Uns (Hi); |
| 267 |
end if; |
| 268 |
end To_Unsigned; |
| 269 |
|
| 270 |
end System.Double_Real; |