srfi-60.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;; srfi-60.scm --- Integers as Bits
  2. ;; Copyright (C) 2005, 2006, 2010 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 3 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. bitwise-if bitwise-merge
  24. log2-binary-factors first-set-bit
  25. bit-set?
  26. copy-bit
  27. bit-field
  28. copy-bit-field
  29. arithmetic-shift
  30. rotate-bit-field
  31. reverse-bit-field
  32. integer->list
  33. list->integer
  34. booleans->integer)
  35. #:replace (bit-count)
  36. #:re-export (logand
  37. logior
  38. logxor
  39. integer-length
  40. logtest
  41. logcount
  42. logbit?
  43. ash))
  44. (load-extension (string-append "libguile-" (effective-version))
  45. "scm_init_srfi_60")
  46. (define bitwise-and logand)
  47. (define bitwise-ior logior)
  48. (define bitwise-xor logxor)
  49. (define bitwise-not lognot)
  50. (define any-bits-set? logtest)
  51. (define bit-count logcount)
  52. (define (bitwise-if mask n0 n1)
  53. (logior (logand mask n0)
  54. (logand (lognot mask) n1)))
  55. (define bitwise-merge bitwise-if)
  56. (define first-set-bit log2-binary-factors)
  57. (define bit-set? logbit?)
  58. (define bit-field bit-extract)
  59. (define (copy-bit-field n newbits start end)
  60. (logxor n (ash (logxor (bit-extract n start end) ;; cancel old
  61. (bit-extract newbits 0 (- end start))) ;; insert new
  62. start)))
  63. (define arithmetic-shift ash)
  64. (cond-expand-provide (current-module) '(srfi-60))