1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . N U M E R I C S . A U X --
8 -- (Machine Version for x86) --
10 -- Copyright (C) 1998-2007, Free Software Foundation, Inc. --
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. --
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. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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.
76 function Atan (X : Double) return Double is
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
99 function Exp (X : Double) return Double is
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))
109 & "f2xm1 " & NL -- 2**(...) - 1
111 & "faddp %%st, %%st(1)" & NL -- 2**(Fraction (X * Log2 (E)))
112 & "fscale " & NL -- E ** X
114 Outputs => Double'Asm_Output ("=t", Result),
115 Inputs => Double'Asm_Input ("0", X));
123 function Is_Nan (X : Double) return Boolean is
125 -- The IEEE NaN values are the only ones that do not equal themselves
134 function Log (X : Double) return Double is
142 Outputs => Double'Asm_Output ("=t", Result),
143 Inputs => Double'Asm_Input ("0", X));
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
163 P6 : constant Double := Double'Model (Half_Pi - P1 - P2 - P3 - P4 - P5);
164 K : Double := X * Two_Over_Pi;
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;
181 -- K is not a number, because X was not finite
183 raise Constraint_Error;
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;
196 function Sqrt (X : Double) return Double is
201 raise Argument_Error;
204 Asm (Template => "fsqrt",
205 Outputs => Double'Asm_Output ("=t", Result),
206 Inputs => Double'Asm_Input ("0", X));
211 --------------------------------
212 -- Other Elementary Functions --
213 --------------------------------
215 -- These are built using the previously implemented basic functions
221 function Acos (X : Double) return Double is
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;
240 function Asin (X : Double) return Double is
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;
259 function Cos (X : Double) return Double is
260 Reduced_X : Double := abs X;
262 Quadrant : Natural range 0 .. 3;
265 if Reduced_X > Pi / 4.0 then
266 Reduce (Reduced_X, Quadrant);
270 Asm (Template => "fcos",
271 Outputs => Double'Asm_Output ("=t", Result),
272 Inputs => Double'Asm_Input ("0", Reduced_X));
274 Asm (Template => "fsin",
275 Outputs => Double'Asm_Output ("=t", Result),
276 Inputs => Double'Asm_Input ("0", -Reduced_X));
278 Asm (Template => "fcos ; fchs",
279 Outputs => Double'Asm_Output ("=t", Result),
280 Inputs => Double'Asm_Input ("0", Reduced_X));
282 Asm (Template => "fsin",
283 Outputs => Double'Asm_Output ("=t", Result),
284 Inputs => Double'Asm_Input ("0", Reduced_X));
288 Asm (Template => "fcos",
289 Outputs => Double'Asm_Output ("=t", Result),
290 Inputs => Double'Asm_Input ("0", Reduced_X));
296 ---------------------
297 -- Logarithmic_Pow --
298 ---------------------
300 function Logarithmic_Pow (X, Y : Double) return Double is
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),
315 (Double'Asm_Input ("0", X),
316 Double'Asm_Input ("u", Y)));
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
338 Exp_High : Double := Double'Floor (Abs_Y);
341 Exp_Int : Mantissa_Type;
343 Factor : Double := 1.0;
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;
356 -- Large values of Y are even integers and will stay integer
357 -- after division by two.
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;
365 exit when Exp_High < 2.0**Double'Machine_Mantissa;
368 elsif Exp_High /= Abs_Y then
369 Exp_Low := Abs_Y - Exp_High;
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;
378 Exp_Low := Exp_Low - Exp_Mid;
380 if Exp_Low >= 0.5 then
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
389 elsif Exp_Low >= 0.25 then
390 Factor := Sqrt (Sqrt (X));
391 Exp_Low := Exp_Low - 0.25; -- exact
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);
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;
420 -- Exp_Int is even and Exp_Int > 0, so
421 -- Base**Y = (Base**2)**(Exp_Int / 2)
424 Exp_Int := Exp_Int / 2;
427 -- Exp_Int = 1 or Exp_Int = 0
430 Factor := Base * Factor;
434 Factor := 1.0 / Factor;
444 function Sin (X : Double) return Double is
445 Reduced_X : Double := X;
447 Quadrant : Natural range 0 .. 3;
450 if abs X > Pi / 4.0 then
451 Reduce (Reduced_X, Quadrant);
455 Asm (Template => "fsin",
456 Outputs => Double'Asm_Output ("=t", Result),
457 Inputs => Double'Asm_Input ("0", Reduced_X));
459 Asm (Template => "fcos",
460 Outputs => Double'Asm_Output ("=t", Result),
461 Inputs => Double'Asm_Input ("0", Reduced_X));
463 Asm (Template => "fsin",
464 Outputs => Double'Asm_Output ("=t", Result),
465 Inputs => Double'Asm_Input ("0", -Reduced_X));
467 Asm (Template => "fcos ; fchs",
468 Outputs => Double'Asm_Output ("=t", Result),
469 Inputs => Double'Asm_Input ("0", Reduced_X));
473 Asm (Template => "fsin",
474 Outputs => Double'Asm_Output ("=t", Result),
475 Inputs => Double'Asm_Input ("0", Reduced_X));
485 function Tan (X : Double) return Double is
486 Reduced_X : Double := X;
488 Quadrant : Natural range 0 .. 3;
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
498 Outputs => Double'Asm_Output ("=t", Result),
499 Inputs => Double'Asm_Input ("0", Reduced_X));
501 Asm (Template => "fsincos" & NL
502 & "fdivp %%st, %%st(1)" & NL
504 Outputs => Double'Asm_Output ("=t", Result),
505 Inputs => Double'Asm_Input ("0", Reduced_X));
511 & "ffree %%st(0) " & NL
513 Outputs => Double'Asm_Output ("=t", Result),
514 Inputs => Double'Asm_Input ("0", Reduced_X));
524 function Sinh (X : Double) return Double is
526 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
529 return (Exp (X) - Exp (-X)) / 2.0;
531 return Exp (X) / 2.0;
539 function Cosh (X : Double) return Double is
541 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
544 return (Exp (X) + Exp (-X)) / 2.0;
546 return Exp (X) / 2.0;
554 function Tanh (X : Double) return Double is
556 -- Return the Hyperbolic Tangent of x
560 -- Tanh (X) is defined to be ----------- = --------
565 return Double'Copy_Sign (1.0, X);
568 return 1.0 / (1.0 + Exp (-(2.0 * X))) - 1.0 / (1.0 + Exp (2.0 * X));
571 end Ada.Numerics.Aux;