* sh.h (REG_CLASS_FROM_LETTER): Change to:
[official-gcc.git] / gcc / ada / 86numaux.adb
bloba13733305a1a37b0ac8fad9d71aa49bba9557459
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 -- Copyright (C) 1998-2001 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, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
35 -- File a-numaux.adb <- 86numaux.adb
37 -- This version of Numerics.Aux is for the IEEE Double Extended floating
38 -- point format on x86.
40 with System.Machine_Code; use System.Machine_Code;
42 package body Ada.Numerics.Aux is
44 NL : constant String := ASCII.LF & ASCII.HT;
46 type FPU_Stack_Pointer is range 0 .. 7;
47 for FPU_Stack_Pointer'Size use 3;
49 type FPU_Status_Word is record
50 B : Boolean; -- FPU Busy (for 8087 compatibility only)
51 ES : Boolean; -- Error Summary Status
52 SF : Boolean; -- Stack Fault
54 Top : FPU_Stack_Pointer;
56 -- Condition Code Flags
58 -- C2 is set by FPREM and FPREM1 to indicate incomplete reduction.
59 -- In case of successfull recorction, C0, C3 and C1 are set to the
60 -- three least significant bits of the result (resp. Q2, Q1 and Q0).
62 -- C2 is used by FPTAN, FSIN, FCOS, and FSINCOS to indicate that
63 -- that source operand is beyond the allowable range of
64 -- -2.0**63 .. 2.0**63.
66 C3 : Boolean;
67 C2 : Boolean;
68 C1 : Boolean;
69 C0 : Boolean;
71 -- Exception Flags
73 PE : Boolean; -- Precision
74 UE : Boolean; -- Underflow
75 OE : Boolean; -- Overflow
76 ZE : Boolean; -- Zero Divide
77 DE : Boolean; -- Denormalized Operand
78 IE : Boolean; -- Invalid Operation
79 end record;
81 for FPU_Status_Word use record
82 B at 0 range 15 .. 15;
83 C3 at 0 range 14 .. 14;
84 Top at 0 range 11 .. 13;
85 C2 at 0 range 10 .. 10;
86 C1 at 0 range 9 .. 9;
87 C0 at 0 range 8 .. 8;
88 ES at 0 range 7 .. 7;
89 SF at 0 range 6 .. 6;
90 PE at 0 range 5 .. 5;
91 UE at 0 range 4 .. 4;
92 OE at 0 range 3 .. 3;
93 ZE at 0 range 2 .. 2;
94 DE at 0 range 1 .. 1;
95 IE at 0 range 0 .. 0;
96 end record;
98 for FPU_Status_Word'Size use 16;
100 -----------------------
101 -- Local subprograms --
102 -----------------------
104 function Is_Nan (X : Double) return Boolean;
105 -- Return True iff X is a IEEE NaN value
107 function Logarithmic_Pow (X, Y : Double) return Double;
108 -- Implementation of X**Y using Exp and Log functions (binary base)
109 -- to calculate the exponentiation. This is used by Pow for values
110 -- for values of Y in the open interval (-0.25, 0.25)
112 function Reduce (X : Double) return Double;
113 -- Implement partial reduction of X by Pi in the x86.
115 -- Note that for the Sin, Cos and Tan functions completely accurate
116 -- reduction of the argument is done for arguments in the range of
117 -- -2.0**63 .. 2.0**63, using a 66-bit approximation of Pi.
119 pragma Inline (Is_Nan);
120 pragma Inline (Reduce);
122 ---------------------------------
123 -- Basic Elementary Functions --
124 ---------------------------------
126 -- This section implements a few elementary functions that are
127 -- used to build the more complex ones. This ordering enables
128 -- better inlining.
130 ----------
131 -- Atan --
132 ----------
134 function Atan (X : Double) return Double is
135 Result : Double;
137 begin
138 Asm (Template =>
139 "fld1" & NL
140 & "fpatan",
141 Outputs => Double'Asm_Output ("=t", Result),
142 Inputs => Double'Asm_Input ("0", X));
144 -- The result value is NaN iff input was invalid
146 if not (Result = Result) then
147 raise Argument_Error;
148 end if;
150 return Result;
151 end Atan;
153 ---------
154 -- Exp --
155 ---------
157 function Exp (X : Double) return Double is
158 Result : Double;
159 begin
160 Asm (Template =>
161 "fldl2e " & NL
162 & "fmulp %%st, %%st(1)" & NL -- X * log2 (E)
163 & "fld %%st(0) " & NL
164 & "frndint " & NL -- Integer (X * Log2 (E))
165 & "fsubr %%st, %%st(1)" & NL -- Fraction (X * Log2 (E))
166 & "fxch " & NL
167 & "f2xm1 " & NL -- 2**(...) - 1
168 & "fld1 " & NL
169 & "faddp %%st, %%st(1)" & NL -- 2**(Fraction (X * Log2 (E)))
170 & "fscale " & NL -- E ** X
171 & "fstp %%st(1) ",
172 Outputs => Double'Asm_Output ("=t", Result),
173 Inputs => Double'Asm_Input ("0", X));
174 return Result;
175 end Exp;
177 ------------
178 -- Is_Nan --
179 ------------
181 function Is_Nan (X : Double) return Boolean is
182 begin
183 -- The IEEE NaN values are the only ones that do not equal themselves
185 return not (X = X);
186 end Is_Nan;
188 ---------
189 -- Log --
190 ---------
192 function Log (X : Double) return Double is
193 Result : Double;
195 begin
196 Asm (Template =>
197 "fldln2 " & NL
198 & "fxch " & NL
199 & "fyl2x " & NL,
200 Outputs => Double'Asm_Output ("=t", Result),
201 Inputs => Double'Asm_Input ("0", X));
202 return Result;
203 end Log;
205 ------------
206 -- Reduce --
207 ------------
209 function Reduce (X : Double) return Double is
210 Result : Double;
211 begin
213 (Template =>
214 -- Partial argument reduction
215 "fldpi " & NL
216 & "fadd %%st(0), %%st" & NL
217 & "fxch %%st(1) " & NL
218 & "fprem1 " & NL
219 & "fstp %%st(1) ",
220 Outputs => Double'Asm_Output ("=t", Result),
221 Inputs => Double'Asm_Input ("0", X));
222 return Result;
223 end Reduce;
225 ----------
226 -- Sqrt --
227 ----------
229 function Sqrt (X : Double) return Double is
230 Result : Double;
232 begin
233 if X < 0.0 then
234 raise Argument_Error;
235 end if;
237 Asm (Template => "fsqrt",
238 Outputs => Double'Asm_Output ("=t", Result),
239 Inputs => Double'Asm_Input ("0", X));
241 return Result;
242 end Sqrt;
244 ---------------------------------
245 -- Other Elementary Functions --
246 ---------------------------------
248 -- These are built using the previously implemented basic functions
250 ----------
251 -- Acos --
252 ----------
254 function Acos (X : Double) return Double is
255 Result : Double;
256 begin
257 Result := 2.0 * Atan (Sqrt ((1.0 - X) / (1.0 + X)));
259 -- The result value is NaN iff input was invalid
261 if Is_Nan (Result) then
262 raise Argument_Error;
263 end if;
265 return Result;
266 end Acos;
268 ----------
269 -- Asin --
270 ----------
272 function Asin (X : Double) return Double is
273 Result : Double;
274 begin
276 Result := Atan (X / Sqrt ((1.0 - X) * (1.0 + X)));
278 -- The result value is NaN iff input was invalid
280 if Is_Nan (Result) then
281 raise Argument_Error;
282 end if;
284 return Result;
285 end Asin;
287 ---------
288 -- Cos --
289 ---------
291 function Cos (X : Double) return Double is
292 Reduced_X : Double := X;
293 Result : Double;
294 Status : FPU_Status_Word;
296 begin
298 loop
300 (Template =>
301 "fcos " & NL
302 & "xorl %%eax, %%eax " & NL
303 & "fnstsw %%ax ",
304 Outputs => (Double'Asm_Output ("=t", Result),
305 FPU_Status_Word'Asm_Output ("=a", Status)),
306 Inputs => Double'Asm_Input ("0", Reduced_X));
308 exit when not Status.C2;
310 -- Original argument was not in range and the result
311 -- is the unmodified argument.
313 Reduced_X := Reduce (Result);
314 end loop;
316 return Result;
317 end Cos;
319 ---------------------
320 -- Logarithmic_Pow --
321 ---------------------
323 function Logarithmic_Pow (X, Y : Double) return Double is
324 Result : Double;
326 begin
327 Asm (Template => "" -- X : Y
328 & "fyl2x " & NL -- Y * Log2 (X)
329 & "fst %%st(1) " & NL -- Y * Log2 (X) : Y * Log2 (X)
330 & "frndint " & NL -- Int (...) : Y * Log2 (X)
331 & "fsubr %%st, %%st(1)" & NL -- Int (...) : Fract (...)
332 & "fxch " & NL -- Fract (...) : Int (...)
333 & "f2xm1 " & NL -- 2**Fract (...) - 1 : Int (...)
334 & "fld1 " & NL -- 1 : 2**Fract (...) - 1 : Int (...)
335 & "faddp %%st, %%st(1)" & NL -- 2**Fract (...) : Int (...)
336 & "fscale " & NL -- 2**(Fract (...) + Int (...))
337 & "fstp %%st(1) ",
338 Outputs => Double'Asm_Output ("=t", Result),
339 Inputs =>
340 (Double'Asm_Input ("0", X),
341 Double'Asm_Input ("u", Y)));
343 return Result;
344 end Logarithmic_Pow;
346 ---------
347 -- Pow --
348 ---------
350 function Pow (X, Y : Double) return Double is
351 type Mantissa_Type is mod 2**Double'Machine_Mantissa;
352 -- Modular type that can hold all bits of the mantissa of Double
354 -- For negative exponents, a division is done
355 -- at the end of the processing.
357 Negative_Y : constant Boolean := Y < 0.0;
358 Abs_Y : constant Double := abs Y;
360 -- During this function the following invariant is kept:
361 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
363 Base : Double := X;
365 Exp_High : Double := Double'Floor (Abs_Y);
366 Exp_Mid : Double;
367 Exp_Low : Double;
368 Exp_Int : Mantissa_Type;
370 Factor : Double := 1.0;
372 begin
373 -- Select algorithm for calculating Pow:
374 -- integer cases fall through
376 if Exp_High >= 2.0**Double'Machine_Mantissa then
378 -- In case of Y that is IEEE infinity, just raise constraint error
380 if Exp_High > Double'Safe_Last then
381 raise Constraint_Error;
382 end if;
384 -- Large values of Y are even integers and will stay integer
385 -- after division by two.
387 loop
388 -- Exp_Mid and Exp_Low are zero, so
389 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
391 Exp_High := Exp_High / 2.0;
392 Base := Base * Base;
393 exit when Exp_High < 2.0**Double'Machine_Mantissa;
394 end loop;
396 elsif Exp_High /= Abs_Y then
397 Exp_Low := Abs_Y - Exp_High;
399 Factor := 1.0;
401 if Exp_Low /= 0.0 then
403 -- Exp_Low now is in interval (0.0, 1.0)
404 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
406 Exp_Mid := 0.0;
407 Exp_Low := Exp_Low - Exp_Mid;
409 if Exp_Low >= 0.5 then
410 Factor := Sqrt (X);
411 Exp_Low := Exp_Low - 0.5; -- exact
413 if Exp_Low >= 0.25 then
414 Factor := Factor * Sqrt (Factor);
415 Exp_Low := Exp_Low - 0.25; -- exact
416 end if;
418 elsif Exp_Low >= 0.25 then
419 Factor := Sqrt (Sqrt (X));
420 Exp_Low := Exp_Low - 0.25; -- exact
421 end if;
423 -- Exp_Low now is in interval (0.0, 0.25)
425 -- This means it is safe to call Logarithmic_Pow
426 -- for the remaining part.
428 Factor := Factor * Logarithmic_Pow (X, Exp_Low);
429 end if;
431 elsif X = 0.0 then
432 return 0.0;
433 end if;
435 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
437 Exp_Int := Mantissa_Type (Exp_High);
439 -- Standard way for processing integer powers > 0
441 while Exp_Int > 1 loop
442 if (Exp_Int and 1) = 1 then
444 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
446 Factor := Factor * Base;
447 end if;
449 -- Exp_Int is even and Exp_Int > 0, so
450 -- Base**Y = (Base**2)**(Exp_Int / 2)
452 Base := Base * Base;
453 Exp_Int := Exp_Int / 2;
454 end loop;
456 -- Exp_Int = 1 or Exp_Int = 0
458 if Exp_Int = 1 then
459 Factor := Base * Factor;
460 end if;
462 if Negative_Y then
463 Factor := 1.0 / Factor;
464 end if;
466 return Factor;
467 end Pow;
469 ---------
470 -- Sin --
471 ---------
473 function Sin (X : Double) return Double is
474 Reduced_X : Double := X;
475 Result : Double;
476 Status : FPU_Status_Word;
478 begin
480 loop
482 (Template =>
483 "fsin " & NL
484 & "xorl %%eax, %%eax " & NL
485 & "fnstsw %%ax ",
486 Outputs => (Double'Asm_Output ("=t", Result),
487 FPU_Status_Word'Asm_Output ("=a", Status)),
488 Inputs => Double'Asm_Input ("0", Reduced_X));
490 exit when not Status.C2;
492 -- Original argument was not in range and the result
493 -- is the unmodified argument.
495 Reduced_X := Reduce (Result);
496 end loop;
498 return Result;
499 end Sin;
501 ---------
502 -- Tan --
503 ---------
505 function Tan (X : Double) return Double is
506 Reduced_X : Double := X;
507 Result : Double;
508 Status : FPU_Status_Word;
510 begin
512 loop
514 (Template =>
515 "fptan " & NL
516 & "xorl %%eax, %%eax " & NL
517 & "fnstsw %%ax " & NL
518 & "ffree %%st(0) " & NL
519 & "fincstp ",
521 Outputs => (Double'Asm_Output ("=t", Result),
522 FPU_Status_Word'Asm_Output ("=a", Status)),
523 Inputs => Double'Asm_Input ("0", Reduced_X));
525 exit when not Status.C2;
527 -- Original argument was not in range and the result
528 -- is the unmodified argument.
530 Reduced_X := Reduce (Result);
531 end loop;
533 return Result;
534 end Tan;
536 ----------
537 -- Sinh --
538 ----------
540 function Sinh (X : Double) return Double is
541 begin
542 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
544 if abs X < 25.0 then
545 return (Exp (X) - Exp (-X)) / 2.0;
547 else
548 return Exp (X) / 2.0;
549 end if;
551 end Sinh;
553 ----------
554 -- Cosh --
555 ----------
557 function Cosh (X : Double) return Double is
558 begin
559 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
561 if abs X < 22.0 then
562 return (Exp (X) + Exp (-X)) / 2.0;
564 else
565 return Exp (X) / 2.0;
566 end if;
568 end Cosh;
570 ----------
571 -- Tanh --
572 ----------
574 function Tanh (X : Double) return Double is
575 begin
576 -- Return the Hyperbolic Tangent of x
578 -- x -x
579 -- e - e Sinh (X)
580 -- Tanh (X) is defined to be ----------- = --------
581 -- x -x Cosh (X)
582 -- e + e
584 if abs X > 23.0 then
585 return Double'Copy_Sign (1.0, X);
586 end if;
588 return 1.0 / (1.0 + Exp (-2.0 * X)) - 1.0 / (1.0 + Exp (2.0 * X));
590 end Tanh;
592 end Ada.Numerics.Aux;