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-2009, 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 3, 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. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNAT was originally developed by the GNAT team at New York University. --
29 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 ------------------------------------------------------------------------------
33 -- File a-numaux.adb <- 86numaux.adb
35 -- This version of Numerics.Aux is for the IEEE Double Extended floating
36 -- point format on x86.
38 with System
.Machine_Code
; use System
.Machine_Code
;
40 package body Ada
.Numerics
.Aux
is
42 NL
: constant String := ASCII
.LF
& ASCII
.HT
;
44 -----------------------
45 -- Local subprograms --
46 -----------------------
48 function Is_Nan
(X
: Double
) return Boolean;
49 -- Return True iff X is a IEEE NaN value
51 function Logarithmic_Pow
(X
, Y
: Double
) return Double
;
52 -- Implementation of X**Y using Exp and Log functions (binary base)
53 -- to calculate the exponentiation. This is used by Pow for values
54 -- for values of Y in the open interval (-0.25, 0.25)
56 procedure Reduce
(X
: in out Double
; Q
: out Natural);
57 -- Implements reduction of X by Pi/2. Q is the quadrant of the final
58 -- result in the range 0 .. 3. The absolute value of X is at most Pi.
60 pragma Inline
(Is_Nan
);
61 pragma Inline
(Reduce
);
63 --------------------------------
64 -- Basic Elementary Functions --
65 --------------------------------
67 -- This section implements a few elementary functions that are used to
68 -- build the more complex ones. This ordering enables better inlining.
74 function Atan
(X
: Double
) return Double
is
81 Outputs
=> Double
'Asm_Output ("=t", Result
),
82 Inputs
=> Double
'Asm_Input ("0", X
));
84 -- The result value is NaN iff input was invalid
86 if not (Result
= Result
) then
97 function Exp
(X
: Double
) return Double
is
102 & "fmulp %%st, %%st(1)" & NL
-- X * log2 (E)
103 & "fld %%st(0) " & NL
104 & "frndint " & NL
-- Integer (X * Log2 (E))
105 & "fsubr %%st, %%st(1)" & NL
-- Fraction (X * Log2 (E))
107 & "f2xm1 " & NL
-- 2**(...) - 1
109 & "faddp %%st, %%st(1)" & NL
-- 2**(Fraction (X * Log2 (E)))
110 & "fscale " & NL
-- E ** X
112 Outputs
=> Double
'Asm_Output ("=t", Result
),
113 Inputs
=> Double
'Asm_Input ("0", X
));
121 function Is_Nan
(X
: Double
) return Boolean is
123 -- The IEEE NaN values are the only ones that do not equal themselves
132 function Log
(X
: Double
) return Double
is
140 Outputs
=> Double
'Asm_Output ("=t", Result
),
141 Inputs
=> Double
'Asm_Input ("0", X
));
149 procedure Reduce
(X
: in out Double
; Q
: out Natural) is
150 Half_Pi
: constant := Pi
/ 2.0;
151 Two_Over_Pi
: constant := 2.0 / Pi
;
153 HM
: constant := Integer'Min (Double
'Machine_Mantissa / 2, Natural'Size);
154 M
: constant Double
:= 0.5 + 2.0**(1 - HM
); -- Splitting constant
155 P1
: constant Double
:= Double
'Leading_Part (Half_Pi
, HM
);
156 P2
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
, HM
);
157 P3
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
, HM
);
158 P4
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
- P3
, HM
);
159 P5
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
- P3
161 P6
: constant Double
:= Double
'Model (Half_Pi
- P1
- P2
- P3
- P4
- P5
);
162 K
: Double
:= X
* Two_Over_Pi
;
164 -- For X < 2.0**32, all products below are computed exactly.
165 -- Due to cancellation effects all subtractions are exact as well.
166 -- As no double extended floating-point number has more than 75
167 -- zeros after the binary point, the result will be the correctly
168 -- rounded result of X - K * (Pi / 2.0).
170 while abs K
>= 2.0**HM
loop
171 K
:= K
* M
- (K
* M
- K
);
172 X
:= (((((X
- K
* P1
) - K
* P2
) - K
* P3
)
173 - K
* P4
) - K
* P5
) - K
* P6
;
174 K
:= X
* Two_Over_Pi
;
179 -- K is not a number, because X was not finite
181 raise Constraint_Error
;
184 K
:= Double
'Rounding (K
);
185 Q
:= Integer (K
) mod 4;
186 X
:= (((((X
- K
* P1
) - K
* P2
) - K
* P3
)
187 - K
* P4
) - K
* P5
) - K
* P6
;
194 function Sqrt
(X
: Double
) return Double
is
199 raise Argument_Error
;
202 Asm
(Template
=> "fsqrt",
203 Outputs
=> Double
'Asm_Output ("=t", Result
),
204 Inputs
=> Double
'Asm_Input ("0", X
));
209 --------------------------------
210 -- Other Elementary Functions --
211 --------------------------------
213 -- These are built using the previously implemented basic functions
219 function Acos
(X
: Double
) return Double
is
223 Result
:= 2.0 * Atan
(Sqrt
((1.0 - X
) / (1.0 + X
)));
225 -- The result value is NaN iff input was invalid
227 if Is_Nan
(Result
) then
228 raise Argument_Error
;
238 function Asin
(X
: Double
) return Double
is
242 Result
:= Atan
(X
/ Sqrt
((1.0 - X
) * (1.0 + X
)));
244 -- The result value is NaN iff input was invalid
246 if Is_Nan
(Result
) then
247 raise Argument_Error
;
257 function Cos
(X
: Double
) return Double
is
258 Reduced_X
: Double
:= abs X
;
260 Quadrant
: Natural range 0 .. 3;
263 if Reduced_X
> Pi
/ 4.0 then
264 Reduce
(Reduced_X
, Quadrant
);
268 Asm
(Template
=> "fcos",
269 Outputs
=> Double
'Asm_Output ("=t", Result
),
270 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
272 Asm
(Template
=> "fsin",
273 Outputs
=> Double
'Asm_Output ("=t", Result
),
274 Inputs
=> Double
'Asm_Input ("0", -Reduced_X
));
276 Asm
(Template
=> "fcos ; fchs",
277 Outputs
=> Double
'Asm_Output ("=t", Result
),
278 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
280 Asm
(Template
=> "fsin",
281 Outputs
=> Double
'Asm_Output ("=t", Result
),
282 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
286 Asm
(Template
=> "fcos",
287 Outputs
=> Double
'Asm_Output ("=t", Result
),
288 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
294 ---------------------
295 -- Logarithmic_Pow --
296 ---------------------
298 function Logarithmic_Pow
(X
, Y
: Double
) return Double
is
301 Asm
(Template
=> "" -- X : Y
302 & "fyl2x " & NL
-- Y * Log2 (X)
303 & "fld %%st(0) " & NL
-- Y * Log2 (X) : Y * Log2 (X)
304 & "frndint " & NL
-- Int (...) : Y * Log2 (X)
305 & "fsubr %%st, %%st(1)" & NL
-- Int (...) : Fract (...)
306 & "fxch " & NL
-- Fract (...) : Int (...)
307 & "f2xm1 " & NL
-- 2**Fract (...) - 1 : Int (...)
308 & "fld1 " & NL
-- 1 : 2**Fract (...) - 1 : Int (...)
309 & "faddp %%st, %%st(1)" & NL
-- 2**Fract (...) : Int (...)
310 & "fscale ", -- 2**(Fract (...) + Int (...))
311 Outputs
=> Double
'Asm_Output ("=t", Result
),
313 (Double
'Asm_Input ("0", X
),
314 Double
'Asm_Input ("u", Y
)));
322 function Pow
(X
, Y
: Double
) return Double
is
323 type Mantissa_Type
is mod 2**Double
'Machine_Mantissa;
324 -- Modular type that can hold all bits of the mantissa of Double
326 -- For negative exponents, do divide at the end of the processing
328 Negative_Y
: constant Boolean := Y
< 0.0;
329 Abs_Y
: constant Double
:= abs Y
;
331 -- During this function the following invariant is kept:
332 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
336 Exp_High
: Double
:= Double
'Floor (Abs_Y
);
339 Exp_Int
: Mantissa_Type
;
341 Factor
: Double
:= 1.0;
344 -- Select algorithm for calculating Pow (integer cases fall through)
346 if Exp_High
>= 2.0**Double
'Machine_Mantissa then
348 -- In case of Y that is IEEE infinity, just raise constraint error
350 if Exp_High
> Double
'Safe_Last then
351 raise Constraint_Error
;
354 -- Large values of Y are even integers and will stay integer
355 -- after division by two.
358 -- Exp_Mid and Exp_Low are zero, so
359 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
361 Exp_High
:= Exp_High
/ 2.0;
363 exit when Exp_High
< 2.0**Double
'Machine_Mantissa;
366 elsif Exp_High
/= Abs_Y
then
367 Exp_Low
:= Abs_Y
- Exp_High
;
370 if Exp_Low
/= 0.0 then
372 -- Exp_Low now is in interval (0.0, 1.0)
373 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
376 Exp_Low
:= Exp_Low
- Exp_Mid
;
378 if Exp_Low
>= 0.5 then
380 Exp_Low
:= Exp_Low
- 0.5; -- exact
382 if Exp_Low
>= 0.25 then
383 Factor
:= Factor
* Sqrt
(Factor
);
384 Exp_Low
:= Exp_Low
- 0.25; -- exact
387 elsif Exp_Low
>= 0.25 then
388 Factor
:= Sqrt
(Sqrt
(X
));
389 Exp_Low
:= Exp_Low
- 0.25; -- exact
392 -- Exp_Low now is in interval (0.0, 0.25)
394 -- This means it is safe to call Logarithmic_Pow
395 -- for the remaining part.
397 Factor
:= Factor
* Logarithmic_Pow
(X
, Exp_Low
);
404 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
406 Exp_Int
:= Mantissa_Type
(Exp_High
);
408 -- Standard way for processing integer powers > 0
410 while Exp_Int
> 1 loop
411 if (Exp_Int
and 1) = 1 then
413 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
415 Factor
:= Factor
* Base
;
418 -- Exp_Int is even and Exp_Int > 0, so
419 -- Base**Y = (Base**2)**(Exp_Int / 2)
422 Exp_Int
:= Exp_Int
/ 2;
425 -- Exp_Int = 1 or Exp_Int = 0
428 Factor
:= Base
* Factor
;
432 Factor
:= 1.0 / Factor
;
442 function Sin
(X
: Double
) return Double
is
443 Reduced_X
: Double
:= X
;
445 Quadrant
: Natural range 0 .. 3;
448 if abs X
> Pi
/ 4.0 then
449 Reduce
(Reduced_X
, Quadrant
);
453 Asm
(Template
=> "fsin",
454 Outputs
=> Double
'Asm_Output ("=t", Result
),
455 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
457 Asm
(Template
=> "fcos",
458 Outputs
=> Double
'Asm_Output ("=t", Result
),
459 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
461 Asm
(Template
=> "fsin",
462 Outputs
=> Double
'Asm_Output ("=t", Result
),
463 Inputs
=> Double
'Asm_Input ("0", -Reduced_X
));
465 Asm
(Template
=> "fcos ; fchs",
466 Outputs
=> Double
'Asm_Output ("=t", Result
),
467 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
471 Asm
(Template
=> "fsin",
472 Outputs
=> Double
'Asm_Output ("=t", Result
),
473 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
483 function Tan
(X
: Double
) return Double
is
484 Reduced_X
: Double
:= X
;
486 Quadrant
: Natural range 0 .. 3;
489 if abs X
> Pi
/ 4.0 then
490 Reduce
(Reduced_X
, Quadrant
);
492 if Quadrant
mod 2 = 0 then
493 Asm
(Template
=> "fptan" & NL
494 & "ffree %%st(0)" & NL
496 Outputs
=> Double
'Asm_Output ("=t", Result
),
497 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
499 Asm
(Template
=> "fsincos" & NL
500 & "fdivp %%st, %%st(1)" & NL
502 Outputs
=> Double
'Asm_Output ("=t", Result
),
503 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
509 & "ffree %%st(0) " & NL
511 Outputs
=> Double
'Asm_Output ("=t", Result
),
512 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
522 function Sinh
(X
: Double
) return Double
is
524 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
527 return (Exp
(X
) - Exp
(-X
)) / 2.0;
529 return Exp
(X
) / 2.0;
537 function Cosh
(X
: Double
) return Double
is
539 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
542 return (Exp
(X
) + Exp
(-X
)) / 2.0;
544 return Exp
(X
) / 2.0;
552 function Tanh
(X
: Double
) return Double
is
554 -- Return the Hyperbolic Tangent of x
558 -- Tanh (X) is defined to be ----------- = --------
563 return Double
'Copy_Sign (1.0, X
);
566 return 1.0 / (1.0 + Exp
(-(2.0 * X
))) - 1.0 / (1.0 + Exp
(2.0 * X
));
569 end Ada
.Numerics
.Aux
;