srfi-4.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; srfi-4.scm --- Homogeneous Numeric Vector Datatypes
  2. ;; Copyright (C) 2001, 2002, 2004, 2006, 2009, 2010,
  3. ;; 2012, 2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; This library is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU Lesser General Public
  7. ;; License as published by the Free Software Foundation; either
  8. ;; version 3 of the License, or (at your option) any later version.
  9. ;;
  10. ;; This library is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; Lesser General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU Lesser General Public
  16. ;; License along with this library; if not, write to the Free Software
  17. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
  19. ;;; Commentary:
  20. ;; This module exports the homogeneous numeric vector procedures as
  21. ;; defined in SRFI-4. They are fully documented in the Guile
  22. ;; Reference Manual.
  23. ;;; Code:
  24. (define-module (srfi srfi-4)
  25. #:use-module (rnrs bytevectors)
  26. #:export (;; Unsigned 8-bit vectors.
  27. u8vector? make-u8vector u8vector u8vector-length u8vector-ref
  28. u8vector-set! u8vector->list list->u8vector
  29. ;; Signed 8-bit vectors.
  30. s8vector? make-s8vector s8vector s8vector-length s8vector-ref
  31. s8vector-set! s8vector->list list->s8vector
  32. ;; Unsigned 16-bit vectors.
  33. u16vector? make-u16vector u16vector u16vector-length u16vector-ref
  34. u16vector-set! u16vector->list list->u16vector
  35. ;; Signed 16-bit vectors.
  36. s16vector? make-s16vector s16vector s16vector-length s16vector-ref
  37. s16vector-set! s16vector->list list->s16vector
  38. ;; Unsigned 32-bit vectors.
  39. u32vector? make-u32vector u32vector u32vector-length u32vector-ref
  40. u32vector-set! u32vector->list list->u32vector
  41. ;; Signed 32-bit vectors.
  42. s32vector? make-s32vector s32vector s32vector-length s32vector-ref
  43. s32vector-set! s32vector->list list->s32vector
  44. ;; Unsigned 64-bit vectors.
  45. u64vector? make-u64vector u64vector u64vector-length u64vector-ref
  46. u64vector-set! u64vector->list list->u64vector
  47. ;; Signed 64-bit vectors.
  48. s64vector? make-s64vector s64vector s64vector-length s64vector-ref
  49. s64vector-set! s64vector->list list->s64vector
  50. ;; 32-bit floating point vectors.
  51. f32vector? make-f32vector f32vector f32vector-length f32vector-ref
  52. f32vector-set! f32vector->list list->f32vector
  53. ;; 64-bit floating point vectors.
  54. f64vector? make-f64vector f64vector f64vector-length f64vector-ref
  55. f64vector-set! f64vector->list list->f64vector))
  56. (cond-expand-provide (current-module) '(srfi-4))
  57. ;; Need quasisyntax to do this effectively using syntax-case
  58. (define-macro (define-bytevector-type tag infix size)
  59. `(begin
  60. (define (,(symbol-append tag 'vector?) obj)
  61. (and (bytevector? obj) (eq? (array-type obj) ',tag)))
  62. (define (,(symbol-append 'make- tag 'vector) len . fill)
  63. (apply make-srfi-4-vector ',tag len fill))
  64. (define (,(symbol-append tag 'vector-length) v)
  65. (let ((len (/ (bytevector-length v) ,size)))
  66. (if (integer? len)
  67. len
  68. (error "fractional length" v ',tag ,size))))
  69. (define (,(symbol-append tag 'vector) . elts)
  70. (,(symbol-append 'list-> tag 'vector) elts))
  71. (define (,(symbol-append 'list-> tag 'vector) elts)
  72. (let* ((len (length elts))
  73. (v (,(symbol-append 'make- tag 'vector) len)))
  74. (let lp ((i 0) (elts elts))
  75. (if (and (< i len) (pair? elts))
  76. (begin
  77. (,(symbol-append tag 'vector-set!) v i (car elts))
  78. (lp (1+ i) (cdr elts)))
  79. v))))
  80. (define (,(symbol-append tag 'vector->list) v)
  81. (let lp ((i (1- (,(symbol-append tag 'vector-length) v))) (elts '()))
  82. (if (< i 0)
  83. elts
  84. (lp (1- i) (cons (,(symbol-append tag 'vector-ref) v i) elts)))))
  85. (define (,(symbol-append tag 'vector-ref) v i)
  86. (,(symbol-append 'bytevector- infix '-ref) v (* i ,size)))
  87. (define (,(symbol-append tag 'vector-set!) v i x)
  88. (,(symbol-append 'bytevector- infix '-set!) v (* i ,size) x))
  89. (define (,(symbol-append tag 'vector-set!) v i x)
  90. (,(symbol-append 'bytevector- infix '-set!) v (* i ,size) x))))
  91. (define-bytevector-type u8 u8 1)
  92. (define-bytevector-type s8 s8 1)
  93. (define-bytevector-type u16 u16-native 2)
  94. (define-bytevector-type s16 s16-native 2)
  95. (define-bytevector-type u32 u32-native 4)
  96. (define-bytevector-type s32 s32-native 4)
  97. (define-bytevector-type u64 u64-native 8)
  98. (define-bytevector-type s64 s64-native 8)
  99. (define-bytevector-type f32 ieee-single-native 4)
  100. (define-bytevector-type f64 ieee-double-native 8)