ocaml: phase 4 bitwise ops land/lor/lxor/lsl/lsr/asr + bits.ml baseline (+5 tests, 607 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The binop precedence table already had land/lor/lxor/lsl/lsr/asr (iter 0 setup) but eval-op fell through to 'unknown operator' for all of them. SX doesn't expose host bitwise primitives, so each is implemented in eval.sx via arithmetic on the host: land/lor/lxor: mask & shift loop, accumulating 1<<k digits lsl k: repeated * 2 k times lsr k: repeated floor (/ 2) k times asr: aliased to lsr (no sign extension at our bit width) bits.ml baseline: popcount via 'while m > 0 do if m land 1 = 1 then ... ; m := m lsr 1 done'. Sum of popcount(1023, 5, 1024, 0xff) = 10 + 2 + 1 + 8 = 21. 5 land 3 = 1 5 lor 3 = 7 5 lxor 3 = 6 1 lsl 8 = 256 256 lsr 4 = 16 41 baseline programs total.
This commit is contained in:
@@ -513,6 +513,79 @@
|
||||
((= op ">=") (>= lhs rhs))
|
||||
((= op "&&") (and lhs rhs))
|
||||
((= op "||") (or lhs rhs))
|
||||
;; Bitwise ops — implemented via arithmetic since SX doesn't
|
||||
;; expose host bitwise primitives.
|
||||
((= op "land")
|
||||
(let ((r 0) (f 1) (a lhs) (b rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (and (> a 0) (> b 0))
|
||||
(begin
|
||||
(when (and (= (mod a 2) 1) (= (mod b 2) 1))
|
||||
(set! r (+ r f)))
|
||||
(set! a (floor (/ a 2)))
|
||||
(set! b (floor (/ b 2)))
|
||||
(set! f (* f 2))
|
||||
(loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "lor")
|
||||
(let ((r 0) (f 1) (a lhs) (b rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (or (> a 0) (> b 0))
|
||||
(begin
|
||||
(when (or (= (mod a 2) 1) (= (mod b 2) 1))
|
||||
(set! r (+ r f)))
|
||||
(set! a (floor (/ a 2)))
|
||||
(set! b (floor (/ b 2)))
|
||||
(set! f (* f 2))
|
||||
(loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "lxor")
|
||||
(let ((r 0) (f 1) (a lhs) (b rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (or (> a 0) (> b 0))
|
||||
(begin
|
||||
(when (not (= (mod a 2) (mod b 2)))
|
||||
(set! r (+ r f)))
|
||||
(set! a (floor (/ a 2)))
|
||||
(set! b (floor (/ b 2)))
|
||||
(set! f (* f 2))
|
||||
(loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "lsl")
|
||||
(let ((r lhs) (k rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (> k 0) (begin (set! r (* r 2)) (set! k (- k 1)) (loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "lsr")
|
||||
(let ((r lhs) (k rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (> k 0)
|
||||
(begin (set! r (floor (/ r 2))) (set! k (- k 1)) (loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "asr")
|
||||
(let ((r lhs) (k rhs))
|
||||
(begin
|
||||
(define loop
|
||||
(fn ()
|
||||
(when (> k 0)
|
||||
(begin (set! r (floor (/ r 2))) (set! k (- k 1)) (loop)))))
|
||||
(loop)
|
||||
r)))
|
||||
((= op "or") (or lhs rhs))
|
||||
((= op "|>") (rhs lhs))
|
||||
(else (error (str "ocaml-eval: unknown operator " op))))))
|
||||
|
||||
Reference in New Issue
Block a user