Debian Codegraph @

ext/uvector/uvector.scm

raw
1 ;;;
2 ;;; gauche.uvector - uniform vectors
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This module defines the superset of SRFI-4, homogeneous numeric vector
35 ;; types. Most of basic operations are defined in the DSO module libuvector.
36 ;; Besides defining functions, the DSO sets up a reader hook to enable
37 ;; extended syntax such as #s8(1 2 3).
38 ;; This module also defines methods for collection and sequence frameworks.
39
40 ;; NB: The following identifiers are defined in core now:
41 ;; uvector? ${TAG}vector-ref ${TAG}vector-set! uvector-ref uvector-set!
42 ;; uvector-length uvector-immutable?
43
44 (define-module gauche.uvector
45 (use gauche.collection)
46 (use gauche.sequence)
47
48 (include "./exports")
49 (export make-uvector
50
51 open-output-uvector get-output-uvector port->uvector
52 read-block! read-uvector read-uvector!
53 read-bytevector read-bytevector!
54 referencer
55
56 ;; string <-> uvector operations are now in libvec.scm
57 ;; we reexport them for the compatibility.
58 string->s8vector string->s8vector!
59 string->u8vector string->u8vector!
60 string->s32vector string->s32vector!
61 string->u32vector string->u32vector!
62 s8vector->string u8vector->string
63 s32vector->string u32vector->string
64
65 uvector-alias uvector-segment/shared
66 uvector-binary-search uvector-class-element-size
67 uvector-copy uvector-copy! uvector-ref uvector-set! uvector-size
68 uvector->list uvector->vector uvector-swap-bytes uvector-swap-bytes!
69
70 write-block write-uvector write-bytevector
71
72 ;; R7RS compatibility (scheme base) and (scheme bytevector)
73 bytevector bytevector? make-bytevector bytevector-fill!
74 bytevector-length bytevector-u8-ref bytevector-u8-set!
75 bytevector-s8-ref bytevector-s8-set!
76 bytevector->u8-list u8-list->bytevector
77 bytevector-copy bytevector-copy! bytevector-copy!-r6
78 bytevector-append bytevector=?
79 ))
80 (select-module gauche.uvector)
81
82 ;; gauche.vport is used by port->uvector. Technically it's on top
83 ;; of uniform vector ports provided by gauche.vport, but logically
84 ;; it is expected to belong gauche.uvector.
85 (autoload gauche.vport open-output-uvector get-output-uvector)
86
87 ;; referenced from SRFI-160 make-TAGvector-generator
88 (autoload gauche.generator uvector->generator)
89
90 (inline-stub
91 (declcode
92 (.include <math.h>)
93 (.define EXTUVECTOR_EXPORTS)
94 (.include "gauche/uvector.h")
95 (.include "gauche/priv/vectorP.h")
96 (.include "gauche/priv/bytesP.h")
97 (.include "uvectorP.h")))
98
99 ;; uvlib.scm is generated by uvlib.scm.tmpl
100 (include "./uvlib.scm")
101
102 ;;;
103 ;;; Generic procedures
104 ;;;
105
106 ;; uvector-alias
107 (inline-stub
108 (define-cproc uvector-alias
109 (klass::<class> v::<uvector> :optional (start::<fixnum> 0) (end::<fixnum> -1))
110 Scm_UVectorAlias)
111
112 (define-cproc uvector-segment/shared (v::<uvector> n::<fixnum>)
113 (unless (> n 0)
114 (Scm_Error "Positive exact integer required, but got: %d" n))
115 (let* ([len::ScmSmallInt (SCM_UVECTOR_SIZE v)]
116 [i::ScmSmallInt 0]
117 [h SCM_NIL] [t SCM_NIL])
118 (while (< i len)
119 (let* ([vv (Scm_UVectorAlias (Scm_ClassOf (SCM_OBJ v))
120 v i (?: (> (+ i n) len) len (+ i n)))])
121 (SCM_APPEND1 h t vv))
122 (set! i (+ i n)))
123 (return h)))
124 )
125
126 ;; byte swapping
127 (inline-stub
128 (define-cise-stmt swap-bytes-common
129 [(_ c-fn v type)
130 `(let* ([opt::int SWAPB_STD])
131 (cond [(== ,type NULL)]
132 [(SCM_EQ (SCM_OBJ ,type) 'le:arm-le) (= opt SWAPB_ARM_LE)]
133 [(SCM_EQ (SCM_OBJ ,type) 'be:arm-le) (= opt SWAPB_ARM_BE)]
134 [else (Scm_TypeError "type" "#f or a symbol le:arm-le or be:arm-le"
135 (SCM_OBJ ,type))])
136 (,c-fn ,v opt))])
137
138 (define-cproc uvector-swap-bytes (v::<uvector> :optional (type::<symbol>? #f)) ::<void>
139 (swap-bytes-common Scm_UVectorSwapBytes v type))
140
141 (define-cproc uvector-swap-bytes! (v::<uvector> :optional (type::<symbol>? #f)) ::<void>
142 (swap-bytes-common Scm_UVectorSwapBytesX v type))
143 )
144
145 ;; uvector-size
146 (inline-stub
147 (define-cproc uvector-size (v::<uvector>
148 :optional (start::<fixnum> 0) (end::<fixnum> -1))
149 ::<fixnum>
150 (let* ([len::ScmSmallInt (SCM_UVECTOR_SIZE v)])
151 (SCM_CHECK_START_END start end len)
152 (return (* (- end start)
153 (Scm_UVectorElementSize (Scm_ClassOf (SCM_OBJ v)))))))
154
155 (define-cproc uvector-class-element-size (c::<class>) ::<fixnum>
156 (let* ([r::int (Scm_UVectorElementSize c)])
157 (when (< r 0)
158 (Scm_Error "A class of uvector is required, but got: %S" c))
159 (return r)))
160 )
161
162 ;; uvector->list
163 ;; uvector->vector
164 (inline-stub
165 (define-cproc uvector->list (v::<uvector>
166 :optional (start::<fixnum> 0)
167 (end::<fixnum> -1))
168 (case (Scm_UVectorType (Scm_ClassOf (SCM_OBJ v)))
169 [(SCM_UVECTOR_S8) (return (Scm_S8VectorToList (SCM_S8VECTOR v)
170 start end))]
171 [(SCM_UVECTOR_U8) (return (Scm_U8VectorToList (SCM_U8VECTOR v)
172 start end))]
173 [(SCM_UVECTOR_S16) (return (Scm_S16VectorToList (SCM_S16VECTOR v)
174 start end))]
175 [(SCM_UVECTOR_U16) (return (Scm_U16VectorToList (SCM_U16VECTOR v)
176 start end))]
177 [(SCM_UVECTOR_S32) (return (Scm_S32VectorToList (SCM_S32VECTOR v)
178 start end))]
179 [(SCM_UVECTOR_U32) (return (Scm_U32VectorToList (SCM_U32VECTOR v)
180 start end))]
181 [(SCM_UVECTOR_S64) (return (Scm_S64VectorToList (SCM_S64VECTOR v)
182 start end))]
183 [(SCM_UVECTOR_U64) (return (Scm_U64VectorToList (SCM_U64VECTOR v)
184 start end))]
185 [(SCM_UVECTOR_F16) (return (Scm_F16VectorToList (SCM_F16VECTOR v)
186 start end))]
187 [(SCM_UVECTOR_F32) (return (Scm_F32VectorToList (SCM_F32VECTOR v)
188 start end))]
189 [(SCM_UVECTOR_F64) (return (Scm_F64VectorToList (SCM_F64VECTOR v)
190 start end))]
191 [(SCM_UVECTOR_C32) (return (Scm_C32VectorToList (SCM_C32VECTOR v)
192 start end))]
193 [(SCM_UVECTOR_C64) (return (Scm_C64VectorToList (SCM_C64VECTOR v)
194 start end))]
195 [(SCM_UVECTOR_C128) (return (Scm_C128VectorToList (SCM_C128VECTOR v)
196 start end))]
197 [else (Scm_Error "[internal] Invalid uvector type: %S" v)
198 (return SCM_UNDEFINED)]))
199 (define-cproc uvector->vector (v::<uvector>
200 :optional (start::<fixnum> 0)
201 (end::<fixnum> -1))
202 (case (Scm_UVectorType (Scm_ClassOf (SCM_OBJ v)))
203 [(SCM_UVECTOR_S8) (return (Scm_S8VectorToVector (SCM_S8VECTOR v)
204 start end))]
205 [(SCM_UVECTOR_U8) (return (Scm_U8VectorToVector (SCM_U8VECTOR v)
206 start end))]
207 [(SCM_UVECTOR_S16) (return (Scm_S16VectorToVector (SCM_S16VECTOR v)
208 start end))]
209 [(SCM_UVECTOR_U16) (return (Scm_U16VectorToVector (SCM_U16VECTOR v)
210 start end))]
211 [(SCM_UVECTOR_S32) (return (Scm_S32VectorToVector (SCM_S32VECTOR v)
212 start end))]
213 [(SCM_UVECTOR_U32) (return (Scm_U32VectorToVector (SCM_U32VECTOR v)
214 start end))]
215 [(SCM_UVECTOR_S64) (return (Scm_S64VectorToVector (SCM_S64VECTOR v)
216 start end))]
217 [(SCM_UVECTOR_U64) (return (Scm_U64VectorToVector (SCM_U64VECTOR v)
218 start end))]
219 [(SCM_UVECTOR_F16) (return (Scm_F16VectorToVector (SCM_F16VECTOR v)
220 start end))]
221 [(SCM_UVECTOR_F32) (return (Scm_F32VectorToVector (SCM_F32VECTOR v)
222 start end))]
223 [(SCM_UVECTOR_F64) (return (Scm_F64VectorToVector (SCM_F64VECTOR v)
224 start end))]
225 [(SCM_UVECTOR_C32) (return (Scm_C32VectorToVector (SCM_C32VECTOR v)
226 start end))]
227 [(SCM_UVECTOR_C64) (return (Scm_C64VectorToVector (SCM_C64VECTOR v)
228 start end))]
229 [(SCM_UVECTOR_C128) (return (Scm_C128VectorToVector (SCM_C128VECTOR v)
230 start end))]
231 [else (Scm_Error "[internal] Invalid uvector type: %S" v)
232 (return SCM_UNDEFINED)])))
233
234 ;; allocation by class
235 (inline-stub
236 (define-cproc make-uvector (klass::<class> size::<fixnum>
237 :optional (init 0))
238 (unless (>= size 0) (Scm_Error "invalid uvector size: %d" size))
239 (let* ([v (Scm_MakeUVector klass size NULL)])
240 (case (Scm_UVectorType klass)
241 [(SCM_UVECTOR_S8)
242 (Scm_S8VectorFill (SCM_S8VECTOR v)
243 (Scm_GetInteger8Clamp init SCM_CLAMP_ERROR NULL)
244 0 -1)]
245 [(SCM_UVECTOR_U8)
246 (Scm_U8VectorFill (SCM_U8VECTOR v)
247 (Scm_GetIntegerU8Clamp init SCM_CLAMP_ERROR NULL)
248 0 -1)]
249 [(SCM_UVECTOR_S16)
250 (Scm_S16VectorFill (SCM_S16VECTOR v)
251 (Scm_GetInteger16Clamp init SCM_CLAMP_ERROR NULL)
252 0 -1)]
253 [(SCM_UVECTOR_U16)
254 (Scm_U16VectorFill (SCM_U16VECTOR v)
255 (Scm_GetIntegerU16Clamp init SCM_CLAMP_ERROR NULL)
256 0 -1)]
257 [(SCM_UVECTOR_S32)
258 (Scm_S32VectorFill (SCM_S32VECTOR v)
259 (Scm_GetInteger32Clamp init SCM_CLAMP_ERROR NULL)
260 0 -1)]
261 [(SCM_UVECTOR_U32)
262 (Scm_U32VectorFill (SCM_U32VECTOR v)
263 (Scm_GetIntegerU32Clamp init SCM_CLAMP_ERROR NULL)
264 0 -1)]
265 [(SCM_UVECTOR_S64)
266 (Scm_S64VectorFill (SCM_S64VECTOR v)
267 (Scm_GetInteger64Clamp init SCM_CLAMP_ERROR NULL)
268 0 -1)]
269 [(SCM_UVECTOR_U64)
270 (Scm_U64VectorFill (SCM_U64VECTOR v)
271 (Scm_GetIntegerU64Clamp init SCM_CLAMP_ERROR NULL)
272 0 -1)]
273 [(SCM_UVECTOR_F16)
274 (Scm_F16VectorFill (SCM_F16VECTOR v)
275 (Scm_DoubleToHalf (Scm_GetDouble init))
276 0 -1)]
277 [(SCM_UVECTOR_F32)
278 (Scm_F32VectorFill (SCM_F32VECTOR v)
279 (cast float (Scm_GetDouble init))
280 0 -1)]
281 [(SCM_UVECTOR_F64)
282 (Scm_F64VectorFill (SCM_F64VECTOR v)
283 (Scm_GetDouble init)
284 0 -1)]
285 [(SCM_UVECTOR_C32)
286 (Scm_C32VectorFill (SCM_C32VECTOR v) (Scm_GetHalfComplex init) 0 -1)]
287 [(SCM_UVECTOR_C64)
288 (Scm_C64VectorFill (SCM_C64VECTOR v) (Scm_GetFloatComplex init) 0 -1)]
289 [(SCM_UVECTOR_C128)
290 (Scm_C128VectorFill (SCM_C128VECTOR v)
291 (Scm_GetDoubleComplex init) 0 -1)]
292 [else (Scm_Error "[internal] Invalid uvector class: %S" klass)])
293 (return v))))
294
295 ;; generic copy
296 (inline-stub
297 (define-cproc uvector-copy (v::<uvector>
298 :optional (start::<fixnum> 0)
299 (end::<fixnum> -1))
300 (return (Scm_UVectorCopy v start end)))
301 )
302
303 ;; search
304 ;; rounding can be #f, 'floor or 'ceiling (SRFI-114 also uses symbols
305 ;; for rounding. we don't use 'round and 'truncate, though, for
306 ;; it doesn't make much sense.)
307 (inline-stub
308 ;; aux fn to deal with optional fixnum arg. we should make genstub handle
309 ;; this in future.
310 (define-cfn get-fixnum-arg (arg fallback::ScmSmallInt name::(const char*))
311 ::ScmSmallInt :static
312 (cond [(SCM_INTP arg) (return (SCM_INT_VALUE arg))]
313 [(SCM_FALSEP arg) (return fallback)]
314 [else (Scm_Error "%s expects fixnum or #f, but got: %S" name arg)
315 (return 0)])) ; dummy
316
317 (define-cproc uvector-binary-search (v::<uvector> key::<number>
318 :optional
319 (start #f)
320 (end #f)
321 (skip #f)
322 (rounding #f))
323 (let* ([len::ScmSize (SCM_UVECTOR_SIZE v)]
324 [s::ScmSmallInt (get-fixnum-arg start 0 "start")]
325 [e::ScmSmallInt (get-fixnum-arg end -1 "end")]
326 [p::ScmSmallInt (get-fixnum-arg skip 0 "skip")])
327 (SCM_CHECK_START_END s e len)
328 (unless (== (% (- e s) (+ p 1)) 0)
329 (Scm_Error "uvector size (%d) isn't multiple of record size (%d)"
330 (- e s) (+ p 1)))
331 (let* ([r::size_t (cast (size_t) -1)]
332 [lb::size_t]
333 [ub::size_t])
334 (case (Scm_UVectorType (Scm_ClassOf (SCM_OBJ v)))
335 [(SCM_UVECTOR_S8)
336 (let* ([k::int8_t
337 (Scm_GetInteger8Clamp key SCM_CLAMP_ERROR NULL)])
338 (set! r (Scm_BinarySearchS8 (+ (SCM_S8VECTOR_ELEMENTS v) s)
339 (- e s) k p (& lb) (& ub))))]
340 [(SCM_UVECTOR_U8)
341 (let* ([k::uint8_t
342 (Scm_GetIntegerU8Clamp key SCM_CLAMP_ERROR NULL)])
343 (set! r (Scm_BinarySearchU8 (+ (SCM_U8VECTOR_ELEMENTS v) s)
344 (- e s) k p (& lb) (& ub))))]
345 [(SCM_UVECTOR_S16)
346 (let* ([k::int16_t
347 (Scm_GetInteger16Clamp key SCM_CLAMP_ERROR NULL)])
348 (set! r (Scm_BinarySearchS16 (+ (SCM_S16VECTOR_ELEMENTS v) s)
349 (- e s) k p (& lb) (& ub))))]
350 [(SCM_UVECTOR_U16)
351 (let* ([k::uint16_t
352 (Scm_GetIntegerU16Clamp key SCM_CLAMP_ERROR NULL)])
353 (set! r (Scm_BinarySearchU16 (+ (SCM_U16VECTOR_ELEMENTS v) s)
354 (- e s) k p (& lb) (& ub))))]
355 [(SCM_UVECTOR_S32)
356 (let* ([k::int32_t
357 (Scm_GetInteger32Clamp key SCM_CLAMP_ERROR NULL)])
358 (set! r (Scm_BinarySearchS32 (+ (SCM_S32VECTOR_ELEMENTS v) s)
359 (- e s) k p (& lb) (& ub))))]
360 [(SCM_UVECTOR_U32)
361 (let* ([k::uint32_t
362 (Scm_GetIntegerU32Clamp key SCM_CLAMP_ERROR NULL)])
363 (set! r (Scm_BinarySearchU32 (+ (SCM_U32VECTOR_ELEMENTS v) s)
364 (- e s) k p (& lb) (& ub))))]
365 [(SCM_UVECTOR_S64)
366 (let* ([k::int64_t
367 (Scm_GetInteger64Clamp key SCM_CLAMP_ERROR NULL)])
368 (set! r (Scm_BinarySearchS64 (+ (SCM_S64VECTOR_ELEMENTS v) s)
369 (- e s) k p (& lb) (& ub))))]
370 [(SCM_UVECTOR_U64)
371 (let* ([k::uint64_t
372 (Scm_GetIntegerU64Clamp key SCM_CLAMP_ERROR NULL)])
373 (set! r (Scm_BinarySearchU64 (+ (SCM_U64VECTOR_ELEMENTS v) s)
374 (- e s) k p (& lb) (& ub))))]
375 [(SCM_UVECTOR_F16)
376 (let* ([k::ScmHalfFloat (Scm_DoubleToHalf (Scm_GetDouble key))])
377 (set! r (Scm_BinarySearchF16 (+ (SCM_F16VECTOR_ELEMENTS v) s)
378 (- e s) k p (& lb) (& ub))))]
379 [(SCM_UVECTOR_F32)
380 (let* ([k::float (Scm_GetDouble key)])
381 (set! r (Scm_BinarySearchF32 (+ (SCM_F32VECTOR_ELEMENTS v) s)
382 (- e s) k p (& lb) (& ub))))]
383 [(SCM_UVECTOR_F64)
384 (let* ([k::double (Scm_GetDouble key)])
385 (set! r (Scm_BinarySearchF64 (+ (SCM_F64VECTOR_ELEMENTS v) s)
386 (- e s) k p (& lb) (& ub))))]
387 [(SCM_UVECTOR_C32)
388 (let* ([k::ScmHalfComplex (Scm_GetHalfComplex key)])
389 (set! r (Scm_BinarySearchC32 (+ (SCM_C32VECTOR_ELEMENTS v) s)
390 (- e s) k p (& lb) (& ub))))]
391 [(SCM_UVECTOR_C64)
392 (let* ([k::(ScmFloatComplex) (Scm_GetFloatComplex key)])
393 (set! r (Scm_BinarySearchC64 (+ (SCM_C64VECTOR_ELEMENTS v) s)
394 (- e s) k p (& lb) (& ub))))]
395 [(SCM_UVECTOR_C128)
396 (let* ([k::(ScmDoubleComplex) (Scm_GetDoubleComplex key)])
397 (set! r (Scm_BinarySearchC128 (+ (SCM_C128VECTOR_ELEMENTS v) s)
398 (- e s) k p (& lb) (& ub))))]
399 [else (Scm_Error "[internal] Invalid uvector type: %S" v)]
400 )
401 (when (== r (cast (size_t) -1))
402 (cond
403 [(SCM_EQ rounding 'floor) (set! r lb)]
404 [(SCM_EQ rounding 'ceiling) (set! r ub)]
405 [(not (SCM_FALSEP rounding))
406 (Scm_Error "Rounding argument must be either #f, floor \
407 or ceiling, but got: %S" rounding)]))
408 (if (== r (cast (size_t) -1))
409 (return SCM_FALSE)
410 (return (Scm_MakeIntegerU (+ r s)))))))
411 )
412
413 ;; block i/o
414 (inline-stub
415 (define-cproc read-uvector! (v::<uvector>
416 :optional (port::<input-port> (current-input-port))
417 (start::<fixnum> 0)
418 (end::<fixnum> -1)
419 (endian::<symbol>? #f))
420 Scm_ReadBlockX)
421
422 (define-cproc read-uvector (klass::<class> size::<fixnum>
423 :optional (port::<input-port> (current-input-port))
424 (endian::<symbol>? #f))
425 (unless (Scm_SubtypeP klass SCM_CLASS_UVECTOR)
426 (Scm_TypeError "class" "uniform vector class" (SCM_OBJ klass)))
427 (let* ([v::ScmUVector* (cast ScmUVector* (Scm_MakeUVector klass size NULL))]
428 [r (Scm_ReadBlockX v port 0 size endian)])
429 (if (SCM_EOFP r)
430 (return r)
431 (begin
432 (SCM_ASSERT (SCM_INTP r))
433 (let* ([n::ScmSmallInt (SCM_INT_VALUE r)])
434 (SCM_ASSERT (and (<= n size) (<= 0 n)))
435 ;; NB: If read size is a lot shorter than requested size, we may
436 ;; want to copy it instead of just keeping the rest of vector
437 ;; unused.
438 (if (< n size)
439 (return (Scm_UVectorAlias klass v 0 n))
440 (return (SCM_OBJ v))))))))
441
442 (define-cproc write-uvector (v::<uvector>
443 :optional (port::<output-port> (current-output-port))
444 (start::<fixnum> 0)
445 (end::<fixnum> -1)
446 (endian::<symbol>? #f))
447 Scm_WriteBlock)
448 )
449
450 ;; R7RS bytevectors
451 (define (read-bytevector k :optional (port (current-input-port)))
452 (read-uvector <u8vector> k port))
453 (define (read-bytevector! bv :optional (port (current-input-port))
454 (start 0)
455 (end (uvector-length bv)))
456 (assume-type bv <u8vector>)
457 (read-uvector! bv port start end))
458 (define (write-bytevector bv :optional (port (current-output-port))
459 (start 0)
460 (end (uvector-length bv)))
461 (assume-type bv <u8vector>)
462 (write-uvector bv port start end))
463
464 ;; copy
465 (inline-stub
466 (define-cproc uvector-copy! (dest::<uvector> dstart::<fixnum> src::<uvector>
467 :optional (sstart::<fixnum> 0)
468 (send::<fixnum> -1))
469 ::<void>
470 (SCM_UVECTOR_CHECK_MUTABLE dest)
471 (SCM_CHECK_START_END sstart send (SCM_UVECTOR_SIZE src))
472 (let* ([deltsize::int (Scm_UVectorElementSize (Scm_ClassOf (SCM_OBJ dest)))]
473 [doff::ScmSmallInt (* dstart deltsize)]
474 [seltsize::int (Scm_UVectorElementSize (Scm_ClassOf (SCM_OBJ src)))]
475 [soff::ScmSmallInt (* sstart seltsize)]
476 [size::ScmSmallInt (- (* send seltsize) soff)])
477 (memmove (+ (cast char* (SCM_UVECTOR_ELEMENTS dest)) doff)
478 (+ (cast (const char*) (SCM_UVECTOR_ELEMENTS src)) soff)
479 size)))
480 )
481
482 ;; for the bakcward compatibility
483 (define read-block! read-uvector!)
484 (define write-block write-uvector)
485
486 ;; for symmetry of port->string, etc.
487 ;; This actually uses uvector port in gauche.vport, but that's inner
488 ;; details users shouldn't need to care.
489 ;; TODO: Optional endian argument
490 (define (port->uvector iport :optional (class <u8vector>))
491 (let1 p (open-output-uvector (make-uvector class 0) :extendable #t)
492 (copy-port iport p)
493 (get-output-uvector p :shared #t)))
494
495 ;;-------------------------------------------------------------
496 ;; Element range check (SRFI-160)
497 ;;
498
499 (define-cproc u8? (v) ::<boolean>
500 (return (and (SCM_INTP v)
501 (<= 0 (SCM_INT_VALUE v))
502 (< (SCM_INT_VALUE v) 256))))
503
504 (define-cproc s8? (v) ::<boolean>
505 (return (and (SCM_INTP v)
506 (<= -128 (SCM_INT_VALUE v))
507 (< (SCM_INT_VALUE v) 128))))
508
509 (define-cproc u16? (v) ::<boolean>
510 (return (and (SCM_INTP v)
511 (<= 0 (SCM_INT_VALUE v))
512 (< (SCM_INT_VALUE v) 65536))))
513
514 (define-cproc s16? (v) ::<boolean>
515 (return (and (SCM_INTP v)
516 (<= -32768 (SCM_INT_VALUE v))
517 (< (SCM_INT_VALUE v) 32768))))
518
519 (define-cproc u32? (v) ::<boolean>
520 (let* ([oor::int])
521 (cast void (Scm_GetIntegerU32Clamp v SCM_CLAMP_NONE (& oor)))
522 (return oor)))
523
524 (define-cproc s32? (v) ::<boolean>
525 (let* ([oor::int])
526 (cast void (Scm_GetInteger32Clamp v SCM_CLAMP_NONE (& oor)))
527 (return oor)))
528
529 (define-cproc u64? (v) ::<boolean>
530 (let* ([oor::int])
531 (cast void (Scm_GetIntegerU64Clamp v SCM_CLAMP_NONE (& oor)))
532 (return oor)))
533
534 (define-cproc s64? (v) ::<boolean>
535 (let* ([oor::int])
536 (cast void (Scm_GetInteger64Clamp v SCM_CLAMP_NONE (& oor)))
537 (return oor)))
538
539 (define-cproc f16? (v) ::<boolean> :fast-flonum (return (SCM_REALP v)))
540 (define-cproc f32? (v) ::<boolean> :fast-flonum (return (SCM_REALP v)))
541 (define-cproc f64? (v) ::<boolean> :fast-flonum (return (SCM_REALP v)))
542
543 (define-cproc c32? (v) ::<boolean> :fast-flonum (return (SCM_NUMBERP v)))
544 (define-cproc c64? (v) ::<boolean> :fast-flonum (return (SCM_NUMBERP v)))
545 (define-cproc c128? (v) ::<boolean> :fast-flonum (return (SCM_NUMBERP v)))
546
547 ;;-------------------------------------------------------------
548 ;; special coercers (most sequence methods are in uvlib.scm.tmpl
549 ;;
550
551 (define-method coerce-to ((dst <string-meta>) (src <u8vector>))
552 (u8vector->string src))
553 (define-method coerce-to ((dst <string-meta>) (src <s8vector>))
554 (s8vector->string src))
555 (define-method coerce-to ((dst <u8vector-meta>) (src <string>))
556 (string->u8vector src))
557 (define-method coerce-to ((dst <s8vector-meta>) (src <string>))
558 (string->s8vector src))
559 (define-method coerce-to ((dst <string-meta>) (src <u32vector>))
560 (u32vector->string src))
561 (define-method coerce-to ((dst <string-meta>) (src <s32vector>))
562 (s32vector->string src))
563 (define-method coerce-to ((dst <u32vector-meta>) (src <string>))
564 (string->u32vector src))
565 (define-method coerce-to ((dst <s32vector-meta>) (src <string>))
566 (string->s32vector src))
567
568 ;;-------------------------------------------------------------
569 ;; Bytevector aliases (R7RS compatibility)
570 ;;
571
572 (define (%adjust-fill-arg fill)
573 (cond [(<= 0 fill 255) fill]
574 [(<= -128 fill -1) (logand fill #xff)]
575 [else (error "fill argument out of range" fill)]))
576
577 (define (make-bytevector len :optional (fill 0))
578 (make-u8vector len (%adjust-fill-arg fill)))
579 (define (bytevector-fill! v fill) ; scheme.bytevector
580 (u8vector-fill! v (%adjust-fill-arg fill)))
581
582 (define-inline bytevector u8vector)
583 (define-inline bytevector? u8vector?)
584 (define-inline bytevector-length u8vector-length)
585 (define-inline bytevector-u8-ref u8vector-ref)
586 (define-inline bytevector-u8-set! u8vector-set!)
587 (define-inline bytevector-copy u8vector-copy)
588 (define-inline bytevector-copy! u8vector-copy!)
589 (define-inline bytevector-append u8vector-append)
590 (define-inline bytevector=? u8vector=?)
591
592 (define (bytevector-s8-set! v k b) ; scheme.bytevector
593 (bytevector-u8-set! v k (logand b #xff)))
594 (define bytevector-s8-ref ; scheme.bytevector
595 (letrec ([bytevector-s8-ref
596 (^[v k]
597 (let1 b (bytevector-u8-ref v k)
598 (if (>= b 128) (- b 256) b)))])
599 (getter-with-setter bytevector-s8-ref bytevector-s8-set!)))
600
601 (define (bytevector-copy!-r6 src sstart target tstart len) ; scheme.bytevector
602 (u8vector-copy! target tstart src sstart (+ sstart len)))
603
604 (define (bytevector->u8-list v) (u8vector->list v)) ; scheme.bytevector
605 (define (u8-list->bytevector lis) (list->u8vector lis)) ; scheme.bytevector