--- a/ShortFloat.st Tue May 03 11:08:52 2011 +0200
+++ b/ShortFloat.st Wed May 04 15:16:38 2011 +0200
@@ -649,6 +649,63 @@
"
!
+fastInverseSqrt
+ "return a rough but fast approximation of (1 / self sqrt).
+ The error is some 1%, which is ok for some 3D computatins.
+ Do not use this for now: it is non-portable and probably not speeding things up
+ much, unless inlined into the sender code.
+ The code is here as a reminder and might be later used as a hint for the inliner
+ (to speed up 3D computations, for example)"
+
+%{ /* NOCONTEXT */
+#if sizeof(float) == 4
+ float x, rslt;
+ OBJ newFloat;
+
+ x = __shortFloatVal(self);
+ {
+ float xhalf = 0.5f * x;
+ int i = *(int*)&x; // store floating-point bits in integer
+
+ i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
+ x = *(float*)&i; // convert new bits into float
+ x = x*(1.5f - xhalf*x*x); // One round of Newton's method
+ __qMKSFLOAT(newFloat, x);
+ RETURN ( newFloat );
+ }
+#endif
+%}.
+ ^ 1 / self sqrt
+
+ "
+ 10.0 asShortFloat fastInverseSqrt
+ (1 / 10.0 asShortFloat sqrt)
+ "
+
+ "
+ |a b t0 t1 t2|
+
+ a := 345 asShortFloat.
+ t0 := Time millisecondsToRun:[
+ 1000000 timesRepeat:[
+ ]
+ ].
+ t1 := Time millisecondsToRun:[
+ 1000000 timesRepeat:[
+ a fastInverseSqrt
+ ]
+ ].
+ t2 := Time millisecondsToRun:[
+ 1000000 timesRepeat:[
+ (1 / a sqrt)
+ ]
+ ].
+ Transcript show:'empty: '; showCR:t0.
+ Transcript show:'fast: '; showCR:t1.
+ Transcript show:'regular: '; showCR:t2.
+ "
+!
+
negated
"return myself negated"
@@ -1724,9 +1781,9 @@
!ShortFloat class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.104 2010-05-05 13:56:55 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.105 2011-05-04 13:16:38 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.104 2010-05-05 13:56:55 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.105 2011-05-04 13:16:38 cg Exp $'
! !