Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -243,6 +244,7 @@ SRC_D_MODULES = \
core/cpuid \
core/demangle \
core/exception \
core/math \
core/memory \
core/runtime \
core/thread \
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
147 changes: 147 additions & 0 deletions src/core/math.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Written in the D programming language.

/**
* Builtin mathematical intrinsics
*
* Source: $(DRUNTIMESRC core/_math.d)
* Macros:
* TABLE_SV = <table border=1 cellpadding=4 cellspacing=0>
* <caption>Special Values</caption>
* $0</table>
*
* NAN = $(RED NAN)
* SUP = <span style="vertical-align:super;font-size:smaller">$0</span>
* POWER = $1<sup>$2</sup>
* PLUSMN = &plusmn;
* INFIN = &infin;
* PLUSMNINF = &plusmn;&infin;
* LT = &lt;
* GT = &gt;
*
* Copyright: Copyright Digital Mars 2000 - 2011.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* 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);
}
}

10 changes: 10 additions & 0 deletions win32.mak
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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) $**

Expand Down Expand Up @@ -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$@ $**

Expand Down