prefix-chicken.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;INSERTCODE
  2. ;------------------------------------------------------------------------------
  3. (define (time* thunk)
  4. (let* ((start (receive (u s) (cpu-time) (+ u s)))
  5. (start-real (current-milliseconds)))
  6. (let ((result (thunk)))
  7. (let* ((end (receive (u s) (cpu-time) (+ u s)))
  8. (end-real (current-milliseconds)))
  9. (let ((cpu (- end start))
  10. (real (- end-real start-real)))
  11. (display "cpu time: ")
  12. (display cpu)
  13. (display " real time: ")
  14. (display real)
  15. (newline)
  16. result)))))
  17. (define (run-bench name count ok? run)
  18. (let loop ((i count) (result '(undefined)))
  19. (if (< 0 i)
  20. (loop (- i 1) (run))
  21. result)))
  22. (define (run-benchmark name count ok? run-maker . args)
  23. (newline)
  24. (let* ((run (apply run-maker args))
  25. (result (time* (lambda () (run-bench name count ok? run)))))
  26. (if (not (ok? result))
  27. (begin
  28. (display "*** wrong result ***")
  29. (newline)
  30. (display "*** got: ")
  31. (write result)
  32. (newline))))
  33. (exit 0))
  34. (define (fatal-error . args)
  35. (for-each display args)
  36. (newline)
  37. (exit 1))
  38. (define (call-with-output-file/truncate filename proc)
  39. (call-with-output-file filename proc))
  40. ;------------------------------------------------------------------------------
  41. ; Macros...
  42. (define-macro (def-macro form . body)
  43. `(define-macro ,form (let () ,@body)))
  44. (if-fixflo
  45. (begin
  46. ; Specialize fixnum and flonum arithmetic.
  47. ;; This code should be used when f64vectors are available.
  48. ;(def-macro (FLOATvector-const . lst) `',(list->f64vector lst))
  49. ;(def-macro (FLOATvector? x) `(f64vector? ,x))
  50. ;(def-macro (FLOATvector . lst) `(f64vector ,@lst))
  51. ;(def-macro (FLOATmake-vector n . init) `(make-f64vector ,n ,@init))
  52. ;(def-macro (FLOATvector-ref v i) `(f64vector-ref ,v ,i))
  53. ;(def-macro (FLOATvector-set! v i x) `(f64vector-set! ,v ,i ,x))
  54. ;(def-macro (FLOATvector-length v) `(f64vector-length ,v))
  55. ;
  56. ;(def-macro (nuc-const . lst)
  57. ; `',(list->vector
  58. ; (map (lambda (x)
  59. ; (if (vector? x)
  60. ; (list->f64vector (vector->list x))
  61. ; x))
  62. ; lst)))
  63. (def-macro (FLOATvector-const . lst) `',(list->vector lst))
  64. (def-macro (FLOATvector? x) `(vector? ,x))
  65. (def-macro (FLOATvector . lst) `(vector ,@lst))
  66. (def-macro (FLOATmake-vector n . init) `(make-vector ,n ,@init))
  67. (def-macro (FLOATvector-ref v i) `(vector-ref ,v ,i))
  68. (def-macro (FLOATvector-set! v i x) `(vector-set! ,v ,i ,x))
  69. (def-macro (FLOATvector-length v) `(vector-length ,v))
  70. (def-macro (nuc-const . lst)
  71. `',(list->vector lst))
  72. (def-macro (FLOAT+ . lst) `(fp+ ,@lst))
  73. (def-macro (FLOAT- . lst) (if (= (length lst) 1) `(fpneg ,@lst) `(fp- ,@lst)))
  74. (def-macro (FLOAT* . lst) `(fp* ,@lst))
  75. (def-macro (FLOAT/ . lst) (if (= (length lst) 1) `(fp/ 1.0 ,@lst) `(fp/ ,@lst)))
  76. (def-macro (FLOAT= . lst) `(fp= ,@lst))
  77. (def-macro (FLOAT< . lst) `(fp< ,@lst))
  78. (def-macro (FLOAT<= . lst) `(fp<= ,@lst))
  79. (def-macro (FLOAT> . lst) `(fp> ,@lst))
  80. (def-macro (FLOAT>= . lst) `(fp>= ,@lst))
  81. (def-macro (FLOATnegative? . lst) `(fp< ,@lst 0.0))
  82. (def-macro (FLOATpositive? . lst) `(fp> ,@lst 0.0))
  83. (def-macro (FLOATzero? . lst) `(fp= ,@lst 0.0))
  84. (def-macro (FLOATabs . lst) `(abs ,@lst))
  85. (def-macro (FLOATsin . lst) `(sin ,@lst))
  86. (def-macro (FLOATcos . lst) `(cos ,@lst))
  87. (def-macro (FLOATatan . lst) `(atan ,@lst))
  88. (def-macro (FLOATsqrt . lst) `(sqrt ,@lst))
  89. (def-macro (FLOATmin . lst) `(fpmin ,@lst))
  90. (def-macro (FLOATmax . lst) `(fpmax ,@lst))
  91. (def-macro (FLOATround . lst) `(round ,@lst))
  92. (def-macro (FLOATinexact->exact . lst) `(inexact->exact ,@lst))
  93. (define (GENERIC+ x y) (+ x y))
  94. (define (GENERIC- x y) (- x y))
  95. (define (GENERIC* x y) (* x y))
  96. (define (GENERIC/ x y) (/ x y))
  97. (define (GENERICquotient x y) (quotient x y))
  98. (define (GENERICremainder x y) (remainder x y))
  99. (define (GENERICmodulo x y) (modulo x y))
  100. (define (GENERIC= x y) (= x y))
  101. (define (GENERIC< x y) (< x y))
  102. (define (GENERIC<= x y) (<= x y))
  103. (define (GENERIC> x y) (> x y))
  104. (define (GENERIC>= x y) (>= x y))
  105. (define (GENERICexpt x y) (expt x y))
  106. (def-macro (+ . lst) `(fx+ ,@lst))
  107. (def-macro (- . lst) (if (= (length lst) 1) `(fxneg ,@lst) `(fx- ,@lst)))
  108. (def-macro (* . lst) `(fx* ,@lst))
  109. (def-macro (quotient . lst) `(fx/ ,@lst))
  110. (def-macro (modulo . lst) `(fxmod ,@lst))
  111. ;(def-macro (remainder . lst) `(remainder ,@lst))
  112. (def-macro (= . lst) `(fx= ,@lst))
  113. (def-macro (< . lst) `(fx< ,@lst))
  114. (def-macro (<= . lst) `(fx<= ,@lst))
  115. (def-macro (> . lst) `(fx> ,@lst))
  116. (def-macro (>= . lst) `(fx>= ,@lst))
  117. (def-macro (negative? . lst) `(fx< ,@lst 0))
  118. (def-macro (positive? . lst) `(fx> ,@lst 0))
  119. (def-macro (zero? . lst) `(fx= ,@lst 0))
  120. (def-macro (odd? . lst) `(fx= (fxmod ,@lst 2) 1))
  121. (def-macro (even? . lst) `(fx= (fxmod ,@lst 2) 0))
  122. (def-macro (bitwise-or . lst) `(bitwise-ior ,@lst))
  123. ;(def-macro (bitwise-and . lst) `(bitwise-and ,@lst))
  124. ;(def-macro (bitwise-not . lst) `(bitwise-not ,@lst))
  125. )
  126. (begin
  127. ; Don't specialize fixnum and flonum arithmetic.
  128. (def-macro (FLOATvector-const . lst) `',(list->vector lst))
  129. (def-macro (FLOATvector? x) `(vector? ,x))
  130. (def-macro (FLOATvector . lst) `(vector ,@lst))
  131. (def-macro (FLOATmake-vector n . init) `(make-vector ,n ,@init))
  132. (def-macro (FLOATvector-ref v i) `(vector-ref ,v ,i))
  133. (def-macro (FLOATvector-set! v i x) `(vector-set! ,v ,i ,x))
  134. (def-macro (FLOATvector-length v) `(vector-length ,v))
  135. (def-macro (nuc-const . lst)
  136. `',(list->vector lst))
  137. (def-macro (FLOAT+ . lst) `(+ ,@lst))
  138. (def-macro (FLOAT- . lst) `(- ,@lst))
  139. (def-macro (FLOAT* . lst) `(* ,@lst))
  140. (def-macro (FLOAT/ . lst) `(/ ,@lst))
  141. (def-macro (FLOAT= . lst) `(= ,@lst))
  142. (def-macro (FLOAT< . lst) `(< ,@lst))
  143. (def-macro (FLOAT<= . lst) `(<= ,@lst))
  144. (def-macro (FLOAT> . lst) `(> ,@lst))
  145. (def-macro (FLOAT>= . lst) `(>= ,@lst))
  146. (def-macro (FLOATnegative? . lst) `(negative? ,@lst))
  147. (def-macro (FLOATpositive? . lst) `(positive? ,@lst))
  148. (def-macro (FLOATzero? . lst) `(zero? ,@lst))
  149. (def-macro (FLOATabs . lst) `(abs ,@lst))
  150. (def-macro (FLOATsin . lst) `(sin ,@lst))
  151. (def-macro (FLOATcos . lst) `(cos ,@lst))
  152. (def-macro (FLOATatan . lst) `(atan ,@lst))
  153. (def-macro (FLOATsqrt . lst) `(sqrt ,@lst))
  154. (def-macro (FLOATmin . lst) `(min ,@lst))
  155. (def-macro (FLOATmax . lst) `(max ,@lst))
  156. (def-macro (FLOATround . lst) `(round ,@lst))
  157. (def-macro (FLOATinexact->exact . lst) `(inexact->exact ,@lst))
  158. (def-macro (GENERIC+ . lst) `(+ ,@lst))
  159. (def-macro (GENERIC- . lst) `(- ,@lst))
  160. (def-macro (GENERIC* . lst) `(* ,@lst))
  161. (def-macro (GENERIC/ . lst) `(/ ,@lst))
  162. (def-macro (GENERICquotient . lst) `(quotient ,@lst))
  163. (def-macro (GENERICremainder . lst) `(remainder ,@lst))
  164. (def-macro (GENERICmodulo . lst) `(modulo ,@lst))
  165. (def-macro (GENERIC= . lst) `(= ,@lst))
  166. (def-macro (GENERIC< . lst) `(< ,@lst))
  167. (def-macro (GENERIC<= . lst) `(<= ,@lst))
  168. (def-macro (GENERIC> . lst) `(> ,@lst))
  169. (def-macro (GENERIC>= . lst) `(>= ,@lst))
  170. (def-macro (GENERICexpt . lst) `(expt ,@lst))
  171. )
  172. )
  173. ;------------------------------------------------------------------------------