fixing pr42337
[official-gcc.git] / gcc / ada / s-exnllf.adb
bloba1e59c179e0f4eb751ac1bd5b7045d8790b66f5e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . E X N _ L L F --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009 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 ------------------------------------------------------------------------------
32 package body System.Exn_LLF is
34 -------------------------
35 -- Exn_Long_Long_Float --
36 -------------------------
38 function Exn_Long_Long_Float
39 (Left : Long_Long_Float;
40 Right : Integer)
41 return Long_Long_Float
43 Result : Long_Long_Float := 1.0;
44 Factor : Long_Long_Float := Left;
45 Exp : Integer := Right;
47 begin
48 -- We use the standard logarithmic approach, Exp gets shifted right
49 -- testing successive low order bits and Factor is the value of the
50 -- base raised to the next power of 2. If the low order bit or Exp is
51 -- set, multiply the result by this factor. For negative exponents,
52 -- invert result upon return.
54 if Exp >= 0 then
55 loop
56 if Exp rem 2 /= 0 then
57 Result := Result * Factor;
58 end if;
60 Exp := Exp / 2;
61 exit when Exp = 0;
62 Factor := Factor * Factor;
63 end loop;
65 return Result;
67 -- Here we have a negative exponent, and we compute the result as:
69 -- 1.0 / (Left ** (-Right))
71 -- Note that the case of Left being zero is not special, it will
72 -- simply result in a division by zero at the end, yielding a
73 -- correctly signed infinity, or possibly generating an overflow.
75 -- Note on overflow: The coding of this routine assumes that the
76 -- target generates infinities with standard IEEE semantics. If this
77 -- is not the case, then the code below may raise Constraint_Error.
78 -- This follows the implementation permission given in RM 4.5.6(12).
80 else
81 begin
82 loop
83 if Exp rem 2 /= 0 then
84 Result := Result * Factor;
85 end if;
87 Exp := Exp / 2;
88 exit when Exp = 0;
89 Factor := Factor * Factor;
90 end loop;
92 return 1.0 / Result;
93 end;
94 end if;
95 end Exn_Long_Long_Float;
97 end System.Exn_LLF;