bytevectors.scm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (define-module (mescc bytevectors)
  21. #:use-module (mes guile)
  22. #:export (bytevector-u64-native-set!
  23. bytevector-u32-native-set!
  24. bytevector-u16-native-set!
  25. bytevector-u8-set!
  26. make-bytevector))
  27. ;; rnrs compatibility
  28. (define (bytevector-u64-native-set! bv index value)
  29. (when (not (= 0 index)) (error "bytevector-u64-native-set! index not zero: " index " value: " value))
  30. (let ((x (list
  31. (modulo value #x100)
  32. (modulo (ash value -8) #x100)
  33. (modulo (ash value -16) #x100)
  34. (modulo (ash value -24) #x100)
  35. (modulo (ash value -32) #x100)
  36. (modulo (ash value -40) #x100)
  37. (modulo (ash value -48) #x100)
  38. (modulo (ash value -56) #x100))))
  39. (set-car! bv (car x))
  40. (set-cdr! bv (cdr x))
  41. x))
  42. (define (bytevector-u32-native-set! bv index value)
  43. (when (not (= 0 index)) (error "bytevector-u32-native-set! index not zero: " index " value: " value))
  44. (let ((x (list
  45. (modulo value #x100)
  46. (modulo (ash value -8) #x100)
  47. (modulo (ash value -16) #x100)
  48. (modulo (ash value -24) #x100))))
  49. (set-car! bv (car x))
  50. (set-cdr! bv (cdr x))
  51. x))
  52. (define (bytevector-u16-native-set! bv index value)
  53. (when (not (= 0 index)) (error "bytevector-u16-native-set! index not zero: " index " value: " value))
  54. (let ((x (list
  55. (modulo value #x100)
  56. (modulo (ash value -8) #x100))))
  57. (set-car! bv (car x))
  58. (set-cdr! bv (cdr x))
  59. x))
  60. (define (bytevector-u8-set! bv index value)
  61. (when (not (= 0 index)) (error "bytevector-u8-set! index not zero: " index " value: " value))
  62. (let ((x (modulo value #x100)))
  63. (set-car! bv x)
  64. x))
  65. (define (make-bytevector length)
  66. (make-list length 0))