prefix-scheme48.scm 9.4 KB

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