Skip to content

Commit

Permalink
Implemented quotient and remainder.
Browse files Browse the repository at this point in the history
  • Loading branch information
namin committed Dec 30, 2011
1 parent e62d9ff commit 9f4eda4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
19 changes: 19 additions & 0 deletions src/compiler.scm
@@ -1,4 +1,5 @@
(load "tests-driver.scm")
(load "tests-4.1.1-req.scm")
(load "tests-3.4-req.scm")
(load "tests-3.3-req.scm")
(load "tests-3.2-req.scm")
Expand Down Expand Up @@ -261,6 +262,24 @@
(emit " shr $~s, %eax" fxshift)
(emit " mull ~s(%esp)" si))

(define (emit-div si env arg1 arg2)
(emit-expr si env arg2)
(emit " shr $~s, %eax" fxshift)
(emit-stack-save si)
(emit-expr (next-stack-index si) env arg1)
(emit " mov $0, %edx")
(emit " shr $~s, %eax" fxshift)
(emit " divl ~s(%esp)" si))

(define-primitive ($fxquotient si env arg1 arg2)
(emit-div si env arg1 arg2)
(emit " shl $~s, %eax" fxshift))

(define-primitive ($fxremainder si env arg1 arg2)
(emit-div si env arg1 arg2)
(emit " mov %edx, %eax")
(emit " shl $~s, %eax" fxshift))

(define-primitive (fxlogor si env arg1 arg2)
(emit-binop si env arg1 arg2)
(emit " or ~s(%esp), %eax" si))
Expand Down
33 changes: 32 additions & 1 deletion src/lib.scm
Expand Up @@ -68,4 +68,35 @@
[(not (fixnum? i)) (error)]
[(not (char? c)) (error)]
[(not (and (fx<= 0 i) (fx< i (string-length s)))) (error)]
[else ($string-set! s i c)]))
[else ($string-set! s i c)]))

(define-lib-primitive (liftneg f a b)
(cond
[(and (fx< a 0) (fx>= b 0))
(fx- 0 (f (fx- 0 a) b))]
[(and (fx>= a 0) (fx< b 0))
(fx- 0 (f a (fx- 0 b)))]
[(and (fx< a 0) (fx< b 0))
(f (fx- 0 a) (fx- 0 b))]
[else
(f a b)]))

(define-lib-primitive (liftneg1 f a b)
(cond
[(and (fx< a 0) (fx>= b 0))
(fx- 0 (f (fx- 0 a) b))]
[(and (fx>= a 0) (fx< b 0))
(f a (fx- 0 b))]
[(and (fx< a 0) (fx< b 0))
(fx- 0 (f (fx- 0 a) (fx- 0 b)))]
[else
(f a b)]))

(define-lib-primitive (fxquotient a b)
(liftneg (lambda (a b) ($fxquotient a b)) a b))

(define-lib-primitive (fxremainder a b)
(liftneg1 (lambda (a b) ($fxremainder a b)) a b))



21 changes: 1 addition & 20 deletions src/tests-4.1-req.scm
@@ -1,23 +1,4 @@
(add-tests-with-string-output "remainder/modulo/quotient"
[#\tab => "#\\tab\n"]
[(fxquotient 16 4) => "4\n"]
[(fxquotient 5 2) => "2\n"]
[(fxquotient -45 7) => "-6\n"]
[(fxquotient 10 -3) => "-3\n"]
[(fxquotient -17 -9) => "1\n"]

[(fxremainder 16 4) => "0\n"]
[(fxremainder 5 2) => "1\n"]
[(fxremainder -45 7) => "-3\n"]
[(fxremainder 10 -3) => "1\n"]
[(fxremainder -17 -9) => "-8\n"]

; [(fxmodulo 16 4) => "0\n"]
; [(fxmodulo 5 2) => "1\n"]
; [(fxmodulo -45 7) => "4\n"]
; [(fxmodulo 10 -3) => "-2\n"]
; [(fxmodulo -17 -9) => "-8\n"]
)
(load "tests-4.1.1-req.scm")

(add-tests-with-string-output "write-char"
[(begin
Expand Down
20 changes: 20 additions & 0 deletions src/tests-4.1.1-req.scm
@@ -0,0 +1,20 @@
(add-tests-with-string-output "remainder/modulo/quotient"
[#\tab => "#\\tab\n"]
[(fxquotient 16 4) => "4\n"]
[(fxquotient 5 2) => "2\n"]
[(fxquotient -45 7) => "-6\n"]
[(fxquotient 10 -3) => "-3\n"]
[(fxquotient -17 -9) => "1\n"]

[(fxremainder 16 4) => "0\n"]
[(fxremainder 5 2) => "1\n"]
[(fxremainder -45 7) => "-3\n"]
[(fxremainder 10 -3) => "1\n"]
[(fxremainder -17 -9) => "-8\n"]

; [(fxmodulo 16 4) => "0\n"]
; [(fxmodulo 5 2) => "1\n"]
; [(fxmodulo -45 7) => "4\n"]
; [(fxmodulo 10 -3) => "-2\n"]
; [(fxmodulo -17 -9) => "-8\n"]
)

0 comments on commit 9f4eda4

Please sign in to comment.