60.upstream.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ;;;; "logical.scm", bit access and operations for integers for Scheme
  2. ;;; Copyright (C) 1991, 1993, 2001, 2003, 2005 Aubrey Jaffer
  3. ;
  4. ;Permission to copy this software, to modify it, to redistribute it,
  5. ;to distribute modified versions, and to use it for any purpose is
  6. ;granted, subject to the following restrictions and understandings.
  7. ;
  8. ;1. Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2. I have made no warranty or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3. In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19. (define logical:boole-xor
  20. '#(#(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
  21. #(1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14)
  22. #(2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13)
  23. #(3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12)
  24. #(4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11)
  25. #(5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10)
  26. #(6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9)
  27. #(7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8)
  28. #(8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7)
  29. #(9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6)
  30. #(10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5)
  31. #(11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4)
  32. #(12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3)
  33. #(13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2)
  34. #(14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1)
  35. #(15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)))
  36. (define logical:boole-and
  37. '#(#(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  38. #(0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)
  39. #(0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2)
  40. #(0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3)
  41. #(0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4)
  42. #(0 1 0 1 4 5 4 5 0 1 0 1 4 5 4 5)
  43. #(0 0 2 2 4 4 6 6 0 0 2 2 4 4 6 6)
  44. #(0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7)
  45. #(0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8)
  46. #(0 1 0 1 0 1 0 1 8 9 8 9 8 9 8 9)
  47. #(0 0 2 2 0 0 2 2 8 8 10 10 8 8 10 10)
  48. #(0 1 2 3 0 1 2 3 8 9 10 11 8 9 10 11)
  49. #(0 0 0 0 4 4 4 4 8 8 8 8 12 12 12 12)
  50. #(0 1 0 1 4 5 4 5 8 9 8 9 12 13 12 13)
  51. #(0 0 2 2 4 4 6 6 8 8 10 10 12 12 14 14)
  52. #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)))
  53. (define (logical:ash-4 x)
  54. (if (negative? x)
  55. (+ -1 (quotient (+ 1 x) 16))
  56. (quotient x 16)))
  57. (define (logical:reduce op4 ident)
  58. (lambda args
  59. (do ((res ident (op4 res (car rgs) 1 0))
  60. (rgs args (cdr rgs)))
  61. ((null? rgs) res))))
  62. ;@
  63. (define logand
  64. (letrec
  65. ((lgand
  66. (lambda (n2 n1 scl acc)
  67. (cond ((= n1 n2) (+ acc (* scl n1)))
  68. ((zero? n2) acc)
  69. ((zero? n1) acc)
  70. (else (lgand (logical:ash-4 n2)
  71. (logical:ash-4 n1)
  72. (* 16 scl)
  73. (+ (* (vector-ref (vector-ref logical:boole-and
  74. (modulo n1 16))
  75. (modulo n2 16))
  76. scl)
  77. acc)))))))
  78. (logical:reduce lgand -1)))
  79. ;@
  80. (define logior
  81. (letrec
  82. ((lgior
  83. (lambda (n2 n1 scl acc)
  84. (cond ((= n1 n2) (+ acc (* scl n1)))
  85. ((zero? n2) (+ acc (* scl n1)))
  86. ((zero? n1) (+ acc (* scl n2)))
  87. (else (lgior (logical:ash-4 n2)
  88. (logical:ash-4 n1)
  89. (* 16 scl)
  90. (+ (* (- 15 (vector-ref
  91. (vector-ref logical:boole-and
  92. (- 15 (modulo n1 16)))
  93. (- 15 (modulo n2 16))))
  94. scl)
  95. acc)))))))
  96. (logical:reduce lgior 0)))
  97. ;@
  98. (define logxor
  99. (letrec
  100. ((lgxor
  101. (lambda (n2 n1 scl acc)
  102. (cond ((= n1 n2) acc)
  103. ((zero? n2) (+ acc (* scl n1)))
  104. ((zero? n1) (+ acc (* scl n2)))
  105. (else (lgxor (logical:ash-4 n2)
  106. (logical:ash-4 n1)
  107. (* 16 scl)
  108. (+ (* (vector-ref (vector-ref logical:boole-xor
  109. (modulo n1 16))
  110. (modulo n2 16))
  111. scl)
  112. acc)))))))
  113. (logical:reduce lgxor 0)))
  114. ;@
  115. (define (lognot n) (- -1 n))
  116. ;@
  117. (define (logtest n1 n2)
  118. (not (zero? (logand n1 n2))))
  119. ;@
  120. (define (logbit? index n)
  121. (logtest (expt 2 index) n))
  122. ;@
  123. (define (copy-bit index to bool)
  124. (if bool
  125. (logior to (arithmetic-shift 1 index))
  126. (logand to (lognot (arithmetic-shift 1 index)))))
  127. ;@
  128. (define (bitwise-if mask n0 n1)
  129. (logior (logand mask n0)
  130. (logand (lognot mask) n1)))
  131. ;@
  132. (define (bit-field n start end)
  133. (logand (lognot (ash -1 (- end start)))
  134. (arithmetic-shift n (- start))))
  135. ;@
  136. (define (copy-bit-field to from start end)
  137. (bitwise-if (arithmetic-shift (lognot (ash -1 (- end start))) start)
  138. (arithmetic-shift from start)
  139. to))
  140. ;@
  141. (define (rotate-bit-field n count start end)
  142. (define width (- end start))
  143. (set! count (modulo count width))
  144. (let ((mask (lognot (ash -1 width))))
  145. (define zn (logand mask (arithmetic-shift n (- start))))
  146. (logior (arithmetic-shift
  147. (logior (logand mask (arithmetic-shift zn count))
  148. (arithmetic-shift zn (- count width)))
  149. start)
  150. (logand (lognot (ash mask start)) n))))
  151. ;@
  152. (define (arithmetic-shift n count)
  153. (if (negative? count)
  154. (let ((k (expt 2 (- count))))
  155. (if (negative? n)
  156. (+ -1 (quotient (+ 1 n) k))
  157. (quotient n k)))
  158. (* (expt 2 count) n)))
  159. ;@
  160. (define integer-length
  161. (letrec ((intlen (lambda (n tot)
  162. (case n
  163. ((0 -1) (+ 0 tot))
  164. ((1 -2) (+ 1 tot))
  165. ((2 3 -3 -4) (+ 2 tot))
  166. ((4 5 6 7 -5 -6 -7 -8) (+ 3 tot))
  167. (else (intlen (logical:ash-4 n) (+ 4 tot)))))))
  168. (lambda (n) (intlen n 0))))
  169. ;@
  170. (define logcount
  171. (letrec ((logcnt (lambda (n tot)
  172. (if (zero? n)
  173. tot
  174. (logcnt (quotient n 16)
  175. (+ (vector-ref
  176. '#(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4)
  177. (modulo n 16))
  178. tot))))))
  179. (lambda (n)
  180. (cond ((negative? n) (logcnt (lognot n) 0))
  181. ((positive? n) (logcnt n 0))
  182. (else 0)))))
  183. ;@
  184. (define (log2-binary-factors n)
  185. (+ -1 (integer-length (logand n (- n)))))
  186. (define (bit-reverse k n)
  187. (do ((m (if (negative? n) (lognot n) n) (arithmetic-shift m -1))
  188. (k (+ -1 k) (+ -1 k))
  189. (rvs 0 (logior (arithmetic-shift rvs 1) (logand 1 m))))
  190. ((negative? k) (if (negative? n) (lognot rvs) rvs))))
  191. ;@
  192. (define (reverse-bit-field n start end)
  193. (define width (- end start))
  194. (let ((mask (lognot (ash -1 width))))
  195. (define zn (logand mask (arithmetic-shift n (- start))))
  196. (logior (arithmetic-shift (bit-reverse width zn) start)
  197. (logand (lognot (ash mask start)) n))))
  198. ;@
  199. (define (integer->list k . len)
  200. (if (null? len)
  201. (do ((k k (arithmetic-shift k -1))
  202. (lst '() (cons (odd? k) lst)))
  203. ((<= k 0) lst))
  204. (do ((idx (+ -1 (car len)) (+ -1 idx))
  205. (k k (arithmetic-shift k -1))
  206. (lst '() (cons (odd? k) lst)))
  207. ((negative? idx) lst))))
  208. ;@
  209. (define (list->integer bools)
  210. (do ((bs bools (cdr bs))
  211. (acc 0 (+ acc acc (if (car bs) 1 0))))
  212. ((null? bs) acc)))
  213. (define (booleans->integer . bools)
  214. (list->integer bools))
  215. ;;;;@ SRFI-60 aliases
  216. (define ash arithmetic-shift)
  217. (define bitwise-ior logior)
  218. (define bitwise-xor logxor)
  219. (define bitwise-and logand)
  220. (define bitwise-not lognot)
  221. (define bit-count logcount)
  222. (define bit-set? logbit?)
  223. (define any-bits-set? logtest)
  224. (define first-set-bit log2-binary-factors)
  225. (define bitwise-merge bitwise-if)
  226. ;;; Legacy
  227. ;;(define (logical:rotate k count len) (rotate-bit-field k count 0 len))
  228. ;;(define (logical:ones deg) (lognot (ash -1 deg)))
  229. ;;(define integer-expt expt) ; legacy name