mandelbrot.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #| The Computer Language Benchmarks Game
  2. http://shootout.alioth.debian.org/
  3. Contributed by Per Bothner. Based on (i.e. a fairly direct transcription
  4. of, followed by major inlining) the "Java 6 -server"
  5. version contributed by Stefan Krause and slightly modified by Chad Whipkey.
  6. |#
  7. (define (compute (size :: int) (out :: java.io.PrintStream)) :: void
  8. (let* ((fac :: double (/ 2.0 size))
  9. (shift :: int (let ((sz8 :: int (remainder size 8)))
  10. (if (= sz8 0) 0 (- 8 sz8))))
  11. (buffer-size :: int 8192)
  12. (bbuffer :: byte[] (byte[] length: buffer-size))
  13. (buf-len :: int 0)
  14. (sz :: java.lang.Integer size))
  15. (out:format "P4\n%d %d\n" sz sz)
  16. (do ((y :: int 0 (+ y 1)))
  17. ((>= y size))
  18. (let* ((bits :: int 0)
  19. (Ci :: double (- (* y fac) 1.0)))
  20. (do ((x :: int 0 (+ x 1)))
  21. ((>= x size))
  22. (let ((Zr :: double 0.0)
  23. (Zi :: double 0.0)
  24. (Cr :: double (- (* x fac) 1.5))
  25. (ZrN :: double 0)
  26. (ZiN :: double 0)
  27. (i :: int 50))
  28. (let loop ()
  29. (set! Zi (+ (* 2.0 Zr Zi) Ci))
  30. (set! Zr (+ (- ZrN ZiN) Cr))
  31. (set! ZiN (* Zi Zi))
  32. (set! ZrN (* Zr Zr))
  33. (cond ((<= (+ ZiN ZrN) 4.0)
  34. (set! i (- i 1))
  35. (if (> i 0)
  36. (loop)))))
  37. (set! bits (bitwise-arithmetic-shift-left bits 1))
  38. (if (= i 0)
  39. (set! bits (+ bits 1)))
  40. (cond ((= (remainder x 8) 7)
  41. (set! (bbuffer buf-len) bits)
  42. (set! buf-len (+ buf-len 1))
  43. (cond ((= buf-len buffer-size)
  44. (out:write bbuffer 0 buffer-size)
  45. (set! buf-len 0)))
  46. (set! bits 0)))))
  47. (cond ((> shift 0)
  48. (set! bits (bitwise-arithmetic-shift-left bits shift))
  49. (set! (bbuffer buf-len) bits)
  50. (set! buf-len (+ buf-len 1))
  51. (cond ((= buf-len buffer-size)
  52. (out:write bbuffer 0 buffer-size)
  53. (set! buf-len 0)))))))
  54. (out:write bbuffer 0 buf-len)))
  55. (compute (string->number (cadr (command-line))) java.lang.System:out)