(* ETH Oberon, Copyright 2001 ETH Zuerich Institut fuer Computersysteme, ETH Zentrum, CH-8092 Zuerich.
Refer to the "General ETH Oberon System Source License" contract available at: http://www.oberon.ethz.ch/ *)

MODULE MathL;	(** portable *)
(** AUTHOR "?"; PURPOSE "Math utility module (LONGREAL)"; *)

IMPORT SYSTEM;

(* Aos version - requires floating-point instruction support. *)

(**
	Commonly needed Math for LONGREALs.
*)

CONST
	e* = 2.7182818284590452354D0;
	pi* = 3.14159265358979323846D0;

PROCEDURE -Sin(x: LONGREAL):LONGREAL;
CODE
	FLD QWORD [ESP]
	FSIN
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Sin;

PROCEDURE -Cos(x: LONGREAL):LONGREAL;
CODE
	FLD QWORD [ESP]
	FCOS
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Cos;

PROCEDURE -Arctan(x: LONGREAL):LONGREAL;
CODE
	FLD QWORD [ESP]
	FLD1
	FPATAN
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Arctan;

PROCEDURE -Sqrt(x: LONGREAL):LONGREAL;
CODE
	FLD QWORD [ESP]
	FSQRT
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Sqrt;

PROCEDURE -Ln(x: LONGREAL):LONGREAL;
CODE
	FLD1
	FLDL2E
	FDIVP
	FLD QWORD [ESP]
	FYL2X
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Ln;

PROCEDURE -Exp(x: LONGREAL):LONGREAL;
CODE
	FLD QWORD [ESP]
	FLDL2E
	FMULP
	FLD ST0
	FRNDINT
	FXCH ST1
	FSUB ST0, ST1
	F2XM1
	FLD1
	FADDP
	FSCALE
	FSTP ST1
	FSTP QWORD [ESP]
	MOVSD XMM0, [ESP]
	ADD ESP, 8
END Exp;

PROCEDURE sin*(x: LONGREAL): LONGREAL;
BEGIN
	IF x < 0.0D0 THEN RETURN -Sin(-x) ELSE RETURN Sin(x) END
END sin;

PROCEDURE cos*(x: LONGREAL): LONGREAL;
BEGIN
	IF x < 0.0D0 THEN RETURN Cos(-x) ELSE RETURN Cos(x) END
END cos;

PROCEDURE arctan*(x: LONGREAL): LONGREAL;
BEGIN
	RETURN Arctan(x)
END arctan;

PROCEDURE sqrt*(x: LONGREAL): LONGREAL;
BEGIN
	IF x <= 0 THEN
		IF x = 0 THEN RETURN 0 ELSE HALT(80) END
	ELSE
		RETURN Sqrt(x)
	END
END sqrt;

PROCEDURE ln*(x: LONGREAL): LONGREAL;
BEGIN
	IF x <= 0 THEN HALT(80)
	ELSE
		RETURN Ln(x)
	END
END ln;

PROCEDURE exp*(x: LONGREAL): LONGREAL;
BEGIN
	RETURN Exp(x)
END exp;

END MathL.
