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-2012, 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
32package body System.Exn_LLF is
33
34   -------------------------
35   -- Exn_Long_Long_Float --
36   -------------------------
37
38   function Exn_Long_Long_Float
39     (Left  : Long_Long_Float;
40      Right : Integer) return Long_Long_Float
41   is
42      Result : Long_Long_Float := 1.0;
43      Factor : Long_Long_Float := Left;
44      Exp    : Integer := Right;
45
46   begin
47      --  We use the standard logarithmic approach, Exp gets shifted right
48      --  testing successive low order bits and Factor is the value of the
49      --  base raised to the next power of 2. If the low order bit or Exp is
50      --  set, multiply the result by this factor. For negative exponents,
51      --  invert result upon return.
52
53      if Exp >= 0 then
54         loop
55            if Exp rem 2 /= 0 then
56               Result := Result * Factor;
57            end if;
58
59            Exp := Exp / 2;
60            exit when Exp = 0;
61            Factor := Factor * Factor;
62         end loop;
63
64         return Result;
65
66      --  Here we have a negative exponent, and we compute the result as:
67
68      --     1.0 / (Left ** (-Right))
69
70      --  Note that the case of Left being zero is not special, it will
71      --  simply result in a division by zero at the end, yielding a
72      --  correctly signed infinity, or possibly generating an overflow.
73
74      --  Note on overflow: The coding of this routine assumes that the
75      --  target generates infinities with standard IEEE semantics. If this
76      --  is not the case, then the code below may raise Constraint_Error.
77      --  This follows the implementation permission given in RM 4.5.6(12).
78
79      else
80         begin
81            loop
82               if Exp rem 2 /= 0 then
83                  Result := Result * Factor;
84               end if;
85
86               Exp := Exp / 2;
87               exit when Exp = 0;
88               Factor := Factor * Factor;
89            end loop;
90
91            return 1.0 / Result;
92         end;
93      end if;
94   end Exn_Long_Long_Float;
95
96end System.Exn_LLF;
97