OSDN Git Service

2008-04-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-numaux-x86.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                     A D A . N U M E R I C S . A U X                      --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                        (Machine Version for x86)                         --
9 --                                                                          --
10 --          Copyright (C) 1998-2007, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, USA.                                              --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT was originally developed  by the GNAT team at  New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  File a-numaux.adb <- 86numaux.adb
36
37 --  This version of Numerics.Aux is for the IEEE Double Extended floating
38 --  point format on x86.
39
40 with System.Machine_Code; use System.Machine_Code;
41
42 package body Ada.Numerics.Aux is
43
44    NL : constant String := ASCII.LF & ASCII.HT;
45
46    -----------------------
47    -- Local subprograms --
48    -----------------------
49
50    function Is_Nan (X : Double) return Boolean;
51    --  Return True iff X is a IEEE NaN value
52
53    function Logarithmic_Pow (X, Y : Double) return Double;
54    --  Implementation of X**Y using Exp and Log functions (binary base)
55    --  to calculate the exponentiation. This is used by Pow for values
56    --  for values of Y in the open interval (-0.25, 0.25)
57
58    procedure Reduce (X : in out Double; Q : out Natural);
59    --  Implements reduction of X by Pi/2. Q is the quadrant of the final
60    --  result in the range 0 .. 3. The absolute value of X is at most Pi.
61
62    pragma Inline (Is_Nan);
63    pragma Inline (Reduce);
64
65    --------------------------------
66    -- Basic Elementary Functions --
67    --------------------------------
68
69    --  This section implements a few elementary functions that are used to
70    --  build the more complex ones. This ordering enables better inlining.
71
72    ----------
73    -- Atan --
74    ----------
75
76    function Atan (X : Double) return Double is
77       Result  : Double;
78
79    begin
80       Asm (Template =>
81            "fld1" & NL
82          & "fpatan",
83          Outputs  => Double'Asm_Output ("=t", Result),
84          Inputs   => Double'Asm_Input  ("0", X));
85
86       --  The result value is NaN iff input was invalid
87
88       if not (Result = Result) then
89          raise Argument_Error;
90       end if;
91
92       return Result;
93    end Atan;
94
95    ---------
96    -- Exp --
97    ---------
98
99    function Exp (X : Double) return Double is
100       Result : Double;
101    begin
102       Asm (Template =>
103          "fldl2e               " & NL
104        & "fmulp   %%st, %%st(1)" & NL -- X * log2 (E)
105        & "fld     %%st(0)      " & NL
106        & "frndint              " & NL -- Integer (X * Log2 (E))
107        & "fsubr   %%st, %%st(1)" & NL -- Fraction (X * Log2 (E))
108        & "fxch                 " & NL
109        & "f2xm1                " & NL -- 2**(...) - 1
110        & "fld1                 " & NL
111        & "faddp   %%st, %%st(1)" & NL -- 2**(Fraction (X * Log2 (E)))
112        & "fscale               " & NL -- E ** X
113        & "fstp    %%st(1)      ",
114          Outputs  => Double'Asm_Output ("=t", Result),
115          Inputs   => Double'Asm_Input  ("0", X));
116       return Result;
117    end Exp;
118
119    ------------
120    -- Is_Nan --
121    ------------
122
123    function Is_Nan (X : Double) return Boolean is
124    begin
125       --  The IEEE NaN values are the only ones that do not equal themselves
126
127       return not (X = X);
128    end Is_Nan;
129
130    ---------
131    -- Log --
132    ---------
133
134    function Log (X : Double) return Double is
135       Result : Double;
136
137    begin
138       Asm (Template =>
139          "fldln2               " & NL
140        & "fxch                 " & NL
141        & "fyl2x                " & NL,
142          Outputs  => Double'Asm_Output ("=t", Result),
143          Inputs   => Double'Asm_Input  ("0", X));
144       return Result;
145    end Log;
146
147    ------------
148    -- Reduce --
149    ------------
150
151    procedure Reduce (X : in out Double; Q : out Natural) is
152       Half_Pi     : constant := Pi / 2.0;
153       Two_Over_Pi : constant := 2.0 / Pi;
154
155       HM : constant := Integer'Min (Double'Machine_Mantissa / 2, Natural'Size);
156       M  : constant Double := 0.5 + 2.0**(1 - HM); -- Splitting constant
157       P1 : constant Double := Double'Leading_Part (Half_Pi, HM);
158       P2 : constant Double := Double'Leading_Part (Half_Pi - P1, HM);
159       P3 : constant Double := Double'Leading_Part (Half_Pi - P1 - P2, HM);
160       P4 : constant Double := Double'Leading_Part (Half_Pi - P1 - P2 - P3, HM);
161       P5 : constant Double := Double'Leading_Part (Half_Pi - P1 - P2 - P3
162                                                                  - P4, HM);
163       P6 : constant Double := Double'Model (Half_Pi - P1 - P2 - P3 - P4 - P5);
164       K  : Double := X * Two_Over_Pi;
165    begin
166       --  For X < 2.0**32, all products below are computed exactly.
167       --  Due to cancellation effects all subtractions are exact as well.
168       --  As no double extended floating-point number has more than 75
169       --  zeros after the binary point, the result will be the correctly
170       --  rounded result of X - K * (Pi / 2.0).
171
172       while abs K >= 2.0**HM loop
173          K := K * M - (K * M - K);
174          X := (((((X - K * P1) - K * P2) - K * P3)
175                      - K * P4) - K * P5) - K * P6;
176          K := X * Two_Over_Pi;
177       end loop;
178
179       if K /= K then
180
181          --  K is not a number, because X was not finite
182
183          raise Constraint_Error;
184       end if;
185
186       K := Double'Rounding (K);
187       Q := Integer (K) mod 4;
188       X := (((((X - K * P1) - K * P2) - K * P3)
189                   - K * P4) - K * P5) - K * P6;
190    end Reduce;
191
192    ----------
193    -- Sqrt --
194    ----------
195
196    function Sqrt (X : Double) return Double is
197       Result  : Double;
198
199    begin
200       if X < 0.0 then
201          raise Argument_Error;
202       end if;
203
204       Asm (Template => "fsqrt",
205            Outputs  => Double'Asm_Output ("=t", Result),
206            Inputs   => Double'Asm_Input  ("0", X));
207
208       return Result;
209    end Sqrt;
210
211    --------------------------------
212    -- Other Elementary Functions --
213    --------------------------------
214
215    --  These are built using the previously implemented basic functions
216
217    ----------
218    -- Acos --
219    ----------
220
221    function Acos (X : Double) return Double is
222       Result  : Double;
223
224    begin
225       Result := 2.0 * Atan (Sqrt ((1.0 - X) / (1.0 + X)));
226
227       --  The result value is NaN iff input was invalid
228
229       if Is_Nan (Result) then
230          raise Argument_Error;
231       end if;
232
233       return Result;
234    end Acos;
235
236    ----------
237    -- Asin --
238    ----------
239
240    function Asin (X : Double) return Double is
241       Result  : Double;
242
243    begin
244       Result := Atan (X / Sqrt ((1.0 - X) * (1.0 + X)));
245
246       --  The result value is NaN iff input was invalid
247
248       if Is_Nan (Result) then
249          raise Argument_Error;
250       end if;
251
252       return Result;
253    end Asin;
254
255    ---------
256    -- Cos --
257    ---------
258
259    function Cos (X : Double) return Double is
260       Reduced_X : Double := abs X;
261       Result    : Double;
262       Quadrant  : Natural range 0 .. 3;
263
264    begin
265       if Reduced_X > Pi / 4.0 then
266          Reduce (Reduced_X, Quadrant);
267
268          case Quadrant is
269             when 0 =>
270                Asm (Template  => "fcos",
271                   Outputs  => Double'Asm_Output ("=t", Result),
272                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
273             when 1 =>
274                Asm (Template  => "fsin",
275                   Outputs  => Double'Asm_Output ("=t", Result),
276                   Inputs   => Double'Asm_Input  ("0", -Reduced_X));
277             when 2 =>
278                Asm (Template  => "fcos ; fchs",
279                   Outputs  => Double'Asm_Output ("=t", Result),
280                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
281             when 3 =>
282                Asm (Template  => "fsin",
283                   Outputs  => Double'Asm_Output ("=t", Result),
284                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
285          end case;
286
287       else
288          Asm (Template  => "fcos",
289               Outputs  => Double'Asm_Output ("=t", Result),
290               Inputs   => Double'Asm_Input  ("0", Reduced_X));
291       end if;
292
293       return Result;
294    end Cos;
295
296    ---------------------
297    -- Logarithmic_Pow --
298    ---------------------
299
300    function Logarithmic_Pow (X, Y : Double) return Double is
301       Result  : Double;
302    begin
303       Asm (Template => ""             --  X                  : Y
304        & "fyl2x                " & NL --  Y * Log2 (X)
305        & "fld     %%st(0)      " & NL --  Y * Log2 (X)       : Y * Log2 (X)
306        & "frndint              " & NL --  Int (...)          : Y * Log2 (X)
307        & "fsubr   %%st, %%st(1)" & NL --  Int (...)          : Fract (...)
308        & "fxch                 " & NL --  Fract (...)        : Int (...)
309        & "f2xm1                " & NL --  2**Fract (...) - 1 : Int (...)
310        & "fld1                 " & NL --  1 : 2**Fract (...) - 1 : Int (...)
311        & "faddp   %%st, %%st(1)" & NL --  2**Fract (...)     : Int (...)
312        & "fscale               ",     --  2**(Fract (...) + Int (...))
313          Outputs  => Double'Asm_Output ("=t", Result),
314          Inputs   =>
315            (Double'Asm_Input  ("0", X),
316             Double'Asm_Input  ("u", Y)));
317       return Result;
318    end Logarithmic_Pow;
319
320    ---------
321    -- Pow --
322    ---------
323
324    function Pow (X, Y : Double) return Double is
325       type Mantissa_Type is mod 2**Double'Machine_Mantissa;
326       --  Modular type that can hold all bits of the mantissa of Double
327
328       --  For negative exponents, do divide at the end of the processing
329
330       Negative_Y : constant Boolean := Y < 0.0;
331       Abs_Y      : constant Double := abs Y;
332
333       --  During this function the following invariant is kept:
334       --  X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
335
336       Base : Double := X;
337
338       Exp_High : Double := Double'Floor (Abs_Y);
339       Exp_Mid  : Double;
340       Exp_Low  : Double;
341       Exp_Int  : Mantissa_Type;
342
343       Factor : Double := 1.0;
344
345    begin
346       --  Select algorithm for calculating Pow (integer cases fall through)
347
348       if Exp_High >= 2.0**Double'Machine_Mantissa then
349
350          --  In case of Y that is IEEE infinity, just raise constraint error
351
352          if Exp_High > Double'Safe_Last then
353             raise Constraint_Error;
354          end if;
355
356          --  Large values of Y are even integers and will stay integer
357          --  after division by two.
358
359          loop
360             --  Exp_Mid and Exp_Low are zero, so
361             --    X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
362
363             Exp_High := Exp_High / 2.0;
364             Base := Base * Base;
365             exit when Exp_High < 2.0**Double'Machine_Mantissa;
366          end loop;
367
368       elsif Exp_High /= Abs_Y then
369          Exp_Low := Abs_Y - Exp_High;
370          Factor := 1.0;
371
372          if Exp_Low /= 0.0 then
373
374             --  Exp_Low now is in interval (0.0, 1.0)
375             --  Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
376
377             Exp_Mid := 0.0;
378             Exp_Low := Exp_Low - Exp_Mid;
379
380             if Exp_Low >= 0.5 then
381                Factor := Sqrt (X);
382                Exp_Low := Exp_Low - 0.5;  -- exact
383
384                if Exp_Low >= 0.25 then
385                   Factor := Factor * Sqrt (Factor);
386                   Exp_Low := Exp_Low - 0.25; --  exact
387                end if;
388
389             elsif Exp_Low >= 0.25 then
390                Factor := Sqrt (Sqrt (X));
391                Exp_Low := Exp_Low - 0.25; --  exact
392             end if;
393
394             --  Exp_Low now is in interval (0.0, 0.25)
395
396             --  This means it is safe to call Logarithmic_Pow
397             --  for the remaining part.
398
399             Factor := Factor * Logarithmic_Pow (X, Exp_Low);
400          end if;
401
402       elsif X = 0.0 then
403          return 0.0;
404       end if;
405
406       --  Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
407
408       Exp_Int := Mantissa_Type (Exp_High);
409
410       --  Standard way for processing integer powers > 0
411
412       while Exp_Int > 1 loop
413          if (Exp_Int and 1) = 1 then
414
415             --  Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
416
417             Factor := Factor * Base;
418          end if;
419
420          --  Exp_Int is even and Exp_Int > 0, so
421          --    Base**Y = (Base**2)**(Exp_Int / 2)
422
423          Base := Base * Base;
424          Exp_Int := Exp_Int / 2;
425       end loop;
426
427       --  Exp_Int = 1 or Exp_Int = 0
428
429       if Exp_Int = 1 then
430          Factor := Base * Factor;
431       end if;
432
433       if Negative_Y then
434          Factor := 1.0 / Factor;
435       end if;
436
437       return Factor;
438    end Pow;
439
440    ---------
441    -- Sin --
442    ---------
443
444    function Sin (X : Double) return Double is
445       Reduced_X : Double := X;
446       Result    : Double;
447       Quadrant  : Natural range 0 .. 3;
448
449    begin
450       if abs X > Pi / 4.0 then
451          Reduce (Reduced_X, Quadrant);
452
453          case Quadrant is
454             when 0 =>
455                Asm (Template  => "fsin",
456                   Outputs  => Double'Asm_Output ("=t", Result),
457                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
458             when 1 =>
459                Asm (Template  => "fcos",
460                   Outputs  => Double'Asm_Output ("=t", Result),
461                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
462             when 2 =>
463                Asm (Template  => "fsin",
464                   Outputs  => Double'Asm_Output ("=t", Result),
465                   Inputs   => Double'Asm_Input  ("0", -Reduced_X));
466             when 3 =>
467                Asm (Template  => "fcos ; fchs",
468                   Outputs  => Double'Asm_Output ("=t", Result),
469                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
470          end case;
471
472       else
473          Asm (Template  => "fsin",
474             Outputs  => Double'Asm_Output ("=t", Result),
475             Inputs   => Double'Asm_Input  ("0", Reduced_X));
476       end if;
477
478       return Result;
479    end Sin;
480
481    ---------
482    -- Tan --
483    ---------
484
485    function Tan (X : Double) return Double is
486       Reduced_X : Double := X;
487       Result    : Double;
488       Quadrant  : Natural range 0 .. 3;
489
490    begin
491       if abs X > Pi / 4.0 then
492          Reduce (Reduced_X, Quadrant);
493
494          if Quadrant mod 2 = 0 then
495             Asm (Template  => "fptan" & NL
496                             & "ffree   %%st(0)"  & NL
497                             & "fincstp",
498                   Outputs  => Double'Asm_Output ("=t", Result),
499                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
500          else
501             Asm (Template  => "fsincos" & NL
502                             & "fdivp   %%st, %%st(1)" & NL
503                             & "fchs",
504                   Outputs  => Double'Asm_Output ("=t", Result),
505                   Inputs   => Double'Asm_Input  ("0", Reduced_X));
506          end if;
507
508       else
509          Asm (Template  =>
510                "fptan                 " & NL
511              & "ffree   %%st(0)      " & NL
512              & "fincstp              ",
513                Outputs  => Double'Asm_Output ("=t", Result),
514                Inputs   => Double'Asm_Input  ("0", Reduced_X));
515       end if;
516
517       return Result;
518    end Tan;
519
520    ----------
521    -- Sinh --
522    ----------
523
524    function Sinh (X : Double) return Double is
525    begin
526       --  Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
527
528       if abs X < 25.0 then
529          return (Exp (X) - Exp (-X)) / 2.0;
530       else
531          return Exp (X) / 2.0;
532       end if;
533    end Sinh;
534
535    ----------
536    -- Cosh --
537    ----------
538
539    function Cosh (X : Double) return Double is
540    begin
541       --  Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
542
543       if abs X < 22.0 then
544          return (Exp (X) + Exp (-X)) / 2.0;
545       else
546          return Exp (X) / 2.0;
547       end if;
548    end Cosh;
549
550    ----------
551    -- Tanh --
552    ----------
553
554    function Tanh (X : Double) return Double is
555    begin
556       --  Return the Hyperbolic Tangent of x
557
558       --                                    x    -x
559       --                                   e  - e        Sinh (X)
560       --       Tanh (X) is defined to be -----------   = --------
561       --                                    x    -x      Cosh (X)
562       --                                   e  + e
563
564       if abs X > 23.0 then
565          return Double'Copy_Sign (1.0, X);
566       end if;
567
568       return 1.0 / (1.0 + Exp (-(2.0 * X))) - 1.0 / (1.0 + Exp (2.0 * X));
569    end Tanh;
570
571 end Ada.Numerics.Aux;