1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . N U M E R I C S . A U X --
8 -- (Machine Version for x86) --
12 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
14 -- GNAT is free software; you can redistribute it and/or modify it under --
15 -- terms of the GNU General Public License as published by the Free Soft- --
16 -- ware Foundation; either version 2, or (at your option) any later ver- --
17 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
20 -- for more details. You should have received a copy of the GNU General --
21 -- Public License distributed with GNAT; see file COPYING. If not, write --
22 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
23 -- MA 02111-1307, USA. --
25 -- As a special exception, if other files instantiate generics from this --
26 -- unit, or you link this unit with other files to produce an executable, --
27 -- this unit does not by itself cause the resulting executable to be --
28 -- covered by the GNU General Public License. This exception does not --
29 -- however invalidate any other reasons why the executable file might be --
30 -- covered by the GNU Public License. --
32 -- GNAT was originally developed by the GNAT team at New York University. --
33 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- File a-numaux.adb <- 86numaux.adb
39 -- This version of Numerics.Aux is for the IEEE Double Extended floating
40 -- point format on x86.
42 with System
.Machine_Code
; use System
.Machine_Code
;
44 package body Ada
.Numerics
.Aux
is
46 NL
: constant String := ASCII
.LF
& ASCII
.HT
;
48 type FPU_Stack_Pointer
is range 0 .. 7;
49 for FPU_Stack_Pointer
'Size use 3;
51 type FPU_Status_Word
is record
52 B
: Boolean; -- FPU Busy (for 8087 compatibility only)
53 ES
: Boolean; -- Error Summary Status
54 SF
: Boolean; -- Stack Fault
56 Top
: FPU_Stack_Pointer
;
58 -- Condition Code Flags
60 -- C2 is set by FPREM and FPREM1 to indicate incomplete reduction.
61 -- In case of successfull recorction, C0, C3 and C1 are set to the
62 -- three least significant bits of the result (resp. Q2, Q1 and Q0).
64 -- C2 is used by FPTAN, FSIN, FCOS, and FSINCOS to indicate that
65 -- that source operand is beyond the allowable range of
66 -- -2.0**63 .. 2.0**63.
75 PE
: Boolean; -- Precision
76 UE
: Boolean; -- Underflow
77 OE
: Boolean; -- Overflow
78 ZE
: Boolean; -- Zero Divide
79 DE
: Boolean; -- Denormalized Operand
80 IE
: Boolean; -- Invalid Operation
83 for FPU_Status_Word
use record
84 B
at 0 range 15 .. 15;
85 C3
at 0 range 14 .. 14;
86 Top
at 0 range 11 .. 13;
87 C2
at 0 range 10 .. 10;
100 for FPU_Status_Word
'Size use 16;
102 -----------------------
103 -- Local subprograms --
104 -----------------------
106 function Is_Nan
(X
: Double
) return Boolean;
107 -- Return True iff X is a IEEE NaN value
109 function Logarithmic_Pow
(X
, Y
: Double
) return Double
;
110 -- Implementation of X**Y using Exp and Log functions (binary base)
111 -- to calculate the exponentiation. This is used by Pow for values
112 -- for values of Y in the open interval (-0.25, 0.25)
114 function Reduce
(X
: Double
) return Double
;
115 -- Implement partial reduction of X by Pi in the x86.
117 -- Note that for the Sin, Cos and Tan functions completely accurate
118 -- reduction of the argument is done for arguments in the range of
119 -- -2.0**63 .. 2.0**63, using a 66-bit approximation of Pi.
121 pragma Inline
(Is_Nan
);
122 pragma Inline
(Reduce
);
124 ---------------------------------
125 -- Basic Elementary Functions --
126 ---------------------------------
128 -- This section implements a few elementary functions that are
129 -- used to build the more complex ones. This ordering enables
136 function Atan
(X
: Double
) return Double
is
143 Outputs
=> Double
'Asm_Output ("=t", Result
),
144 Inputs
=> Double
'Asm_Input ("0", X
));
146 -- The result value is NaN iff input was invalid
148 if not (Result
= Result
) then
149 raise Argument_Error
;
159 function Exp
(X
: Double
) return Double
is
164 & "fmulp %%st, %%st(1)" & NL
-- X * log2 (E)
165 & "fld %%st(0) " & NL
166 & "frndint " & NL
-- Integer (X * Log2 (E))
167 & "fsubr %%st, %%st(1)" & NL
-- Fraction (X * Log2 (E))
169 & "f2xm1 " & NL
-- 2**(...) - 1
171 & "faddp %%st, %%st(1)" & NL
-- 2**(Fraction (X * Log2 (E)))
172 & "fscale " & NL
-- E ** X
174 Outputs
=> Double
'Asm_Output ("=t", Result
),
175 Inputs
=> Double
'Asm_Input ("0", X
));
183 function Is_Nan
(X
: Double
) return Boolean is
185 -- The IEEE NaN values are the only ones that do not equal themselves
194 function Log
(X
: Double
) return Double
is
202 Outputs
=> Double
'Asm_Output ("=t", Result
),
203 Inputs
=> Double
'Asm_Input ("0", X
));
211 function Reduce
(X
: Double
) return Double
is
216 -- Partial argument reduction
218 & "fadd %%st(0), %%st" & NL
219 & "fxch %%st(1) " & NL
222 Outputs
=> Double
'Asm_Output ("=t", Result
),
223 Inputs
=> Double
'Asm_Input ("0", X
));
231 function Sqrt
(X
: Double
) return Double
is
236 raise Argument_Error
;
239 Asm
(Template
=> "fsqrt",
240 Outputs
=> Double
'Asm_Output ("=t", Result
),
241 Inputs
=> Double
'Asm_Input ("0", X
));
246 ---------------------------------
247 -- Other Elementary Functions --
248 ---------------------------------
250 -- These are built using the previously implemented basic functions
256 function Acos
(X
: Double
) return Double
is
259 Result
:= 2.0 * Atan
(Sqrt
((1.0 - X
) / (1.0 + X
)));
261 -- The result value is NaN iff input was invalid
263 if Is_Nan
(Result
) then
264 raise Argument_Error
;
274 function Asin
(X
: Double
) return Double
is
278 Result
:= Atan
(X
/ Sqrt
((1.0 - X
) * (1.0 + X
)));
280 -- The result value is NaN iff input was invalid
282 if Is_Nan
(Result
) then
283 raise Argument_Error
;
293 function Cos
(X
: Double
) return Double
is
294 Reduced_X
: Double
:= X
;
296 Status
: FPU_Status_Word
;
304 & "xorl %%eax, %%eax " & NL
306 Outputs
=> (Double
'Asm_Output ("=t", Result
),
307 FPU_Status_Word
'Asm_Output ("=a", Status
)),
308 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
310 exit when not Status
.C2
;
312 -- Original argument was not in range and the result
313 -- is the unmodified argument.
315 Reduced_X
:= Reduce
(Result
);
321 ---------------------
322 -- Logarithmic_Pow --
323 ---------------------
325 function Logarithmic_Pow
(X
, Y
: Double
) return Double
is
329 Asm
(Template
=> "" -- X : Y
330 & "fyl2x " & NL
-- Y * Log2 (X)
331 & "fst %%st(1) " & NL
-- Y * Log2 (X) : Y * Log2 (X)
332 & "frndint " & NL
-- Int (...) : Y * Log2 (X)
333 & "fsubr %%st, %%st(1)" & NL
-- Int (...) : Fract (...)
334 & "fxch " & NL
-- Fract (...) : Int (...)
335 & "f2xm1 " & NL
-- 2**Fract (...) - 1 : Int (...)
336 & "fld1 " & NL
-- 1 : 2**Fract (...) - 1 : Int (...)
337 & "faddp %%st, %%st(1)" & NL
-- 2**Fract (...) : Int (...)
338 & "fscale " & NL
-- 2**(Fract (...) + Int (...))
340 Outputs
=> Double
'Asm_Output ("=t", Result
),
342 (Double
'Asm_Input ("0", X
),
343 Double
'Asm_Input ("u", Y
)));
352 function Pow
(X
, Y
: Double
) return Double
is
353 type Mantissa_Type
is mod 2**Double
'Machine_Mantissa;
354 -- Modular type that can hold all bits of the mantissa of Double
356 -- For negative exponents, a division is done
357 -- at the end of the processing.
359 Negative_Y
: constant Boolean := Y
< 0.0;
360 Abs_Y
: constant Double
:= abs Y
;
362 -- During this function the following invariant is kept:
363 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
367 Exp_High
: Double
:= Double
'Floor (Abs_Y
);
370 Exp_Int
: Mantissa_Type
;
372 Factor
: Double
:= 1.0;
375 -- Select algorithm for calculating Pow:
376 -- integer cases fall through
378 if Exp_High
>= 2.0**Double
'Machine_Mantissa then
380 -- In case of Y that is IEEE infinity, just raise constraint error
382 if Exp_High
> Double
'Safe_Last then
383 raise Constraint_Error
;
386 -- Large values of Y are even integers and will stay integer
387 -- after division by two.
390 -- Exp_Mid and Exp_Low are zero, so
391 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
393 Exp_High
:= Exp_High
/ 2.0;
395 exit when Exp_High
< 2.0**Double
'Machine_Mantissa;
398 elsif Exp_High
/= Abs_Y
then
399 Exp_Low
:= Abs_Y
- Exp_High
;
403 if Exp_Low
/= 0.0 then
405 -- Exp_Low now is in interval (0.0, 1.0)
406 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
409 Exp_Low
:= Exp_Low
- Exp_Mid
;
411 if Exp_Low
>= 0.5 then
413 Exp_Low
:= Exp_Low
- 0.5; -- exact
415 if Exp_Low
>= 0.25 then
416 Factor
:= Factor
* Sqrt
(Factor
);
417 Exp_Low
:= Exp_Low
- 0.25; -- exact
420 elsif Exp_Low
>= 0.25 then
421 Factor
:= Sqrt
(Sqrt
(X
));
422 Exp_Low
:= Exp_Low
- 0.25; -- exact
425 -- Exp_Low now is in interval (0.0, 0.25)
427 -- This means it is safe to call Logarithmic_Pow
428 -- for the remaining part.
430 Factor
:= Factor
* Logarithmic_Pow
(X
, Exp_Low
);
437 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
439 Exp_Int
:= Mantissa_Type
(Exp_High
);
441 -- Standard way for processing integer powers > 0
443 while Exp_Int
> 1 loop
444 if (Exp_Int
and 1) = 1 then
446 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
448 Factor
:= Factor
* Base
;
451 -- Exp_Int is even and Exp_Int > 0, so
452 -- Base**Y = (Base**2)**(Exp_Int / 2)
455 Exp_Int
:= Exp_Int
/ 2;
458 -- Exp_Int = 1 or Exp_Int = 0
461 Factor
:= Base
* Factor
;
465 Factor
:= 1.0 / Factor
;
475 function Sin
(X
: Double
) return Double
is
476 Reduced_X
: Double
:= X
;
478 Status
: FPU_Status_Word
;
486 & "xorl %%eax, %%eax " & NL
488 Outputs
=> (Double
'Asm_Output ("=t", Result
),
489 FPU_Status_Word
'Asm_Output ("=a", Status
)),
490 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
492 exit when not Status
.C2
;
494 -- Original argument was not in range and the result
495 -- is the unmodified argument.
497 Reduced_X
:= Reduce
(Result
);
507 function Tan
(X
: Double
) return Double
is
508 Reduced_X
: Double
:= X
;
510 Status
: FPU_Status_Word
;
518 & "xorl %%eax, %%eax " & NL
519 & "fnstsw %%ax " & NL
520 & "ffree %%st(0) " & NL
523 Outputs
=> (Double
'Asm_Output ("=t", Result
),
524 FPU_Status_Word
'Asm_Output ("=a", Status
)),
525 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
527 exit when not Status
.C2
;
529 -- Original argument was not in range and the result
530 -- is the unmodified argument.
532 Reduced_X
:= Reduce
(Result
);
542 function Sinh
(X
: Double
) return Double
is
544 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
547 return (Exp
(X
) - Exp
(-X
)) / 2.0;
550 return Exp
(X
) / 2.0;
559 function Cosh
(X
: Double
) return Double
is
561 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
564 return (Exp
(X
) + Exp
(-X
)) / 2.0;
567 return Exp
(X
) / 2.0;
576 function Tanh
(X
: Double
) return Double
is
578 -- Return the Hyperbolic Tangent of x
582 -- Tanh (X) is defined to be ----------- = --------
587 return Double
'Copy_Sign (1.0, X
);
590 return 1.0 / (1.0 + Exp
(-2.0 * X
)) - 1.0 / (1.0 + Exp
(2.0 * X
));
594 end Ada
.Numerics
.Aux
;