mips.h (set_volatile): Delete.
[official-gcc.git] / gcc / ada / a-numaux-x86.adb
blob7b988262b8134b611b9388444392aab0405a5511
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 ------------------------------------------------------------------------------
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 -----------------------
47 -- Local subprograms --
48 -----------------------
50 function Is_Nan (X : Double) return Boolean;
51 -- Return True iff X is a IEEE NaN value
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)
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.
62 pragma Inline (Is_Nan);
63 pragma Inline (Reduce);
65 --------------------------------
66 -- Basic Elementary Functions --
67 --------------------------------
69 -- This section implements a few elementary functions that are used to
70 -- build the more complex ones. This ordering enables better inlining.
72 ----------
73 -- Atan --
74 ----------
76 function Atan (X : Double) return Double is
77 Result : Double;
79 begin
80 Asm (Template =>
81 "fld1" & NL
82 & "fpatan",
83 Outputs => Double'Asm_Output ("=t", Result),
84 Inputs => Double'Asm_Input ("0", X));
86 -- The result value is NaN iff input was invalid
88 if not (Result = Result) then
89 raise Argument_Error;
90 end if;
92 return Result;
93 end Atan;
95 ---------
96 -- Exp --
97 ---------
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;
119 ------------
120 -- Is_Nan --
121 ------------
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
127 return not (X = X);
128 end Is_Nan;
130 ---------
131 -- Log --
132 ---------
134 function Log (X : Double) return Double is
135 Result : Double;
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;
147 ------------
148 -- Reduce --
149 ------------
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;
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).
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;
179 if K /= K then
181 -- K is not a number, because X was not finite
183 raise Constraint_Error;
184 end if;
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;
192 ----------
193 -- Sqrt --
194 ----------
196 function Sqrt (X : Double) return Double is
197 Result : Double;
199 begin
200 if X < 0.0 then
201 raise Argument_Error;
202 end if;
204 Asm (Template => "fsqrt",
205 Outputs => Double'Asm_Output ("=t", Result),
206 Inputs => Double'Asm_Input ("0", X));
208 return Result;
209 end Sqrt;
211 --------------------------------
212 -- Other Elementary Functions --
213 --------------------------------
215 -- These are built using the previously implemented basic functions
217 ----------
218 -- Acos --
219 ----------
221 function Acos (X : Double) return Double is
222 Result : Double;
224 begin
225 Result := 2.0 * Atan (Sqrt ((1.0 - X) / (1.0 + X)));
227 -- The result value is NaN iff input was invalid
229 if Is_Nan (Result) then
230 raise Argument_Error;
231 end if;
233 return Result;
234 end Acos;
236 ----------
237 -- Asin --
238 ----------
240 function Asin (X : Double) return Double is
241 Result : Double;
243 begin
244 Result := Atan (X / Sqrt ((1.0 - X) * (1.0 + X)));
246 -- The result value is NaN iff input was invalid
248 if Is_Nan (Result) then
249 raise Argument_Error;
250 end if;
252 return Result;
253 end Asin;
255 ---------
256 -- Cos --
257 ---------
259 function Cos (X : Double) return Double is
260 Reduced_X : Double := abs X;
261 Result : Double;
262 Quadrant : Natural range 0 .. 3;
264 begin
265 if Reduced_X > Pi / 4.0 then
266 Reduce (Reduced_X, Quadrant);
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;
287 else
288 Asm (Template => "fcos",
289 Outputs => Double'Asm_Output ("=t", Result),
290 Inputs => Double'Asm_Input ("0", Reduced_X));
291 end if;
293 return Result;
294 end Cos;
296 ---------------------
297 -- Logarithmic_Pow --
298 ---------------------
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;
320 ---------
321 -- Pow --
322 ---------
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
328 -- For negative exponents, do divide at the end of the processing
330 Negative_Y : constant Boolean := Y < 0.0;
331 Abs_Y : constant Double := abs Y;
333 -- During this function the following invariant is kept:
334 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
336 Base : Double := X;
338 Exp_High : Double := Double'Floor (Abs_Y);
339 Exp_Mid : Double;
340 Exp_Low : Double;
341 Exp_Int : Mantissa_Type;
343 Factor : Double := 1.0;
345 begin
346 -- Select algorithm for calculating Pow (integer cases fall through)
348 if Exp_High >= 2.0**Double'Machine_Mantissa then
350 -- In case of Y that is IEEE infinity, just raise constraint error
352 if Exp_High > Double'Safe_Last then
353 raise Constraint_Error;
354 end if;
356 -- Large values of Y are even integers and will stay integer
357 -- after division by two.
359 loop
360 -- Exp_Mid and Exp_Low are zero, so
361 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
363 Exp_High := Exp_High / 2.0;
364 Base := Base * Base;
365 exit when Exp_High < 2.0**Double'Machine_Mantissa;
366 end loop;
368 elsif Exp_High /= Abs_Y then
369 Exp_Low := Abs_Y - Exp_High;
370 Factor := 1.0;
372 if Exp_Low /= 0.0 then
374 -- Exp_Low now is in interval (0.0, 1.0)
375 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
377 Exp_Mid := 0.0;
378 Exp_Low := Exp_Low - Exp_Mid;
380 if Exp_Low >= 0.5 then
381 Factor := Sqrt (X);
382 Exp_Low := Exp_Low - 0.5; -- exact
384 if Exp_Low >= 0.25 then
385 Factor := Factor * Sqrt (Factor);
386 Exp_Low := Exp_Low - 0.25; -- exact
387 end if;
389 elsif Exp_Low >= 0.25 then
390 Factor := Sqrt (Sqrt (X));
391 Exp_Low := Exp_Low - 0.25; -- exact
392 end if;
394 -- Exp_Low now is in interval (0.0, 0.25)
396 -- This means it is safe to call Logarithmic_Pow
397 -- for the remaining part.
399 Factor := Factor * Logarithmic_Pow (X, Exp_Low);
400 end if;
402 elsif X = 0.0 then
403 return 0.0;
404 end if;
406 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
408 Exp_Int := Mantissa_Type (Exp_High);
410 -- Standard way for processing integer powers > 0
412 while Exp_Int > 1 loop
413 if (Exp_Int and 1) = 1 then
415 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
417 Factor := Factor * Base;
418 end if;
420 -- Exp_Int is even and Exp_Int > 0, so
421 -- Base**Y = (Base**2)**(Exp_Int / 2)
423 Base := Base * Base;
424 Exp_Int := Exp_Int / 2;
425 end loop;
427 -- Exp_Int = 1 or Exp_Int = 0
429 if Exp_Int = 1 then
430 Factor := Base * Factor;
431 end if;
433 if Negative_Y then
434 Factor := 1.0 / Factor;
435 end if;
437 return Factor;
438 end Pow;
440 ---------
441 -- Sin --
442 ---------
444 function Sin (X : Double) return Double is
445 Reduced_X : Double := X;
446 Result : Double;
447 Quadrant : Natural range 0 .. 3;
449 begin
450 if abs X > Pi / 4.0 then
451 Reduce (Reduced_X, Quadrant);
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;
472 else
473 Asm (Template => "fsin",
474 Outputs => Double'Asm_Output ("=t", Result),
475 Inputs => Double'Asm_Input ("0", Reduced_X));
476 end if;
478 return Result;
479 end Sin;
481 ---------
482 -- Tan --
483 ---------
485 function Tan (X : Double) return Double is
486 Reduced_X : Double := X;
487 Result : Double;
488 Quadrant : Natural range 0 .. 3;
490 begin
491 if abs X > Pi / 4.0 then
492 Reduce (Reduced_X, Quadrant);
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;
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;
517 return Result;
518 end Tan;
520 ----------
521 -- Sinh --
522 ----------
524 function Sinh (X : Double) return Double is
525 begin
526 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
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;
535 ----------
536 -- Cosh --
537 ----------
539 function Cosh (X : Double) return Double is
540 begin
541 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
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;
550 ----------
551 -- Tanh --
552 ----------
554 function Tanh (X : Double) return Double is
555 begin
556 -- Return the Hyperbolic Tangent of x
558 -- x -x
559 -- e - e Sinh (X)
560 -- Tanh (X) is defined to be ----------- = --------
561 -- x -x Cosh (X)
562 -- e + e
564 if abs X > 23.0 then
565 return Double'Copy_Sign (1.0, X);
566 end if;
568 return 1.0 / (1.0 + Exp (-(2.0 * X))) - 1.0 / (1.0 + Exp (2.0 * X));
569 end Tanh;
571 end Ada.Numerics.Aux;