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) --
10 -- Copyright (C) 1998-2001 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, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, 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 type FPU_Stack_Pointer
is range 0 .. 7;
47 for FPU_Stack_Pointer
'Size use 3;
49 type FPU_Status_Word
is record
50 B
: Boolean; -- FPU Busy (for 8087 compatibility only)
51 ES
: Boolean; -- Error Summary Status
52 SF
: Boolean; -- Stack Fault
54 Top
: FPU_Stack_Pointer
;
56 -- Condition Code Flags
58 -- C2 is set by FPREM and FPREM1 to indicate incomplete reduction.
59 -- In case of successfull recorction, C0, C3 and C1 are set to the
60 -- three least significant bits of the result (resp. Q2, Q1 and Q0).
62 -- C2 is used by FPTAN, FSIN, FCOS, and FSINCOS to indicate that
63 -- that source operand is beyond the allowable range of
64 -- -2.0**63 .. 2.0**63.
73 PE
: Boolean; -- Precision
74 UE
: Boolean; -- Underflow
75 OE
: Boolean; -- Overflow
76 ZE
: Boolean; -- Zero Divide
77 DE
: Boolean; -- Denormalized Operand
78 IE
: Boolean; -- Invalid Operation
81 for FPU_Status_Word
use record
82 B
at 0 range 15 .. 15;
83 C3
at 0 range 14 .. 14;
84 Top
at 0 range 11 .. 13;
85 C2
at 0 range 10 .. 10;
98 for FPU_Status_Word
'Size use 16;
100 -----------------------
101 -- Local subprograms --
102 -----------------------
104 function Is_Nan
(X
: Double
) return Boolean;
105 -- Return True iff X is a IEEE NaN value
107 function Logarithmic_Pow
(X
, Y
: Double
) return Double
;
108 -- Implementation of X**Y using Exp and Log functions (binary base)
109 -- to calculate the exponentiation. This is used by Pow for values
110 -- for values of Y in the open interval (-0.25, 0.25)
112 function Reduce
(X
: Double
) return Double
;
113 -- Implement partial reduction of X by Pi in the x86.
115 -- Note that for the Sin, Cos and Tan functions completely accurate
116 -- reduction of the argument is done for arguments in the range of
117 -- -2.0**63 .. 2.0**63, using a 66-bit approximation of Pi.
119 pragma Inline
(Is_Nan
);
120 pragma Inline
(Reduce
);
122 ---------------------------------
123 -- Basic Elementary Functions --
124 ---------------------------------
126 -- This section implements a few elementary functions that are
127 -- used to build the more complex ones. This ordering enables
134 function Atan
(X
: Double
) return Double
is
141 Outputs
=> Double
'Asm_Output ("=t", Result
),
142 Inputs
=> Double
'Asm_Input ("0", X
));
144 -- The result value is NaN iff input was invalid
146 if not (Result
= Result
) then
147 raise Argument_Error
;
157 function Exp
(X
: Double
) return Double
is
162 & "fmulp %%st, %%st(1)" & NL
-- X * log2 (E)
163 & "fld %%st(0) " & NL
164 & "frndint " & NL
-- Integer (X * Log2 (E))
165 & "fsubr %%st, %%st(1)" & NL
-- Fraction (X * Log2 (E))
167 & "f2xm1 " & NL
-- 2**(...) - 1
169 & "faddp %%st, %%st(1)" & NL
-- 2**(Fraction (X * Log2 (E)))
170 & "fscale " & NL
-- E ** X
172 Outputs
=> Double
'Asm_Output ("=t", Result
),
173 Inputs
=> Double
'Asm_Input ("0", X
));
181 function Is_Nan
(X
: Double
) return Boolean is
183 -- The IEEE NaN values are the only ones that do not equal themselves
192 function Log
(X
: Double
) return Double
is
200 Outputs
=> Double
'Asm_Output ("=t", Result
),
201 Inputs
=> Double
'Asm_Input ("0", X
));
209 function Reduce
(X
: Double
) return Double
is
214 -- Partial argument reduction
216 & "fadd %%st(0), %%st" & NL
217 & "fxch %%st(1) " & NL
220 Outputs
=> Double
'Asm_Output ("=t", Result
),
221 Inputs
=> Double
'Asm_Input ("0", X
));
229 function Sqrt
(X
: Double
) return Double
is
234 raise Argument_Error
;
237 Asm
(Template
=> "fsqrt",
238 Outputs
=> Double
'Asm_Output ("=t", Result
),
239 Inputs
=> Double
'Asm_Input ("0", X
));
244 ---------------------------------
245 -- Other Elementary Functions --
246 ---------------------------------
248 -- These are built using the previously implemented basic functions
254 function Acos
(X
: Double
) return Double
is
257 Result
:= 2.0 * Atan
(Sqrt
((1.0 - X
) / (1.0 + X
)));
259 -- The result value is NaN iff input was invalid
261 if Is_Nan
(Result
) then
262 raise Argument_Error
;
272 function Asin
(X
: Double
) return Double
is
276 Result
:= Atan
(X
/ Sqrt
((1.0 - X
) * (1.0 + X
)));
278 -- The result value is NaN iff input was invalid
280 if Is_Nan
(Result
) then
281 raise Argument_Error
;
291 function Cos
(X
: Double
) return Double
is
292 Reduced_X
: Double
:= X
;
294 Status
: FPU_Status_Word
;
302 & "xorl %%eax, %%eax " & NL
304 Outputs
=> (Double
'Asm_Output ("=t", Result
),
305 FPU_Status_Word
'Asm_Output ("=a", Status
)),
306 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
308 exit when not Status
.C2
;
310 -- Original argument was not in range and the result
311 -- is the unmodified argument.
313 Reduced_X
:= Reduce
(Result
);
319 ---------------------
320 -- Logarithmic_Pow --
321 ---------------------
323 function Logarithmic_Pow
(X
, Y
: Double
) return Double
is
327 Asm
(Template
=> "" -- X : Y
328 & "fyl2x " & NL
-- Y * Log2 (X)
329 & "fst %%st(1) " & NL
-- Y * Log2 (X) : Y * Log2 (X)
330 & "frndint " & NL
-- Int (...) : Y * Log2 (X)
331 & "fsubr %%st, %%st(1)" & NL
-- Int (...) : Fract (...)
332 & "fxch " & NL
-- Fract (...) : Int (...)
333 & "f2xm1 " & NL
-- 2**Fract (...) - 1 : Int (...)
334 & "fld1 " & NL
-- 1 : 2**Fract (...) - 1 : Int (...)
335 & "faddp %%st, %%st(1)" & NL
-- 2**Fract (...) : Int (...)
336 & "fscale " & NL
-- 2**(Fract (...) + Int (...))
338 Outputs
=> Double
'Asm_Output ("=t", Result
),
340 (Double
'Asm_Input ("0", X
),
341 Double
'Asm_Input ("u", Y
)));
350 function Pow
(X
, Y
: Double
) return Double
is
351 type Mantissa_Type
is mod 2**Double
'Machine_Mantissa;
352 -- Modular type that can hold all bits of the mantissa of Double
354 -- For negative exponents, a division is done
355 -- at the end of the processing.
357 Negative_Y
: constant Boolean := Y
< 0.0;
358 Abs_Y
: constant Double
:= abs Y
;
360 -- During this function the following invariant is kept:
361 -- X ** (abs Y) = Base**(Exp_High + Exp_Mid + Exp_Low) * Factor
365 Exp_High
: Double
:= Double
'Floor (Abs_Y
);
368 Exp_Int
: Mantissa_Type
;
370 Factor
: Double
:= 1.0;
373 -- Select algorithm for calculating Pow:
374 -- integer cases fall through
376 if Exp_High
>= 2.0**Double
'Machine_Mantissa then
378 -- In case of Y that is IEEE infinity, just raise constraint error
380 if Exp_High
> Double
'Safe_Last then
381 raise Constraint_Error
;
384 -- Large values of Y are even integers and will stay integer
385 -- after division by two.
388 -- Exp_Mid and Exp_Low are zero, so
389 -- X**(abs Y) = Base ** Exp_High = (Base**2) ** (Exp_High / 2)
391 Exp_High
:= Exp_High
/ 2.0;
393 exit when Exp_High
< 2.0**Double
'Machine_Mantissa;
396 elsif Exp_High
/= Abs_Y
then
397 Exp_Low
:= Abs_Y
- Exp_High
;
401 if Exp_Low
/= 0.0 then
403 -- Exp_Low now is in interval (0.0, 1.0)
404 -- Exp_Mid := Double'Floor (Exp_Low * 4.0) / 4.0;
407 Exp_Low
:= Exp_Low
- Exp_Mid
;
409 if Exp_Low
>= 0.5 then
411 Exp_Low
:= Exp_Low
- 0.5; -- exact
413 if Exp_Low
>= 0.25 then
414 Factor
:= Factor
* Sqrt
(Factor
);
415 Exp_Low
:= Exp_Low
- 0.25; -- exact
418 elsif Exp_Low
>= 0.25 then
419 Factor
:= Sqrt
(Sqrt
(X
));
420 Exp_Low
:= Exp_Low
- 0.25; -- exact
423 -- Exp_Low now is in interval (0.0, 0.25)
425 -- This means it is safe to call Logarithmic_Pow
426 -- for the remaining part.
428 Factor
:= Factor
* Logarithmic_Pow
(X
, Exp_Low
);
435 -- Exp_High is non-zero integer smaller than 2**Double'Machine_Mantissa
437 Exp_Int
:= Mantissa_Type
(Exp_High
);
439 -- Standard way for processing integer powers > 0
441 while Exp_Int
> 1 loop
442 if (Exp_Int
and 1) = 1 then
444 -- Base**Y = Base**(Exp_Int - 1) * Exp_Int for Exp_Int > 0
446 Factor
:= Factor
* Base
;
449 -- Exp_Int is even and Exp_Int > 0, so
450 -- Base**Y = (Base**2)**(Exp_Int / 2)
453 Exp_Int
:= Exp_Int
/ 2;
456 -- Exp_Int = 1 or Exp_Int = 0
459 Factor
:= Base
* Factor
;
463 Factor
:= 1.0 / Factor
;
473 function Sin
(X
: Double
) return Double
is
474 Reduced_X
: Double
:= X
;
476 Status
: FPU_Status_Word
;
484 & "xorl %%eax, %%eax " & NL
486 Outputs
=> (Double
'Asm_Output ("=t", Result
),
487 FPU_Status_Word
'Asm_Output ("=a", Status
)),
488 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
490 exit when not Status
.C2
;
492 -- Original argument was not in range and the result
493 -- is the unmodified argument.
495 Reduced_X
:= Reduce
(Result
);
505 function Tan
(X
: Double
) return Double
is
506 Reduced_X
: Double
:= X
;
508 Status
: FPU_Status_Word
;
516 & "xorl %%eax, %%eax " & NL
517 & "fnstsw %%ax " & NL
518 & "ffree %%st(0) " & NL
521 Outputs
=> (Double
'Asm_Output ("=t", Result
),
522 FPU_Status_Word
'Asm_Output ("=a", Status
)),
523 Inputs
=> Double
'Asm_Input ("0", Reduced_X
));
525 exit when not Status
.C2
;
527 -- Original argument was not in range and the result
528 -- is the unmodified argument.
530 Reduced_X
:= Reduce
(Result
);
540 function Sinh
(X
: Double
) return Double
is
542 -- Mathematically Sinh (x) is defined to be (Exp (X) - Exp (-X)) / 2.0
545 return (Exp
(X
) - Exp
(-X
)) / 2.0;
548 return Exp
(X
) / 2.0;
557 function Cosh
(X
: Double
) return Double
is
559 -- Mathematically Cosh (X) is defined to be (Exp (X) + Exp (-X)) / 2.0
562 return (Exp
(X
) + Exp
(-X
)) / 2.0;
565 return Exp
(X
) / 2.0;
574 function Tanh
(X
: Double
) return Double
is
576 -- Return the Hyperbolic Tangent of x
580 -- Tanh (X) is defined to be ----------- = --------
585 return Double
'Copy_Sign (1.0, X
);
588 return 1.0 / (1.0 + Exp
(-2.0 * X
)) - 1.0 / (1.0 + Exp
(2.0 * X
));
592 end Ada
.Numerics
.Aux
;