gnu.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; Extensions to SRFI-4
  2. ;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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. ;;; Commentary:
  18. ;; Extensions to SRFI-4. Fully documented in the Guile Reference Manual.
  19. ;;; Code:
  20. (define-module (srfi srfi-4 gnu)
  21. #:use-module (rnrs bytevectors)
  22. #:use-module (srfi srfi-4)
  23. #:export (;; Complex numbers with 32- and 64-bit components.
  24. c32vector? make-c32vector c32vector c32vector-length c32vector-ref
  25. c32vector-set! c32vector->list list->c32vector
  26. c64vector? make-c64vector c64vector c64vector-length c64vector-ref
  27. c64vector-set! c64vector->list list->c64vector
  28. make-srfi-4-vector
  29. ;; Somewhat polymorphic conversions.
  30. any->u8vector any->s8vector any->u16vector any->s16vector
  31. any->u32vector any->s32vector any->u64vector any->s64vector
  32. any->f32vector any->f64vector any->c32vector any->c64vector))
  33. (define make-srfi-4-vector (@@ (srfi srfi-4) make-srfi-4-vector))
  34. (define (bytevector-c32-native-ref v i)
  35. (make-rectangular (bytevector-ieee-single-native-ref v i)
  36. (bytevector-ieee-single-native-ref v (+ i 4))))
  37. (define (bytevector-c32-native-set! v i x)
  38. (bytevector-ieee-single-native-set! v i (real-part x))
  39. (bytevector-ieee-single-native-set! v (+ i 4) (imag-part x)))
  40. (define (bytevector-c64-native-ref v i)
  41. (make-rectangular (bytevector-ieee-double-native-ref v i)
  42. (bytevector-ieee-double-native-ref v (+ i 8))))
  43. (define (bytevector-c64-native-set! v i x)
  44. (bytevector-ieee-double-native-set! v i (real-part x))
  45. (bytevector-ieee-double-native-set! v (+ i 8) (imag-part x)))
  46. ((@@ (srfi srfi-4) define-bytevector-type) c32 c32-native 8)
  47. ((@@ (srfi srfi-4) define-bytevector-type) c64 c64-native 16)
  48. (define-macro (define-any->vector . tags)
  49. `(begin
  50. ,@(map (lambda (tag)
  51. `(define (,(symbol-append 'any-> tag 'vector) obj)
  52. (cond ((,(symbol-append tag 'vector?) obj) obj)
  53. ((pair? obj) (,(symbol-append 'list-> tag 'vector) obj))
  54. ((and (array? obj) (eqv? 1 (array-rank obj)))
  55. (let* ((len (array-length obj))
  56. (v (,(symbol-append 'make- tag 'vector) len)))
  57. (let lp ((i 0))
  58. (if (< i len)
  59. (begin
  60. (,(symbol-append tag 'vector-set!)
  61. v i (array-ref obj i))
  62. (lp (1+ i)))
  63. v))))
  64. (else (scm-error 'wrong-type-arg #f "" '() (list obj))))))
  65. tags)))
  66. (define-any->vector u8 s8 u16 s16 u32 s32 u64 s64 f32 f64 c32 c64)