| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . F A T _ G E N -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 generic package provides a target independent implementation of the |
| 33 |
-- floating-point attributes that denote functions. The implementations here |
| 34 |
-- are portable, but very slow. The runtime contains a set of instantiations |
| 35 |
-- of this package for all predefined floating-point types, and these should |
| 36 |
-- be replaced by efficient assembly language code where possible. |
| 37 |
|
| 38 |
generic |
| 39 |
type T is digits <>; |
| 40 |
|
| 41 |
package System.Fat_Gen is |
| 42 |
pragma Pure; |
| 43 |
|
| 44 |
subtype UI is Integer; |
| 45 |
-- The runtime representation of universal integer for the purposes of |
| 46 |
-- this package is integer. The expander generates conversions for the |
| 47 |
-- actual type used. For functions returning universal integer, there |
| 48 |
-- is no problem, since the result always is in range of integer. For |
| 49 |
-- input arguments, the expander has to do some special casing to deal |
| 50 |
-- with the (very annoying) cases of out of range values. If we used |
| 51 |
-- Long_Long_Integer to represent universal, then there would be no |
| 52 |
-- problem, but the resulting inefficiency would be annoying. |
| 53 |
|
| 54 |
function Adjacent (X, Towards : T) return T; |
| 55 |
-- If ``Towards`` = ``X``, the function returns ``X``; oterwise, it yields |
| 56 |
-- the machien number of the type *T* adjacent to ``X`` in the direction |
| 57 |
-- ``Towards``, if that machine number exists. |
| 58 |
|
| 59 |
function Ceiling (X : T) return T; |
| 60 |
-- Truncate ``X``. If the truncation is equal to ``X`` return ``X``. If |
| 61 |
-- ``X`` is less than zero, return the truncation, otherwise add one |
| 62 |
-- to the truncation. |
| 63 |
|
| 64 |
function Compose (Fraction : T; Exponent : UI) return T; |
| 65 |
-- Decompose the ``Fraction`` into its fraction and exponent parts. Call |
| 66 |
-- *Scaling* with the returned fraction part and ``Exponent``. |
| 67 |
|
| 68 |
function Copy_Sign (Value, Sign : T) return T; |
| 69 |
-- Take the absolute value of ``Value``. Negate the result if ``Sign`` is |
| 70 |
-- less than zero. |
| 71 |
|
| 72 |
function Exponent (X : T) return UI; |
| 73 |
-- Decompose `X`` and return the exponent part. |
| 74 |
|
| 75 |
function Floor (X : T) return T; |
| 76 |
-- Truncate ``X``. If the truncation is equal to ``X`` return ``X``. If |
| 77 |
-- ``X`` is greater than zero, return the truncation, otherwise subtract |
| 78 |
-- one from the truncation. |
| 79 |
|
| 80 |
function Fraction (X : T) return T; |
| 81 |
-- Decompose `X`` and return the fraction part |
| 82 |
|
| 83 |
function Leading_Part (X : T; Radix_Digits : UI) return T; |
| 84 |
-- Return ``X`` if the ``Radix_Digits`` is larger than the type's machine |
| 85 |
-- mantissa. Otherwise scale down and truncate ``X`` by the difference |
| 86 |
-- between the exponent of ``X`` and ``Radix_Digits``, then scale the |
| 87 |
-- result back up. |
| 88 |
|
| 89 |
function Machine (X : T) return T; |
| 90 |
-- Force ``X`` to be stored in memory and retrieve the result |
| 91 |
|
| 92 |
function Machine_Rounding (X : T) return T; |
| 93 |
-- Truncate the absolute value of ``X`` + 0.5. If ``X`` is negative, negate |
| 94 |
-- the result. |
| 95 |
|
| 96 |
function Model (X : T) return T; |
| 97 |
-- If ``X`` is a model number of *T*, the function returns ``X``; |
| 98 |
-- otherwise it yields the value obtained by rounding or truncating ``X`` |
| 99 |
-- to either one of the adjacent model numbers of *T*. |
| 100 |
-- |
| 101 |
-- We treat *Model* as identical to *Machine*. This is true of IEEE and |
| 102 |
-- other nice floating-point systems, but not necessarily true of all |
| 103 |
-- systems. |
| 104 |
|
| 105 |
function Pred (X : T) return T; |
| 106 |
-- Return the machine number immediately below the value of ``X``. |
| 107 |
-- |
| 108 |
-- If zero, return the negative of *Succ* (``X``). |
| 109 |
-- |
| 110 |
-- If ``X`` = *T*'First, return negative infinity. |
| 111 |
-- |
| 112 |
-- If ``X`` is already infinity, return ``X``. |
| 113 |
-- |
| 114 |
-- Otherwise, subtract from ``X`` a number equivalent to the value of its |
| 115 |
-- least significant bit. |
| 116 |
|
| 117 |
function Remainder (X, Y : T) return T; |
| 118 |
-- Return the remainder (n) of ``X`` divided by ``Y``. |
| 119 |
-- If abs(n - ``X`` / ``Y``) = 1/2 then n is chosen to be even. |
| 120 |
-- |
| 121 |
-- Calculate the modulus remainder: if abs(``X``) < abs(``Y``) then the |
| 122 |
-- remainder is abs(``X``). Otherwise, decompose abs(``X``) and abs(``Y``). |
| 123 |
-- Then: |
| 124 |
-- |
| 125 |
-- .. code-block:: ada |
| 126 |
-- |
| 127 |
-- P := Compose (Y_Frac, X_Exp); |
| 128 |
-- K := X_Exp - Y_Exp; |
| 129 |
-- Rem := |X|; |
| 130 |
-- for J in reverse 0 .. K loop |
| 131 |
-- if Rem >= P then |
| 132 |
-- Rem := Rem - P; |
| 133 |
-- end if; |
| 134 |
-- P := P * 0.5; |
| 135 |
-- end loop; |
| 136 |
-- |
| 137 |
-- Return the IEEE remainder by adjusting result such that if |
| 138 |
-- abs(n - X/Y) = 1/2 then n is even. |
| 139 |
|
| 140 |
function Rounding (X : T) return T; |
| 141 |
-- The function yields the integral value nearest to ``X``, rounding away |
| 142 |
-- from zero if ``X`` lies exactly halfway between two integers. |
| 143 |
-- |
| 144 |
-- The function truncates the absolute value of ``X`` + 0.5. If ``X`` is |
| 145 |
-- negative, negate the result. |
| 146 |
|
| 147 |
function Scaling (X : T; Adjustment : UI) return T; |
| 148 |
-- Let v be the value ``X`` * *T*'Machine_RadixAdjustment. If v is a |
| 149 |
-- machine number of the type *T*, or if abs(v) >= *T*'Model_Small, the |
| 150 |
-- function yields v; otherwise, it yields either one of the machine |
| 151 |
-- numbers of the type *T* adjacent to v. |
| 152 |
-- |
| 153 |
-- If ``X`` or ``Adjustment`` equal zero, return ``X``. Otherwise, return |
| 154 |
-- ``X`` * Machine_Radix ** ``Adjustment``. |
| 155 |
|
| 156 |
function Succ (X : T) return T; |
| 157 |
-- Returns the machine number immediately above the value of X. |
| 158 |
-- |
| 159 |
-- If zero, return the smallest denormal. |
| 160 |
-- |
| 161 |
-- If ``X`` = *T*'Last, return infinity. |
| 162 |
-- |
| 163 |
-- If ``X`` is already infinity, return ``X``. |
| 164 |
-- |
| 165 |
-- Otherwise, add to X a number equivalent to the value of its least |
| 166 |
-- significant bit. |
| 167 |
|
| 168 |
function Truncation (X : T) return T; |
| 169 |
-- The function yields the value *Ceiling* (``X``) when ``X`` is negative, |
| 170 |
-- and *Floor* (``X``) otherwise. |
| 171 |
-- |
| 172 |
-- Return *T*'Machine (RM1 + N) - RM1 where N is abs(``X``) and |
| 173 |
-- RM1 = radix ** (mantissa - 1). Negate the result where ``X`` is |
| 174 |
-- negative. |
| 175 |
|
| 176 |
function Unbiased_Rounding (X : T) return T; |
| 177 |
-- The integral value nearest to ``X``, rounding toward the even integer |
| 178 |
-- if ``X`` lies exactly halfway between two integers. |
| 179 |
-- |
| 180 |
-- This function truncates abs(``X``). If the tail of the result is greater |
| 181 |
-- than 0.5 add one to the result. If the tail equals 0.5, round to the |
| 182 |
-- nearest even integer. Negate the result if ``X`` is negative. |
| 183 |
|
| 184 |
function Valid (X : not null access T) return Boolean; |
| 185 |
-- This function checks if the object of type *T* referenced by ``X`` is |
| 186 |
-- valid, and returns True/False accordingly. The parameter is passed by |
| 187 |
-- reference (access) here, as the object of type T may be an abnormal |
| 188 |
-- value that cannot be passed in a floating-point register, and the whole |
| 189 |
-- point of 'Valid is to prevent exceptions. Note that the object of |
| 190 |
-- type *T* must have the natural alignment for type *T*. |
| 191 |
-- |
| 192 |
-- If denormalized numbers are valid: return True unless ``X`` is infinity |
| 193 |
-- or NaN. If denormalized numbers are not valid, return False if ``X`` is |
| 194 |
-- a denormal number. |
| 195 |
|
| 196 |
type S is new String (1 .. T'Size / Character'Size); |
| 197 |
type P is access all S with Storage_Size => 0; |
| 198 |
-- Buffer and access types used to initialize temporaries for validity |
| 199 |
-- checks, if the value to be checked has reverse scalar storage order, or |
| 200 |
-- is not known to be properly aligned (for example it appears in a packed |
| 201 |
-- record). In this case, we cannot call Valid since Valid assumes proper |
| 202 |
-- full alignment. Instead, we copy the value to a temporary location using |
| 203 |
-- type S (we cannot simply do a copy of a T value, because the value might |
| 204 |
-- be invalid, in which case it might not be possible to copy it through a |
| 205 |
-- floating point register). |
| 206 |
|
| 207 |
private |
| 208 |
pragma Inline (Compose); |
| 209 |
pragma Inline (Copy_Sign); |
| 210 |
pragma Inline (Exponent); |
| 211 |
pragma Inline (Fraction); |
| 212 |
pragma Inline (Machine); |
| 213 |
pragma Inline (Model); |
| 214 |
pragma Inline (Valid); |
| 215 |
|
| 216 |
end System.Fat_Gen; |