* c-decl.c (duplicate_decls): Conditionalize DECL_SAVED_TREE copy.
[official-gcc.git] / gcc / ada / 86numaux.adb
blob69a7f338686b85390a6270b83fbc79accdad834d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME 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 -- $Revision$
11 -- --
12 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
13 -- --
14 -- GNAT is free software; you can redistribute it and/or modify it under --
15 -- terms of the GNU General Public License as published by the Free Soft- --
16 -- ware Foundation; either version 2, or (at your option) any later ver- --
17 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
20 -- for more details. You should have received a copy of the GNU General --
21 -- Public License distributed with GNAT; see file COPYING. If not, write --
22 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
23 -- MA 02111-1307, USA. --
24 -- --
25 -- As a special exception, if other files instantiate generics from this --
26 -- unit, or you link this unit with other files to produce an executable, --
27 -- this unit does not by itself cause the resulting executable to be --
28 -- covered by the GNU General Public License. This exception does not --
29 -- however invalidate any other reasons why the executable file might be --
30 -- covered by the GNU Public License. --
31 -- --
32 -- GNAT was originally developed by the GNAT team at New York University. --
33 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 -- --
35 ------------------------------------------------------------------------------
37 -- File a-numaux.adb <- 86numaux.adb
39 -- This version of Numerics.Aux is for the IEEE Double Extended floating
40 -- point format on x86.
42 with System.Machine_Code; use System.Machine_Code;
44 package body Ada.Numerics.Aux is
46 NL : constant String := ASCII.LF & ASCII.HT;
48 type FPU_Stack_Pointer is range 0 .. 7;
49 for FPU_Stack_Pointer'Size use 3;
51 type FPU_Status_Word is record
52 B : Boolean; -- FPU Busy (for 8087 compatibility only)
53 ES : Boolean; -- Error Summary Status
54 SF : Boolean; -- Stack Fault
56 Top : FPU_Stack_Pointer;
58 -- Condition Code Flags
60 -- C2 is set by FPREM and FPREM1 to indicate incomplete reduction.
61 -- In case of successfull recorction, C0, C3 and C1 are set to the
62 -- three least significant bits of the result (resp. Q2, Q1 and Q0).
64 -- C2 is used by FPTAN, FSIN, FCOS, and FSINCOS to indicate that
65 -- that source operand is beyond the allowable range of
66 -- -2.0**63 .. 2.0**63.
68 C3 : Boolean;
69 C2 : Boolean;
70 C1 : Boolean;
71 C0 : Boolean;
73 -- Exception Flags
75 PE : Boolean; -- Precision
76 UE : Boolean; -- Underflow
77 OE : Boolean; -- Overflow
78 ZE : Boolean; -- Zero Divide
79 DE : Boolean; -- Denormalized Operand
80 IE : Boolean; -- Invalid Operation
81 end record;
83 for FPU_Status_Word use record
84 B at 0 range 15 .. 15;
85 C3 at 0 range 14 .. 14;
86 Top at 0 range 11 .. 13;
87 C2 at 0 range 10 .. 10;
88 C1 at 0 range 9 .. 9;
89 C0 at 0 range 8 .. 8;
90 ES at 0 range 7 .. 7;
91 SF at 0 range 6 .. 6;
92 PE at 0 range 5 .. 5;
93 UE at 0 range 4 .. 4;
94 OE at 0 range 3 .. 3;
95 ZE at 0 range 2 .. 2;
96 DE at 0 range 1 .. 1;
97 IE at 0 range 0 .. 0;
98 end record;
100 for FPU_Status_Word'Size use 16;
102 -----------------------
103 -- Local subprograms --
104 -----------------------
106 function Is_Nan (X : Double) return Boolean;
107 -- Return True iff X is a IEEE NaN value
109 function Logarithmic_Pow (X, Y : Double) return Double;
110 -- Implementation of X**Y using Exp and Log functions (binary base)
111 -- to calculate the exponentiation. This is used by Pow for values
112 -- for values of Y in the open interval (-0.25, 0.25)
114 function Reduce (X : Double) return Double;
115 -- Implement partial reduction of X by Pi in the x86.
117 -- Note that for the Sin, Cos and Tan functions completely accurate
118 -- reduction of the argument is done for arguments in the range of
119 -- -2.0**63 .. 2.0**63, using a 66-bit approximation of Pi.
121 pragma Inline (Is_Nan);
122 pragma Inline (Reduce);
124 ---------------------------------
125 -- Basic Elementary Functions --
126 ---------------------------------
128 -- This section implements a few elementary functions that are
129 -- used to build the more complex ones. This ordering enables
130 -- better inlining.
132 ----------
133 -- Atan --
134 ----------
136 function Atan (X : Double) return Double is
137 Result : Double;
139 begin
140 Asm (Template =>
141 "fld1" & NL
142 & "fpatan",
143 Outputs => Double'Asm_Output ("=t", Result),
144 Inputs => Double'Asm_Input ("0", X));
146 -- The result value is NaN iff input was invalid
148 if not (Result = Result) then
149 raise Argument_Error;
150 end if;
152 return Result;
153 end Atan;
155 ---------
156 -- Exp --
157 ---------
159 function Exp (X : Double) return Double is
160 Result : Double;
161 begin
162 Asm (Template =>
163 "fldl2e " & NL
164 & "fmulp %%st, %%st(1)" & NL -- X * log2 (E)
165 & "fld %%st(0) " & NL
166 & "frndint " & NL -- Integer (X * Log2 (E))
167 & "fsubr %%st, %%st(1)" & NL -- Fraction (X * Log2 (E))
168 & "fxch " & NL
169 & "f2xm1 " & NL -- 2**(...) - 1
170 & "fld1 " & NL
171 & "faddp %%st, %%st(1)" & NL -- 2**(Fraction (X * Log2 (E)))
172 & "fscale " & NL -- E ** X
173 & "fstp %%st(1) ",
174 Outputs => Double'Asm_Output ("=t", Result),
175 Inputs => Double'Asm_Input ("0", X));
176 return Result;
177 end Exp;
179 ------------
180 -- Is_Nan --
181 ------------
183 function Is_Nan (X : Double) return Boolean is
184 begin
185 -- The IEEE NaN values are the only ones that do not equal themselves
187 return not (X = X);
188 end Is_Nan;
190 ---------
191 -- Log --
192 ---------
194 function Log (X : Double) return Double is
195 Result : Double;
197 begin
198 Asm (Template =>
199 "fldln2 " & NL
200 & "fxch " & NL
201 & "fyl2x " & NL,
202 Outputs => Double'Asm_Output ("=t", Result),
203 Inputs => Double'Asm_Input ("0", X));
204 return Result;
205 end Log;
207 ------------
208 -- Reduce --
209 ------------
211 function Reduce (X : Double) return Double is
212 Result : Double;
213 begin
215 (Template =>
216 -- Partial argument reduction
217 "fldpi " & NL
218 & "fadd %%st(0), %%st" & NL
219 & "fxch %%st(1) " & NL
220 & "fprem1 " & NL
221 & "fstp %%st(1) ",
222 Outputs => Double'Asm_Output ("=t", Result),
223 Inputs => Double'Asm_Input ("0", X));
224 return Result;
225 end Reduce;
227 ----------
228 -- Sqrt --
229 ----------
231 function Sqrt (X : Double) return Double is
232 Result : Double;
234 begin
235 if X < 0.0 then
236 raise Argument_Error;
237 end if;
239 Asm (Template => "fsqrt",
240 Outputs => Double'Asm_Output ("=t", Result),
241 Inputs => Double'Asm_Input ("0", X));
243 return Result;
244 end Sqrt;
246 ---------------------------------
247 -- Other Elementary Functions --
248 ---------------------------------
250 -- These are built using the previously implemented basic functions
252 ----------
253 -- Acos --
254 ----------
256 function Acos (X : Double) return Double is
257 Result : Double;
258 begin
259 Result := 2.0 * Atan (Sqrt ((1.0 - X) / (1.0 + X)));
261 -- The result value is NaN iff input was invalid
263 if Is_Nan (Result) then
264 raise Argument_Error;
265 end if;
267 return Result;
268 end Acos;
270 ----------
271 -- Asin --
272 ----------
274 function Asin (X : Double) return Double is
275 Result : Double;
276 begin
278 Result := Atan (X / Sqrt ((1.0 - X) * (1.0 + X)));
280 -- The result value is NaN iff input was invalid
282 if Is_Nan (Result) then
283 raise Argument_Error;
284 end if;
286 return Result;
287 end Asin;
289 ---------
290 -- Cos --
291 ---------
293 function Cos (X : Double) return Double is
294 Reduced_X : Double := X;
295 Result : Double;
296 Status : FPU_Status_Word;
298 begin
300 loop
302 (Template =>
303 "fcos " & NL
304 & "xorl %%eax, %%eax " & NL
305 & "fnstsw %%ax ",
306 Outputs => (Double'Asm_Output ("=t", Result),
307 FPU_Status_Word'Asm_Output ("=a", Status)),
308 Inputs => Double'Asm_Input ("0", Reduced_X));
310 exit when not Status.C2;
312 -- Original argument was not in range and the result
313 -- is the unmodified argument.
315 Reduced_X := Reduce (Result);
316 end loop;
318 return Result;
319 end Cos;
321 ---------------------
322 -- Logarithmic_Pow --
323 ---------------------
325 function Logarithmic_Pow (X, Y : Double) return Double is
326 Result : Double;
328 begin
329 Asm (Template => "" -- X : Y
330 & "fyl2x " & NL -- Y * Log2 (X)
331 & "fst %%st(1) " & NL -- Y * Log2 (X) : Y * Log2 (X)
332 & "frndint " & NL -- Int (...) : Y * Log2 (X)
333 & "fsubr %%st, %%st(1)" & NL -- Int (...) : Fract (...)
334 & "fxch " & NL -- Fract (...) : Int (...)
335 & "f2xm1 " & NL -- 2**Fract (...) - 1 : Int (...)
336 & "fld1 " & NL -- 1 : 2**Fract (...) - 1 : Int (...)
337 & "faddp %%st, %%st(1)" & NL -- 2**Fract (...) : Int (...)
338 & "fscale " & NL -- 2**(Fract (...) + Int (...))
339 & "fstp %%st(1) ",
340 Outputs => Double'Asm_Output ("=t", Result),
341 Inputs =>
342 (Double'Asm_Input ("0", X),
343 Double'Asm_Input ("u", Y)));
345 return Result;
346 end Logarithmic_Pow;
348 ---------
349 -- Pow --
350 ---------
352 function Pow (X, Y : Double) return Double is
353 type Mantissa_Type is mod 2**Double'Machine_Mantissa;
354 -- Modular type that can hold all bits of the mantissa of Double
356 -- For negative exponents, a division is done
357 -- at the end of the processing.
359 Negative_Y : constant Boolean := Y < 0.0;
360 Abs_Y : constant Double := abs Y;
362 -- During this function the following invariant is kept:
363 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
365 Base : Double := X;
367 Exp_High : Double := Double'Floor (Abs_Y);
368 Exp_Mid : Double;
369 Exp_Low : Double;
370 Exp_Int : Mantissa_Type;
372 Factor : Double := 1.0;
374 begin
375 -- Select algorithm for calculating Pow:
376 -- integer cases fall through
378 if Exp_High >= 2.0**Double'Machine_Mantissa then
380 -- In case of Y that is IEEE infinity, just raise constraint error
382 if Exp_High > Double'Safe_Last then
383 raise Constraint_Error;
384 end if;
386 -- Large values of Y are even integers and will stay integer
387 -- after division by two.
389 loop
390 -- Exp_Mid and Exp_Low are zero, so
391 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
393 Exp_High := Exp_High / 2.0;
394 Base := Base * Base;
395 exit when Exp_High < 2.0**Double'Machine_Mantissa;
396 end loop;
398 elsif Exp_High /= Abs_Y then
399 Exp_Low := Abs_Y - Exp_High;
401 Factor := 1.0;
403 if Exp_Low /= 0.0 then
405 -- Exp_Low now is in interval (0.0, 1.0)
406 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
408 Exp_Mid := 0.0;
409 Exp_Low := Exp_Low - Exp_Mid;
411 if Exp_Low >= 0.5 then
412 Factor := Sqrt (X);
413 Exp_Low := Exp_Low - 0.5; -- exact
415 if Exp_Low >= 0.25 then
416 Factor := Factor * Sqrt (Factor);
417 Exp_Low := Exp_Low - 0.25; -- exact
418 end if;
420 elsif Exp_Low >= 0.25 then
421 Factor := Sqrt (Sqrt (X));
422 Exp_Low := Exp_Low - 0.25; -- exact
423 end if;
425 -- Exp_Low now is in interval (0.0, 0.25)
427 -- This means it is safe to call Logarithmic_Pow
428 -- for the remaining part.
430 Factor := Factor * Logarithmic_Pow (X, Exp_Low);
431 end if;
433 elsif X = 0.0 then
434 return 0.0;
435 end if;
437 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
439 Exp_Int := Mantissa_Type (Exp_High);
441 -- Standard way for processing integer powers > 0
443 while Exp_Int > 1 loop
444 if (Exp_Int and 1) = 1 then
446 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
448 Factor := Factor * Base;
449 end if;
451 -- Exp_Int is even and Exp_Int > 0, so
452 -- Base**Y = (Base**2)**(Exp_Int / 2)
454 Base := Base * Base;
455 Exp_Int := Exp_Int / 2;
456 end loop;
458 -- Exp_Int = 1 or Exp_Int = 0
460 if Exp_Int = 1 then
461 Factor := Base * Factor;
462 end if;
464 if Negative_Y then
465 Factor := 1.0 / Factor;
466 end if;
468 return Factor;
469 end Pow;
471 ---------
472 -- Sin --
473 ---------
475 function Sin (X : Double) return Double is
476 Reduced_X : Double := X;
477 Result : Double;
478 Status : FPU_Status_Word;
480 begin
482 loop
484 (Template =>
485 "fsin " & NL
486 & "xorl %%eax, %%eax " & NL
487 & "fnstsw %%ax ",
488 Outputs => (Double'Asm_Output ("=t", Result),
489 FPU_Status_Word'Asm_Output ("=a", Status)),
490 Inputs => Double'Asm_Input ("0", Reduced_X));
492 exit when not Status.C2;
494 -- Original argument was not in range and the result
495 -- is the unmodified argument.
497 Reduced_X := Reduce (Result);
498 end loop;
500 return Result;
501 end Sin;
503 ---------
504 -- Tan --
505 ---------
507 function Tan (X : Double) return Double is
508 Reduced_X : Double := X;
509 Result : Double;
510 Status : FPU_Status_Word;
512 begin
514 loop
516 (Template =>
517 "fptan " & NL
518 & "xorl %%eax, %%eax " & NL
519 & "fnstsw %%ax " & NL
520 & "ffree %%st(0) " & NL
521 & "fincstp ",
523 Outputs => (Double'Asm_Output ("=t", Result),
524 FPU_Status_Word'Asm_Output ("=a", Status)),
525 Inputs => Double'Asm_Input ("0", Reduced_X));
527 exit when not Status.C2;
529 -- Original argument was not in range and the result
530 -- is the unmodified argument.
532 Reduced_X := Reduce (Result);
533 end loop;
535 return Result;
536 end Tan;
538 ----------
539 -- Sinh --
540 ----------
542 function Sinh (X : Double) return Double is
543 begin
544 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
546 if abs X < 25.0 then
547 return (Exp (X) - Exp (-X)) / 2.0;
549 else
550 return Exp (X) / 2.0;
551 end if;
553 end Sinh;
555 ----------
556 -- Cosh --
557 ----------
559 function Cosh (X : Double) return Double is
560 begin
561 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
563 if abs X < 22.0 then
564 return (Exp (X) + Exp (-X)) / 2.0;
566 else
567 return Exp (X) / 2.0;
568 end if;
570 end Cosh;
572 ----------
573 -- Tanh --
574 ----------
576 function Tanh (X : Double) return Double is
577 begin
578 -- Return the Hyperbolic Tangent of x
580 -- x -x
581 -- e - e Sinh (X)
582 -- Tanh (X) is defined to be ----------- = --------
583 -- x -x Cosh (X)
584 -- e + e
586 if abs X > 23.0 then
587 return Double'Copy_Sign (1.0, X);
588 end if;
590 return 1.0 / (1.0 + Exp (-2.0 * X)) - 1.0 / (1.0 + Exp (2.0 * X));
592 end Tanh;
594 end Ada.Numerics.Aux;