diff --git a/posix.mak b/posix.mak index 5e0d44e2bd..2aa655b1e2 100644 --- a/posix.mak +++ b/posix.mak @@ -55,6 +55,7 @@ MANIFEST= \ src/core/cpuid.d \ src/core/demangle.d \ src/core/exception.d \ + src/core/math.d \ src/core/memory.d \ src/core/runtime.d \ src/core/thread.d \ @@ -243,6 +244,7 @@ SRC_D_MODULES = \ core/cpuid \ core/demangle \ core/exception \ + core/math \ core/memory \ core/runtime \ core/thread \ @@ -371,6 +373,7 @@ DOCS=\ $(DOCDIR)/core_cpuid.html \ $(DOCDIR)/core_demangle.html \ $(DOCDIR)/core_exception.html \ + $(DOCDIR)/core_math.html \ $(DOCDIR)/core_memory.html \ $(DOCDIR)/core_runtime.html \ $(DOCDIR)/core_thread.html \ @@ -391,6 +394,7 @@ IMPORTS=\ $(IMPDIR)/core/cpuid.di \ $(IMPDIR)/core/demangle.di \ $(IMPDIR)/core/exception.di \ + $(IMPDIR)/core/math.di \ $(IMPDIR)/core/memory.di \ $(IMPDIR)/core/runtime.di \ $(IMPDIR)/core/thread.di \ diff --git a/src/core/math.d b/src/core/math.d new file mode 100644 index 0000000000..b521f90fdd --- /dev/null +++ b/src/core/math.d @@ -0,0 +1,147 @@ +// Written in the D programming language. + +/** + * Builtin mathematical intrinsics + * + * Source: $(DRUNTIMESRC core/_math.d) + * Macros: + * TABLE_SV = + * + * $0
Special Values
+ * + * NAN = $(RED NAN) + * SUP = $0 + * POWER = $1$2 + * PLUSMN = ± + * INFIN = ∞ + * PLUSMNINF = ±∞ + * LT = < + * GT = > + * + * Copyright: Copyright Digital Mars 2000 - 2011. + * License: Boost License 1.0. + * Authors: $(WEB digitalmars.com, Walter Bright), + * Don Clugston + */ +module core.math; + +version(LDC) { + public import ldc.intrinsics; +} + +public: + +/*********************************** + * Returns cosine of x. x is in radians. + * + * $(TABLE_SV + * $(TR $(TH x) $(TH cos(x)) $(TH invalid?)) + * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes) ) + * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD $(NAN)) $(TD yes) ) + * ) + * Bugs: + * Results are undefined if |x| >= $(POWER 2,64). + */ + +real cos(real x) @safe pure nothrow; /* intrinsic */ + +/*********************************** + * Returns sine of x. x is in radians. + * + * $(TABLE_SV + * $(TR $(TH x) $(TH sin(x)) $(TH invalid?)) + * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes)) + * $(TR $(TD $(PLUSMN)0.0) $(TD $(PLUSMN)0.0) $(TD no)) + * $(TR $(TD $(PLUSMNINF)) $(TD $(NAN)) $(TD yes)) + * ) + * Bugs: + * Results are undefined if |x| >= $(POWER 2,64). + */ + +real sin(real x) @safe pure nothrow; /* intrinsic */ + +/***************************************** + * Returns x rounded to a long value using the current rounding mode. + * If the integer value of x is + * greater than long.max, the result is + * indeterminate. + */ +long rndtol(real x) @safe pure nothrow; /* intrinsic */ + + +/***************************************** + * Returns x rounded to a long value using the FE_TONEAREST rounding mode. + * If the integer value of x is + * greater than long.max, the result is + * indeterminate. + */ +extern (C) real rndtonl(real x); + +/*************************************** + * Compute square root of x. + * + * $(TABLE_SV + * $(TR $(TH x) $(TH sqrt(x)) $(TH invalid?)) + * $(TR $(TD -0.0) $(TD -0.0) $(TD no)) + * $(TR $(TD $(LT)0.0) $(TD $(NAN)) $(TD yes)) + * $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no)) + * ) + */ + +@safe pure nothrow +{ + float sqrt(float x); /* intrinsic */ + double sqrt(double x); /* intrinsic */ /// ditto + real sqrt(real x); /* intrinsic */ /// ditto +} + +/******************************************* + * Compute n * 2$(SUP exp) + * References: frexp + */ + +real ldexp(real n, int exp) @safe pure nothrow; /* intrinsic */ + +unittest { + assert(ldexp(1, -16384) == 0x1p-16384L); + assert(ldexp(1, -16382) == 0x1p-16382L); +} + +/******************************* + * Returns |x| + * + * $(TABLE_SV + * $(TR $(TH x) $(TH fabs(x))) + * $(TR $(TD $(PLUSMN)0.0) $(TD +0.0) ) + * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD +$(INFIN)) ) + * ) + */ +real fabs(real x) @safe pure nothrow; /* intrinsic */ + +/********************************** + * Rounds x to the nearest integer value, using the current rounding + * mode. + * If the return value is not equal to x, the FE_INEXACT + * exception is raised. + * $(B nearbyint) performs + * the same operation, but does not set the FE_INEXACT exception. + */ +real rint(real x) @safe pure nothrow; /* intrinsic */ + +/*********************************** + * Building block functions, they + * translate to a single x87 instruction. + */ + +real yl2x(real x, real y) @safe pure nothrow; // y * log2(x) +real yl2xp1(real x, real y) @safe pure nothrow; // y * log2(x + 1) + +unittest +{ + version (INLINE_YL2X) + { + assert(yl2x(1024, 1) == 10); + assert(yl2xp1(1023, 1) == 10); + } +} + diff --git a/win32.mak b/win32.mak index 854ba11c7b..065634a12e 100644 --- a/win32.mak +++ b/win32.mak @@ -34,6 +34,7 @@ MANIFEST= \ src\core\cpuid.d \ src\core\demangle.d \ src\core\exception.d \ + src\core\math.d \ src\core\memory.d \ src\core\runtime.d \ src\core\thread.d \ @@ -224,6 +225,7 @@ SRCS= \ src\core\cpuid.d \ src\core\demangle.d \ src\core\exception.d \ + src\core\math.d \ src\core\memory.d \ src\core\runtime.d \ src\core\thread.d \ @@ -349,6 +351,7 @@ DOCS=\ $(DOCDIR)\core_cpuid.html \ $(DOCDIR)\core_demangle.html \ $(DOCDIR)\core_exception.html \ + $(DOCDIR)\core_math.html \ $(DOCDIR)\core_memory.html \ $(DOCDIR)\core_runtime.html \ $(DOCDIR)\core_thread.html \ @@ -369,6 +372,7 @@ IMPORTS=\ $(IMPDIR)\core\cpuid.di \ $(IMPDIR)\core\demangle.di \ $(IMPDIR)\core\exception.di \ + $(IMPDIR)\core\math.di \ $(IMPDIR)\core\memory.di \ $(IMPDIR)\core\runtime.di \ $(IMPDIR)\core\thread.di \ @@ -475,6 +479,9 @@ $(DOCDIR)\core_demangle.html : src\core\demangle.d $(DOCDIR)\core_exception.html : src\core\exception.d $(DMD) -c -d -o- -Isrc -Iimport -Df$@ $(DOCFMT) $** +$(DOCDIR)\core_math.html : src\core\math.d + $(DMD) -c -d -o- -Isrc -Iimport -Df$@ $(DOCFMT) $** + $(DOCDIR)\core_memory.html : src\core\memory.d $(DMD) -c -d -o- -Isrc -Iimport -Df$@ $(DOCFMT) $** @@ -530,6 +537,9 @@ $(IMPDIR)\core\demangle.di : src\core\demangle.d $(IMPDIR)\core\exception.di : src\core\exception.d $(DMD) -c -d -o- -Isrc -Iimport -Hf$@ $** +$(IMPDIR)\core\math.di : src\core\math.d + $(DMD) -c -d -o- -Isrc -Iimport -Hf$@ $** + $(IMPDIR)\core\memory.di : src\core\memory.d $(DMD) -c -d -o- -Isrc -Iimport -Hf$@ $**