byte-vector.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey
  3. (define (byte-vector-endianess)
  4. (if (eq? byte-vector-word-ref high-byte-vector-word-ref)
  5. 'high
  6. 'low))
  7. (define (set-byte-vector-endianess! high-or-low)
  8. (case high-or-low
  9. ((high)
  10. (set! byte-vector-word-ref high-byte-vector-word-ref)
  11. (set! byte-vector-half-word-ref high-byte-vector-half-word-ref)
  12. (set! byte-vector-word-set! high-byte-vector-word-set!)
  13. (set! byte-vector-half-word-set! high-byte-vector-half-word-set!))
  14. ((low)
  15. (set! byte-vector-word-ref low-byte-vector-word-ref)
  16. (set! byte-vector-half-word-ref low-byte-vector-half-word-ref)
  17. (set! byte-vector-word-set! low-byte-vector-word-set!)
  18. (set! byte-vector-half-word-set! low-byte-vector-half-word-set!))
  19. (else
  20. (error "endianess specifier is neither HIGH nor LOW" high-or-low))))
  21. (define (high-byte-vector-word-ref vector index)
  22. (+ (byte-vector-ref vector (+ index 3))
  23. (arithmetic-shift
  24. (+ (byte-vector-ref vector (+ index 2))
  25. (arithmetic-shift
  26. (+ (byte-vector-ref vector (+ index 1))
  27. (arithmetic-shift
  28. (byte-vector-ref vector index)
  29. 8))
  30. 8))
  31. 8)))
  32. (define (high-byte-vector-word-set! vector index value)
  33. (byte-vector-set! vector index (arithmetic-shift value -24))
  34. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -16))
  35. (byte-vector-set! vector (+ index 2) (arithmetic-shift value -8))
  36. (byte-vector-set! vector (+ index 3) value))
  37. (define (high-byte-vector-half-word-ref vector index)
  38. (+ (byte-vector-ref vector (+ index 1))
  39. (arithmetic-shift
  40. (byte-vector-ref vector index)
  41. 8)))
  42. (define (high-byte-vector-half-word-set! vector index value)
  43. (byte-vector-set! vector index (arithmetic-shift value -8))
  44. (byte-vector-set! vector (+ index 1) value))
  45. (define (low-byte-vector-word-ref vector index)
  46. (+ (byte-vector-ref vector index)
  47. (arithmetic-shift
  48. (+ (byte-vector-ref vector (+ index 1))
  49. (arithmetic-shift
  50. (+ (byte-vector-ref vector (+ index 2))
  51. (arithmetic-shift
  52. (byte-vector-ref vector (+ index 3))
  53. 8))
  54. 8))
  55. 8)))
  56. (define (low-byte-vector-word-set! vector index value)
  57. (byte-vector-set! vector index value)
  58. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -8))
  59. (byte-vector-set! vector (+ index 2) (arithmetic-shift value -16))
  60. (byte-vector-set! vector (+ index 3) (arithmetic-shift value -24)))
  61. (define (low-byte-vector-half-word-ref vector index)
  62. (+ (byte-vector-ref vector index)
  63. (arithmetic-shift
  64. (byte-vector-ref vector (+ index 1))
  65. 8)))
  66. (define (low-byte-vector-half-word-set! vector index value)
  67. (byte-vector-set! vector index value)
  68. (byte-vector-set! vector (+ index 1) (arithmetic-shift value -8)))
  69. ; Start high-endian
  70. (define byte-vector-word-ref high-byte-vector-word-ref)
  71. (define byte-vector-half-word-ref high-byte-vector-half-word-ref)
  72. (define byte-vector-word-set! high-byte-vector-word-set!)
  73. (define byte-vector-half-word-set! high-byte-vector-half-word-set!)