srfi-60.scm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;; srfi-60.scm --- Integers as Bits
  2. ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (srfi srfi-60)
  18. #:export (bitwise-and
  19. bitwise-ior
  20. bitwise-xor
  21. bitwise-not
  22. any-bits-set?
  23. bit-count
  24. bitwise-if bitwise-merge
  25. log2-binary-factors first-set-bit
  26. bit-set?
  27. copy-bit
  28. bit-field
  29. copy-bit-field
  30. arithmetic-shift
  31. rotate-bit-field
  32. reverse-bit-field
  33. integer->list
  34. list->integer
  35. booleans->integer)
  36. #:re-export (logand
  37. logior
  38. logxor
  39. integer-length
  40. logtest
  41. logcount
  42. logbit?
  43. ash))
  44. (load-extension "libguile-srfi-srfi-60-v-2" "scm_init_srfi_60")
  45. (define bitwise-and logand)
  46. (define bitwise-ior logior)
  47. (define bitwise-xor logxor)
  48. (define bitwise-not lognot)
  49. (define any-bits-set? logtest)
  50. (define bit-count logcount)
  51. (define (bitwise-if mask n0 n1)
  52. (logior (logand mask n0)
  53. (logand (lognot mask) n1)))
  54. (define bitwise-merge bitwise-if)
  55. (define first-set-bit log2-binary-factors)
  56. (define bit-set? logbit?)
  57. (define bit-field bit-extract)
  58. (define (copy-bit-field n newbits start end)
  59. (logxor n (ash (logxor (bit-extract n start end) ;; cancel old
  60. (bit-extract newbits 0 (- end start))) ;; insert new
  61. start)))
  62. (define arithmetic-shift ash)
  63. (cond-expand-provide (current-module) '(srfi-60))