byte-vector.scm 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/util/byte-vector.scm
  8. (define-module (ps-compiler util byte-vector)
  9. #:use-module (prescheme scheme48)
  10. #:export (make-byte-vector byte-vector? byte-vector-length
  11. byte-vector-ref byte-vector-word-ref byte-vector-half-word-ref
  12. byte-vector-set! byte-vector-word-set! byte-vector-half-word-set!
  13. byte-vector-endianess set-byte-vector-endianess!))
  14. (define (byte-vector-endianess)
  15. (if (eq? byte-vector-word-ref high-byte-vector-word-ref)
  16. 'high
  17. 'low))
  18. (define (set-byte-vector-endianess! high-or-low)
  19. (case high-or-low
  20. ((high)
  21. (set! byte-vector-word-ref high-byte-vector-word-ref)
  22. (set! byte-vector-half-word-ref high-byte-vector-half-word-ref)
  23. (set! byte-vector-word-set! high-byte-vector-word-set!)
  24. (set! byte-vector-half-word-set! high-byte-vector-half-word-set!))
  25. ((low)
  26. (set! byte-vector-word-ref low-byte-vector-word-ref)
  27. (set! byte-vector-half-word-ref low-byte-vector-half-word-ref)
  28. (set! byte-vector-word-set! low-byte-vector-word-set!)
  29. (set! byte-vector-half-word-set! low-byte-vector-half-word-set!))
  30. (else
  31. (error "endianess specifier is neither HIGH nor LOW" high-or-low))))
  32. (define (high-byte-vector-word-ref vector index)
  33. (+ (byte-vector-ref vector (+ index 3))
  34. (arithmetic-shift
  35. (+ (byte-vector-ref vector (+ index 2))
  36. (arithmetic-shift
  37. (+ (byte-vector-ref vector (+ index 1))
  38. (arithmetic-shift
  39. (byte-vector-ref vector index)
  40. 8))
  41. 8))
  42. 8)))
  43. (define (high-byte-vector-word-set! vector index value)
  44. (byte-vector-set! vector index (arithmetic-shift value -24))
  45. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -16))
  46. (byte-vector-set! vector (+ index 2) (arithmetic-shift value -8))
  47. (byte-vector-set! vector (+ index 3) value))
  48. (define (high-byte-vector-half-word-ref vector index)
  49. (+ (byte-vector-ref vector (+ index 1))
  50. (arithmetic-shift
  51. (byte-vector-ref vector index)
  52. 8)))
  53. (define (high-byte-vector-half-word-set! vector index value)
  54. (byte-vector-set! vector index (arithmetic-shift value -8))
  55. (byte-vector-set! vector (+ index 1) value))
  56. (define (low-byte-vector-word-ref vector index)
  57. (+ (byte-vector-ref vector index)
  58. (arithmetic-shift
  59. (+ (byte-vector-ref vector (+ index 1))
  60. (arithmetic-shift
  61. (+ (byte-vector-ref vector (+ index 2))
  62. (arithmetic-shift
  63. (byte-vector-ref vector (+ index 3))
  64. 8))
  65. 8))
  66. 8)))
  67. (define (low-byte-vector-word-set! vector index value)
  68. (byte-vector-set! vector index value)
  69. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -8))
  70. (byte-vector-set! vector (+ index 2) (arithmetic-shift value -16))
  71. (byte-vector-set! vector (+ index 3) (arithmetic-shift value -24)))
  72. (define (low-byte-vector-half-word-ref vector index)
  73. (+ (byte-vector-ref vector index)
  74. (arithmetic-shift
  75. (byte-vector-ref vector (+ index 1))
  76. 8)))
  77. (define (low-byte-vector-half-word-set! vector index value)
  78. (byte-vector-set! vector index value)
  79. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -8)))
  80. ;; Start high-endian
  81. (define byte-vector-word-ref high-byte-vector-word-ref)
  82. (define byte-vector-half-word-ref high-byte-vector-half-word-ref)
  83. (define byte-vector-word-set! high-byte-vector-word-set!)
  84. (define byte-vector-half-word-set! high-byte-vector-half-word-set!)