prefix-mzscheme.scm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. ;INSERTCODE
  2. ;------------------------------------------------------------------------------
  3. (define (run-bench name count ok? run)
  4. (let loop ((i 0) (result (list 'undefined)))
  5. (if (< i count)
  6. (loop (+ i 1) (run))
  7. result)))
  8. (define (run-benchmark name count ok? run-maker . args)
  9. (newline)
  10. (let* ((run (apply run-maker args))
  11. (result (time (run-bench name count ok? run))))
  12. (if (not (ok? result))
  13. (begin
  14. (display "*** wrong result ***")
  15. (newline)
  16. (display "*** got: ")
  17. (write result)
  18. (newline))))
  19. (exit 0))
  20. (define (fatal-error . args)
  21. (apply error #f args))
  22. (define (call-with-output-file/truncate filename proc)
  23. (call-with-output-file filename proc 'truncate))
  24. ;------------------------------------------------------------------------------
  25. ; Macros...
  26. (if-fixflo
  27. (begin
  28. ; Specialize fixnum and flonum arithmetic.
  29. (define-syntax FLOATvector-const
  30. (syntax-rules ()
  31. ((FLOATvector-const x ...) '#(x ...))))
  32. (define-syntax FLOATvector?
  33. (syntax-rules ()
  34. ((FLOATvector? x) (vector? x))))
  35. (define-syntax FLOATvector
  36. (syntax-rules ()
  37. ((FLOATvector x ...) (vector x ...))))
  38. (define-syntax FLOATmake-vector
  39. (syntax-rules ()
  40. ((FLOATmake-vector n) (make-vector n 0.0))
  41. ((FLOATmake-vector n init) (make-vector n init))))
  42. (define-syntax FLOATvector-ref
  43. (syntax-rules ()
  44. ((FLOATvector-ref v i) (vector-ref v i))))
  45. (define-syntax FLOATvector-set!
  46. (syntax-rules ()
  47. ((FLOATvector-set! v i x) (vector-set! v i x))))
  48. (define-syntax FLOATvector-length
  49. (syntax-rules ()
  50. ((FLOATvector-length v) (vector-length v))))
  51. (define-syntax nuc-const
  52. (syntax-rules ()
  53. ((FLOATnuc-const x ...) '#(x ...))))
  54. (define-syntax FLOAT+
  55. (syntax-rules ()
  56. ((FLOAT+ x ...) (fl+ x ...))))
  57. (define-syntax FLOAT-
  58. (syntax-rules ()
  59. ((FLOAT- x ...) (fl- x ...))))
  60. (define-syntax FLOAT*
  61. (syntax-rules ()
  62. ((FLOAT* x ...) (fl* x ...))))
  63. (define-syntax FLOAT/
  64. (syntax-rules ()
  65. ((FLOAT/ x ...) (fl/ x ...))))
  66. (define-syntax FLOAT=
  67. (syntax-rules ()
  68. ((FLOAT= x y) (fl= x y))))
  69. (define-syntax FLOAT<
  70. (syntax-rules ()
  71. ((FLOAT< x y) (fl< x y))))
  72. (define-syntax FLOAT<=
  73. (syntax-rules ()
  74. ((FLOAT<= x y) (fl<= x y))))
  75. (define-syntax FLOAT>
  76. (syntax-rules ()
  77. ((FLOAT> x y) (fl> x y))))
  78. (define-syntax FLOAT>=
  79. (syntax-rules ()
  80. ((FLOAT>= x y) (fl>= x y))))
  81. (define-syntax FLOATnegative?
  82. (syntax-rules ()
  83. ((FLOATnegative? x) (flnegative? x))))
  84. (define-syntax FLOATpositive?
  85. (syntax-rules ()
  86. ((FLOATpositive? x) (flpositive? x))))
  87. (define-syntax FLOATzero?
  88. (syntax-rules ()
  89. ((FLOATzero? x) (flzero? x))))
  90. (define-syntax FLOATabs
  91. (syntax-rules ()
  92. ((FLOATabs x) (flabs x))))
  93. (define-syntax FLOATsin
  94. (syntax-rules ()
  95. ((FLOATsin x) (flsin x))))
  96. (define-syntax FLOATcos
  97. (syntax-rules ()
  98. ((FLOATcos x) (flcos x))))
  99. (define-syntax FLOATatan
  100. (syntax-rules ()
  101. ((FLOATatan x) (flatan x))))
  102. (define-syntax FLOATsqrt
  103. (syntax-rules ()
  104. ((FLOATsqrt x) (flsqrt x))))
  105. (define-syntax FLOATmin
  106. (syntax-rules ()
  107. ((FLOATmin x y) (flmin x y))))
  108. (define-syntax FLOATmax
  109. (syntax-rules ()
  110. ((FLOATmax x y) (flmax x y))))
  111. (define-syntax FLOATround
  112. (syntax-rules ()
  113. ((FLOATround x) (flround x))))
  114. (define-syntax FLOATinexact->exact
  115. (syntax-rules ()
  116. ((FLOATinexact->exact x) (inexact->exact x))))
  117. (define (GENERIC+ x y) (+ x y))
  118. (define (GENERIC- x y) (- x y))
  119. (define (GENERIC* x y) (* x y))
  120. (define (GENERIC/ x y) (/ x y))
  121. (define (GENERICquotient x y) (quotient x y))
  122. (define (GENERICremainder x y) (remainder x y))
  123. (define (GENERICmodulo x y) (modulo x y))
  124. (define (GENERIC= x y) (= x y))
  125. (define (GENERIC< x y) (< x y))
  126. (define (GENERIC<= x y) (<= x y))
  127. (define (GENERIC> x y) (> x y))
  128. (define (GENERIC>= x y) (>= x y))
  129. (define (GENERICexpt x y) (expt x y))
  130. (define-syntax +
  131. (syntax-rules ()
  132. ((+ x ...) (fx+ x ...))))
  133. (define-syntax -
  134. (syntax-rules ()
  135. ((- x ...) (fx- x ...))))
  136. (define-syntax *
  137. (syntax-rules ()
  138. ((* x ...) (fx* x ...))))
  139. (define-syntax quotient
  140. (syntax-rules ()
  141. ((quotient x ...) (fxquotient x ...))))
  142. (define-syntax modulo
  143. (syntax-rules ()
  144. ((modulo x ...) (fxmodulo x ...))))
  145. (define-syntax remainder
  146. (syntax-rules ()
  147. ((remainder x ...) (fxremainder x ...))))
  148. (define-syntax =
  149. (syntax-rules ()
  150. ((= x y) (fx= x y))))
  151. (define-syntax <
  152. (syntax-rules ()
  153. ((< x y) (fx< x y))))
  154. (define-syntax <=
  155. (syntax-rules ()
  156. ((<= x y) (fx<= x y))))
  157. (define-syntax >
  158. (syntax-rules ()
  159. ((> x y) (fx> x y))))
  160. (define-syntax >=
  161. (syntax-rules ()
  162. ((>= x y) (fx>= x y))))
  163. (define-syntax negative?
  164. (syntax-rules ()
  165. ((negative? x) (fxnegative? x))))
  166. (define-syntax positive?
  167. (syntax-rules ()
  168. ((positive? x) (fxpositive? x))))
  169. (define-syntax zero?
  170. (syntax-rules ()
  171. ((zero? x) (fxzero? x))))
  172. (define-syntax odd?
  173. (syntax-rules ()
  174. ((odd? x) (fxodd? x))))
  175. (define-syntax even?
  176. (syntax-rules ()
  177. ((even? x) (fxeven? x))))
  178. (define-syntax bitwise-or
  179. (syntax-rules ()
  180. ((bitwise-or x y) (fxior x y))))
  181. (define-syntax bitwise-and
  182. (syntax-rules ()
  183. ((bitwise-and x y) (fxand x y))))
  184. (define-syntax bitwise-not
  185. (syntax-rules ()
  186. ((bitwise-not x) (fxnot x))))
  187. )
  188. (begin
  189. ; Don't specialize fixnum and flonum arithmetic.
  190. (define-syntax FLOATvector-const
  191. (syntax-rules ()
  192. ((FLOATvector-const x ...) '#(x ...))))
  193. (define-syntax FLOATvector?
  194. (syntax-rules ()
  195. ((FLOATvector? x) (vector? x))))
  196. (define-syntax FLOATvector
  197. (syntax-rules ()
  198. ((FLOATvector x ...) (vector x ...))))
  199. (define-syntax FLOATmake-vector
  200. (syntax-rules ()
  201. ((FLOATmake-vector n) (make-vector n 0.0))
  202. ((FLOATmake-vector n init) (make-vector n init))))
  203. (define-syntax FLOATvector-ref
  204. (syntax-rules ()
  205. ((FLOATvector-ref v i) (vector-ref v i))))
  206. (define-syntax FLOATvector-set!
  207. (syntax-rules ()
  208. ((FLOATvector-set! v i x) (vector-set! v i x))))
  209. (define-syntax FLOATvector-length
  210. (syntax-rules ()
  211. ((FLOATvector-length v) (vector-length v))))
  212. (define-syntax nuc-const
  213. (syntax-rules ()
  214. ((FLOATnuc-const x ...) '#(x ...))))
  215. (define-syntax FLOAT+
  216. (syntax-rules ()
  217. ((FLOAT+ x ...) (+ x ...))))
  218. (define-syntax FLOAT-
  219. (syntax-rules ()
  220. ((FLOAT- x ...) (- x ...))))
  221. (define-syntax FLOAT*
  222. (syntax-rules ()
  223. ((FLOAT* x ...) (* x ...))))
  224. (define-syntax FLOAT/
  225. (syntax-rules ()
  226. ((FLOAT/ x ...) (/ x ...))))
  227. (define-syntax FLOAT=
  228. (syntax-rules ()
  229. ((FLOAT= x y) (= x y))))
  230. (define-syntax FLOAT<
  231. (syntax-rules ()
  232. ((FLOAT< x y) (< x y))))
  233. (define-syntax FLOAT<=
  234. (syntax-rules ()
  235. ((FLOAT<= x y) (<= x y))))
  236. (define-syntax FLOAT>
  237. (syntax-rules ()
  238. ((FLOAT> x y) (> x y))))
  239. (define-syntax FLOAT>=
  240. (syntax-rules ()
  241. ((FLOAT>= x y) (>= x y))))
  242. (define-syntax FLOATnegative?
  243. (syntax-rules ()
  244. ((FLOATnegative? x) (negative? x))))
  245. (define-syntax FLOATpositive?
  246. (syntax-rules ()
  247. ((FLOATpositive? x) (positive? x))))
  248. (define-syntax FLOATzero?
  249. (syntax-rules ()
  250. ((FLOATzero? x) (zero? x))))
  251. (define-syntax FLOATabs
  252. (syntax-rules ()
  253. ((FLOATabs x) (abs x))))
  254. (define-syntax FLOATsin
  255. (syntax-rules ()
  256. ((FLOATsin x) (sin x))))
  257. (define-syntax FLOATcos
  258. (syntax-rules ()
  259. ((FLOATcos x) (cos x))))
  260. (define-syntax FLOATatan
  261. (syntax-rules ()
  262. ((FLOATatan x) (atan x))))
  263. (define-syntax FLOATsqrt
  264. (syntax-rules ()
  265. ((FLOATsqrt x) (sqrt x))))
  266. (define-syntax FLOATmin
  267. (syntax-rules ()
  268. ((FLOATmin x y) (min x y))))
  269. (define-syntax FLOATmax
  270. (syntax-rules ()
  271. ((FLOATmax x y) (max x y))))
  272. (define-syntax FLOATround
  273. (syntax-rules ()
  274. ((FLOATround x) (round x))))
  275. (define-syntax FLOATinexact->exact
  276. (syntax-rules ()
  277. ((FLOATinexact->exact x) (inexact->exact x))))
  278. ; Generic arithmetic.
  279. (define-syntax GENERIC+
  280. (syntax-rules ()
  281. ((GENERIC+ x ...) (+ x ...))))
  282. (define-syntax GENERIC-
  283. (syntax-rules ()
  284. ((GENERIC- x ...) (- x ...))))
  285. (define-syntax GENERIC*
  286. (syntax-rules ()
  287. ((GENERIC* x ...) (* x ...))))
  288. (define-syntax GENERIC/
  289. (syntax-rules ()
  290. ((GENERIC/ x ...) (/ x ...))))
  291. (define-syntax GENERICquotient
  292. (syntax-rules ()
  293. ((GENERICquotient x y) (quotient x y))))
  294. (define-syntax GENERICremainder
  295. (syntax-rules ()
  296. ((GENERICremainder x y) (remainder x y))))
  297. (define-syntax GENERICmodulo
  298. (syntax-rules ()
  299. ((GENERICmodulo x y) (modulo x y))))
  300. (define-syntax GENERIC=
  301. (syntax-rules ()
  302. ((GENERIC= x y) (= x y))))
  303. (define-syntax GENERIC<
  304. (syntax-rules ()
  305. ((GENERIC< x y) (< x y))))
  306. (define-syntax GENERIC<=
  307. (syntax-rules ()
  308. ((GENERIC<= x y) (<= x y))))
  309. (define-syntax GENERIC>
  310. (syntax-rules ()
  311. ((GENERIC> x y) (> x y))))
  312. (define-syntax GENERIC>=
  313. (syntax-rules ()
  314. ((GENERIC>= x y) (>= x y))))
  315. (define-syntax GENERICexpt
  316. (syntax-rules ()
  317. ((GENERICexpt x y) (expt x y))))
  318. )
  319. )
  320. ;------------------------------------------------------------------------------