bitwise.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ;;; bitwise.scm --- The R6RS bitwise arithmetic operations library
  2. ;; Copyright (C) 2010, 2013 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. (library (rnrs arithmetic bitwise (6))
  18. (export bitwise-not
  19. bitwise-and
  20. bitwise-ior
  21. bitwise-xor
  22. bitwise-if
  23. bitwise-bit-count
  24. bitwise-length
  25. bitwise-first-bit-set
  26. bitwise-bit-set?
  27. bitwise-copy-bit
  28. bitwise-bit-field
  29. bitwise-copy-bit-field
  30. bitwise-arithmetic-shift
  31. bitwise-arithmetic-shift-left
  32. bitwise-arithmetic-shift-right
  33. bitwise-rotate-bit-field
  34. bitwise-reverse-bit-field)
  35. (import (rnrs base (6))
  36. (rnrs control (6))
  37. (rename (only (srfi srfi-60) bitwise-if
  38. integer-length
  39. first-set-bit
  40. copy-bit
  41. bit-field
  42. copy-bit-field
  43. rotate-bit-field
  44. reverse-bit-field)
  45. (integer-length bitwise-length)
  46. (first-set-bit bitwise-first-bit-set)
  47. (bit-field bitwise-bit-field)
  48. (reverse-bit-field bitwise-reverse-bit-field))
  49. (rename (only (guile) lognot
  50. logand
  51. logior
  52. logxor
  53. logcount
  54. logbit?
  55. modulo
  56. ash)
  57. (lognot bitwise-not)
  58. (logand bitwise-and)
  59. (logior bitwise-ior)
  60. (logxor bitwise-xor)
  61. (ash bitwise-arithmetic-shift)))
  62. (define (bitwise-bit-count ei)
  63. (if (negative? ei)
  64. (bitwise-not (logcount ei))
  65. (logcount ei)))
  66. (define (bitwise-bit-set? ei1 ei2) (logbit? ei2 ei1))
  67. (define (bitwise-copy-bit ei1 ei2 ei3)
  68. ;; The specification states that ei3 should be either 0 or 1.
  69. ;; However, other values have been tolerated by both Guile 2.0.x and
  70. ;; the sample implementation given the R6RS library document, so for
  71. ;; backward compatibility we continue to permit it.
  72. (copy-bit ei2 ei1 (logbit? 0 ei3)))
  73. (define (bitwise-copy-bit-field ei1 ei2 ei3 ei4)
  74. (copy-bit-field ei1 ei4 ei2 ei3))
  75. (define (bitwise-rotate-bit-field ei1 ei2 ei3 ei4)
  76. (rotate-bit-field ei1 ei4 ei2 ei3))
  77. (define bitwise-arithmetic-shift-left bitwise-arithmetic-shift)
  78. (define (bitwise-arithmetic-shift-right ei1 ei2)
  79. (bitwise-arithmetic-shift ei1 (- ei2))))