From a02983e4ef3a48a13a5501fa359742976bf03a2e Mon Sep 17 00:00:00 2001 From: Ilya Yaroshenko Date: Sun, 22 Feb 2015 18:41:50 +0300 Subject: [PATCH] CTFE-able poly This PR allows many special math function be executed at compile time. reduced code size fix --- std/math.d | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/std/math.d b/std/math.d index f37ae3abd81..6e210c39a5d 100644 --- a/std/math.d +++ b/std/math.d @@ -6240,14 +6240,7 @@ body } else { - ptrdiff_t i = A.length - 1; - typeof(return) r = A[i]; - while (--i >= 0) - { - r *= x; - r += A[i]; - } - return r; + return polyImplBase(x, A); } } @@ -6272,15 +6265,31 @@ body assert(poly(x, pp) == y); } -private real polyImpl(real x, in real[] A) @trusted pure nothrow @nogc -in +unittest { + static assert(poly(3.0, [1.0, 2.0, 3.0]) == 34); +} + +private Unqual!(CommonType!(T1, T2)) polyImplBase(T1, T2)(T1 x, in T2[] A) @trusted pure nothrow @nogc + if (isFloatingPoint!T1 && isFloatingPoint!T2) { - assert(A.length > 0); + ptrdiff_t i = A.length - 1; + typeof(return) r = A[i]; + while (--i >= 0) + { + r *= x; + r += A[i]; + } + return r; } -body + +private real polyImpl(real x, in real[] A) @trusted pure nothrow @nogc { version (D_InlineAsm_X86) { + if(__ctfe) + { + return polyImplBase(x, A); + } version (Windows) { // BUG: This code assumes a frame pointer in EBP. @@ -6429,14 +6438,7 @@ body } else { - ptrdiff_t i = A.length - 1; - real r = A[i]; - while (--i >= 0) - { - r *= x; - r += A[i]; - } - return r; + return polyImplBase(x, A); } }