Browse Source

added approx log

Alexander Rose 4 years ago
parent
commit
a7901c53ce
2 changed files with 25 additions and 6 deletions
  1. 20 2
      src/mol-math/approx.ts
  2. 5 4
      src/mol-util/float-packing.ts

+ 20 - 2
src/mol-math/approx.ts

@@ -15,8 +15,26 @@ export function fasterPow2(v: number) {
     const clipNumber = (v < -126) ? -126 : v;
     _i_fasterPow2[0] = ((1 << 23) * (clipNumber + 126.94269504));
     return _f_fasterPow2[0];
-};
+}
 
 export function fasterExp(v: number) {
     return fasterPow2(1.442695040 * v);
-};
+}
+
+const _a_fasterLog2 = new ArrayBuffer(4);
+const _i_fasterLog2 = new Int32Array(_a_fasterLog2);
+const _f_fasterLog2 = new Float32Array(_a_fasterLog2);
+
+export function fasterLog2(v: number) {
+    _f_fasterLog2[0] = v;
+    const t = _i_fasterLog2[0] * 1.1920928955078125e-7;
+    return t - 126.94269504;
+}
+
+export function fasterLog(v: number) {
+    return 0.6931471805599453 * fasterLog2(v);
+}
+
+export function fasterLog10(v: number) {
+    return 0.30102999566398114 * fasterLog2(v);
+}

+ 5 - 4
src/mol-util/float-packing.ts

@@ -1,19 +1,20 @@
 /**
- * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
 import { clamp } from '../mol-math/interpolate';
+import { fasterExp, fasterLog } from '../mol-math/approx';
 
 const maxFloat = 10000.0; // NOTE same constant is set in shaders
-const floatLogFactor = Math.log(maxFloat + 1.0);
+const floatLogFactor = fasterLog(maxFloat + 1.0);
 
 /** encode float logarithmically */
-export function encodeFloatLog(value: number) { return Math.log(value + 1.0) / floatLogFactor; }
+export function encodeFloatLog(value: number) { return fasterLog(value + 1.0) / floatLogFactor; }
 
 /** decode logarithmically encoded float */
-export function decodeFloatLog(value: number) { return Math.exp(value * floatLogFactor) - 1.0; }
+export function decodeFloatLog(value: number) { return fasterExp(value * floatLogFactor) - 1.0; }
 
 /** encode float as normalized rgb triplet */
 export function encodeFloatRGB(value: number) {