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-2016, 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 with System
.Machine_Code
; use System
.Machine_Code
;
35 package body Ada
.Numerics
.Aux
is
37 NL
: constant String := ASCII
.LF
& ASCII
.HT
;
39 -----------------------
40 -- Local subprograms --
41 -----------------------
43 function Is_Nan
(X
: Double
) return Boolean;
44 -- Return True iff X is a IEEE NaN value
46 function Logarithmic_Pow
(X
, Y
: Double
) return Double
;
47 -- Implementation of X**Y using Exp and Log functions (binary base)
48 -- to calculate the exponentiation. This is used by Pow for values
49 -- for values of Y in the open interval (-0.25, 0.25)
51 procedure Reduce
(X
: in out Double
; Q
: out Natural);
52 -- Implements reduction of X by Pi/2. Q is the quadrant of the final
53 -- result in the range 0 .. 3. The absolute value of X is at most Pi.
55 pragma Inline
(Is_Nan
);
56 pragma Inline
(Reduce
);
58 --------------------------------
59 -- Basic Elementary Functions --
60 --------------------------------
62 -- This section implements a few elementary functions that are used to
63 -- build the more complex ones. This ordering enables better inlining.
69 function Atan
(X
: Double
) return Double
is
76 Outputs
=> Double
'Asm_Output ("=t", Result
),
77 Inputs
=> Double
'Asm_Input ("0", X
));
79 -- The result value is NaN iff input was invalid
81 if not (Result
= Result
) then
92 function Exp
(X
: Double
) return Double
is
97 & "fmulp %%st, %%st(1)" & NL
-- X * log2 (E)
99 & "frndint " & NL
-- Integer (X * Log2 (E))
100 & "fsubr %%st, %%st(1)" & NL
-- Fraction (X * Log2 (E))
102 & "f2xm1 " & NL
-- 2**(...) - 1
104 & "faddp %%st, %%st(1)" & NL
-- 2**(Fraction (X * Log2 (E)))
105 & "fscale " & NL
-- E ** X
107 Outputs
=> Double
'Asm_Output ("=t", Result
),
108 Inputs
=> Double
'Asm_Input ("0", X
));
116 function Is_Nan
(X
: Double
) return Boolean is
118 -- The IEEE NaN values are the only ones that do not equal themselves
127 function Log
(X
: Double
) return Double
is
135 Outputs
=> Double
'Asm_Output ("=t", Result
),
136 Inputs
=> Double
'Asm_Input ("0", X
));
144 procedure Reduce
(X
: in out Double
; Q
: out Natural) is
145 Half_Pi
: constant := Pi
/ 2.0;
146 Two_Over_Pi
: constant := 2.0 / Pi
;
148 HM
: constant := Integer'Min (Double
'Machine_Mantissa / 2, Natural'Size);
149 M
: constant Double
:= 0.5 + 2.0**(1 - HM
); -- Splitting constant
150 P1
: constant Double
:= Double
'Leading_Part (Half_Pi
, HM
);
151 P2
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
, HM
);
152 P3
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
, HM
);
153 P4
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
- P3
, HM
);
154 P5
: constant Double
:= Double
'Leading_Part (Half_Pi
- P1
- P2
- P3
156 P6
: constant Double
:= Double
'Model (Half_Pi
- P1
- P2
- P3
- P4
- P5
);
157 K
: Double
:= X
* Two_Over_Pi
;
159 -- For X < 2.0**32, all products below are computed exactly.
160 -- Due to cancellation effects all subtractions are exact as well.
161 -- As no double extended floating-point number has more than 75
162 -- zeros after the binary point, the result will be the correctly
163 -- rounded result of X - K * (Pi / 2.0).
165 while abs K
>= 2.0**HM
loop
166 K
:= K
* M
- (K
* M
- K
);
167 X
:= (((((X
- K
* P1
) - K
* P2
) - K
* P3
)
168 - K
* P4
) - K
* P5
) - K
* P6
;
169 K
:= X
* Two_Over_Pi
;
174 -- K is not a number, because X was not finite
176 raise Constraint_Error
;
179 K
:= Double
'Rounding (K
);
180 Q
:= Integer (K
) mod 4;
181 X
:= (((((X
- K
* P1
) - K
* P2
) - K
* P3
)
182 - K
* P4
) - K
* P5
) - K
* P6
;
189 function Sqrt
(X
: Double
) return Double
is
194 raise Argument_Error
;
197 Asm
(Template
=> "fsqrt",
198 Outputs
=> Double
'Asm_Output ("=t", Result
),
199 Inputs
=> Double
'Asm_Input ("0", X
));
204 --------------------------------
205 -- Other Elementary Functions --
206 --------------------------------
208 -- These are built using the previously implemented basic functions
214 function Acos
(X
: Double
) return Double
is
218 Result
:= 2.0 * Atan
(Sqrt
((1.0 - X
) / (1.0 + X
)));
220 -- The result value is NaN iff input was invalid
222 if Is_Nan
(Result
) then
223 raise Argument_Error
;
233 function Asin
(X
: Double
) return Double
is
237 Result
:= Atan
(X
/ Sqrt
((1.0 - X
) * (1.0 + X
)));
239 -- The result value is NaN iff input was invalid
241 if Is_Nan
(Result
) then
242 raise Argument_Error
;
252 function Cos
(X
: Double
) return Double
is
253 Reduced_X
: Double
:= abs X
;
255 Quadrant
: Natural range 0 .. 3;
258 if Reduced_X
> Pi
/ 4.0 then
259 Reduce
(Reduced_X
, Quadrant
);
263 Asm
(Template
=> "fcos",
264 Outputs
=> Double
'Asm_Output ("=t", Result
),
265 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
268 Asm
(Template
=> "fsin",
269 Outputs
=> Double
'Asm_Output ("=t", Result
),
270 Inputs
=> Double
'Asm_Input ("0", -Reduced_X
));
273 Asm
(Template
=> "fcos ; fchs",
274 Outputs
=> Double
'Asm_Output ("=t", Result
),
275 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
278 Asm
(Template
=> "fsin",
279 Outputs
=> Double
'Asm_Output ("=t", Result
),
280 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
284 Asm
(Template
=> "fcos",
285 Outputs
=> Double
'Asm_Output ("=t", Result
),
286 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
292 ---------------------
293 -- Logarithmic_Pow --
294 ---------------------
296 function Logarithmic_Pow
(X
, Y
: Double
) return Double
is
299 Asm
(Template
=> "" -- X : Y
300 & "fyl2x " & NL
-- Y * Log2 (X)
301 & "fld %%st(0) " & NL
-- Y * Log2 (X) : Y * Log2 (X)
302 & "frndint " & NL
-- Int (...) : Y * Log2 (X)
303 & "fsubr %%st, %%st(1)" & NL
-- Int (...) : Fract (...)
304 & "fxch " & NL
-- Fract (...) : Int (...)
305 & "f2xm1 " & NL
-- 2**Fract (...) - 1 : Int (...)
306 & "fld1 " & NL
-- 1 : 2**Fract (...) - 1 : Int (...)
307 & "faddp %%st, %%st(1)" & NL
-- 2**Fract (...) : Int (...)
308 & "fscale ", -- 2**(Fract (...) + Int (...))
309 Outputs
=> Double
'Asm_Output ("=t", Result
),
311 (Double
'Asm_Input ("0", X
),
312 Double
'Asm_Input ("u", Y
)));
320 function Pow
(X
, Y
: Double
) return Double
is
321 type Mantissa_Type
is mod 2**Double
'Machine_Mantissa;
322 -- Modular type that can hold all bits of the mantissa of Double
324 -- For negative exponents, do divide at the end of the processing
326 Negative_Y
: constant Boolean := Y
< 0.0;
327 Abs_Y
: constant Double
:= abs Y
;
329 -- During this function the following invariant is kept:
330 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
334 Exp_High
: Double
:= Double
'Floor (Abs_Y
);
337 Exp_Int
: Mantissa_Type
;
339 Factor
: Double
:= 1.0;
342 -- Select algorithm for calculating Pow (integer cases fall through)
344 if Exp_High
>= 2.0**Double
'Machine_Mantissa then
346 -- In case of Y that is IEEE infinity, just raise constraint error
348 if Exp_High
> Double
'Safe_Last then
349 raise Constraint_Error
;
352 -- Large values of Y are even integers and will stay integer
353 -- after division by two.
356 -- Exp_Mid and Exp_Low are zero, so
357 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
359 Exp_High
:= Exp_High
/ 2.0;
361 exit when Exp_High
< 2.0**Double
'Machine_Mantissa;
364 elsif Exp_High
/= Abs_Y
then
365 Exp_Low
:= Abs_Y
- Exp_High
;
368 if Exp_Low
/= 0.0 then
370 -- Exp_Low now is in interval (0.0, 1.0)
371 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
374 Exp_Low
:= Exp_Low
- Exp_Mid
;
376 if Exp_Low
>= 0.5 then
378 Exp_Low
:= Exp_Low
- 0.5; -- exact
380 if Exp_Low
>= 0.25 then
381 Factor
:= Factor
* Sqrt
(Factor
);
382 Exp_Low
:= Exp_Low
- 0.25; -- exact
385 elsif Exp_Low
>= 0.25 then
386 Factor
:= Sqrt
(Sqrt
(X
));
387 Exp_Low
:= Exp_Low
- 0.25; -- exact
390 -- Exp_Low now is in interval (0.0, 0.25)
392 -- This means it is safe to call Logarithmic_Pow
393 -- for the remaining part.
395 Factor
:= Factor
* Logarithmic_Pow
(X
, Exp_Low
);
402 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
404 Exp_Int
:= Mantissa_Type
(Exp_High
);
406 -- Standard way for processing integer powers > 0
408 while Exp_Int
> 1 loop
409 if (Exp_Int
and 1) = 1 then
411 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
413 Factor
:= Factor
* Base
;
416 -- Exp_Int is even and Exp_Int > 0, so
417 -- Base**Y = (Base**2)**(Exp_Int / 2)
420 Exp_Int
:= Exp_Int
/ 2;
423 -- Exp_Int = 1 or Exp_Int = 0
426 Factor
:= Base
* Factor
;
430 Factor
:= 1.0 / Factor
;
440 function Sin
(X
: Double
) return Double
is
441 Reduced_X
: Double
:= X
;
443 Quadrant
: Natural range 0 .. 3;
446 if abs X
> Pi
/ 4.0 then
447 Reduce
(Reduced_X
, Quadrant
);
451 Asm
(Template
=> "fsin",
452 Outputs
=> Double
'Asm_Output ("=t", Result
),
453 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
456 Asm
(Template
=> "fcos",
457 Outputs
=> Double
'Asm_Output ("=t", Result
),
458 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
));
466 Asm
(Template
=> "fcos ; fchs",
467 Outputs
=> Double
'Asm_Output ("=t", Result
),
468 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
472 Asm
(Template
=> "fsin",
473 Outputs
=> Double
'Asm_Output ("=t", Result
),
474 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
484 function Tan
(X
: Double
) return Double
is
485 Reduced_X
: Double
:= X
;
487 Quadrant
: Natural range 0 .. 3;
490 if abs X
> Pi
/ 4.0 then
491 Reduce
(Reduced_X
, Quadrant
);
493 if Quadrant
mod 2 = 0 then
494 Asm
(Template
=> "fptan" & NL
495 & "ffree %%st(0)" & NL
497 Outputs
=> Double
'Asm_Output ("=t", Result
),
498 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
500 Asm
(Template
=> "fsincos" & NL
501 & "fdivp %%st, %%st(1)" & NL
503 Outputs
=> Double
'Asm_Output ("=t", Result
),
504 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
510 & "ffree %%st(0) " & NL
512 Outputs
=> Double
'Asm_Output ("=t", Result
),
513 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
523 function Sinh
(X
: Double
) return Double
is
525 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
528 return (Exp
(X
) - Exp
(-X
)) / 2.0;
530 return Exp
(X
) / 2.0;
538 function Cosh
(X
: Double
) return Double
is
540 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
543 return (Exp
(X
) + Exp
(-X
)) / 2.0;
545 return Exp
(X
) / 2.0;
553 function Tanh
(X
: Double
) return Double
is
555 -- Return the Hyperbolic Tangent of x
559 -- Tanh (X) is defined to be ----------- = --------
564 return Double
'Copy_Sign (1.0, X
);
567 return 1.0 / (1.0 + Exp
(-(2.0 * X
))) - 1.0 / (1.0 + Exp
(2.0 * X
));
570 end Ada
.Numerics
.Aux
;