Created
July 26, 2016 06:53
-
-
Save ktakashi/76f44fc16eefec2d2d1abf3ccfa48b11 to your computer and use it in GitHub Desktop.
Fast inverse square root in R6RS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!r6rs | |
(import (rnrs)) | |
(define (fast-inv-sqrt f) | |
(let ((bv (make-bytevector 4))) | |
(bytevector-ieee-single-native-set! bv 0 f) | |
(let* ((i0 (bitwise-arithmetic-shift (bytevector-s32-native-ref bv 0) -1)) | |
(i1 (- #x5f3759df i0))) | |
(bytevector-s32-native-set! bv 0 i1) | |
(let ((y (bytevector-ieee-single-native-ref bv 0))) | |
(* y (- 1.5 (* 0.5 f y y))))))) | |
(let loop () | |
(let ((line (read (current-input-port)))) | |
(unless (eof-object? line) | |
(display (fast-inv-sqrt line)) (newline) | |
(loop)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment