(* 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 {SYSTEM.AMD64, SYSTEM.FPU}
	FLD qword [RSP]
	FSIN
	FSTP qword [RSP]
	POP RAX
END Sin;

PROCEDURE -Cos(x: LONGREAL):LONGREAL;
CODE {SYSTEM.AMD64, SYSTEM.FPU}
	FLD qword [RSP]
	FCOS
	FSTP qword [RSP]
	POP RAX
END Cos;

PROCEDURE -Arctan(x: LONGREAL):LONGREAL;
CODE {SYSTEM.AMD64, SYSTEM.FPU}
	FLD qword [RSP]
	FLD1
	FPATAN
	FSTP qword [RSP]
	POP RAX
END Arctan;

PROCEDURE -Sqrt(x: LONGREAL):LONGREAL;
CODE {SYSTEM.AMD64, SYSTEM.FPU}
	FLD qword [RSP]
	FSQRT
	FSTP qword [RSP]
	POP RAX
END Sqrt;

PROCEDURE -Ln(x: LONGREAL):LONGREAL;
CODE {SYSTEM.AMD64, SYSTEM.FPU}
	FLD1
	FLDL2E
	FDIVP
	FLD qword [RSP]
	FYL2X
	FSTP qword [RSP]
	POP RAX
END Ln;

PROCEDURE -Exp(x: LONGREAL):LONGREAL;
CODE {SYSTEM.AMD64, SYSTEM.FPU}
	FLD qword [RSP]
	FLDL2E
	FMULP
	FLD ST0
	FRNDINT
	FXCH ST1
	FSUB ST0, ST1
	F2XM1
	FLD1
	FADDP
	FSCALE
	FSTP ST1
	FSTP qword [RSP]
	POP RAX
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.