From 075d4aed617dbc2a152fa23ee6376cf0e75d2033 Mon Sep 17 00:00:00 2001 From: div0 Date: Sun, 8 Nov 2009 19:09:23 +0000 Subject: [PATCH] logarithm git-svn-id: svn://svn.icculus.org/nexuiz/trunk@8255 f962a42d-fe04-0410-a3ab-8c8b0445ebaa --- data/qcsrc/common/util-pre.qh | 4 ++++ data/qcsrc/common/util.qc | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/data/qcsrc/common/util-pre.qh b/data/qcsrc/common/util-pre.qh index e69de29bb..679b7e0a4 100644 --- a/data/qcsrc/common/util-pre.qh +++ b/data/qcsrc/common/util-pre.qh @@ -0,0 +1,4 @@ +#define log log_builtin \ + #undef log \ + /* turn the next use of "log" into a declaration of log_builtin */ + diff --git a/data/qcsrc/common/util.qc b/data/qcsrc/common/util.qc index 655d17c70..be8705032 100644 --- a/data/qcsrc/common/util.qc +++ b/data/qcsrc/common/util.qc @@ -1,3 +1,44 @@ +// checkextension wrapper for log +float sqrt(float f); // declared later +float exp(float f); // declared later +float pow(float f, float e); // declared later +float checkextension(string s); // declared later +float log_synth(float f) +{ + float p, l; + if(f < 0) + return sqrt(-1); // nan? -inf? + if(f == 0) + return sqrt(-1); // ACTUALLY this should rather be -inf, but we cannot create a +inf in QC + if(f + f == f) + return l; // +inf + if(f < 1) + { + f = 1 / f; + p = -1; + } + else + p = 1; + while(f > 2) + { + f = sqrt(f); + p *= 2; + } + // two steps are good enough + l = ((6-f) * f - 5) / 4.32808512266689022212; + l += exp(-l) * f - 1; + l += exp(-l) * f - 1; + print(ftos(l), " ", ftos(p), "\n"); + return l * p; +} +float log(float f) +{ + if(checkextension("DP_QC_LOG")) + return log_builtin(f); + else + return log_synth(f); +} + string wordwrap_buffer; void wordwrap_buffer_put(string s) -- 2.39.2