1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-02 23:50:47 +02:00

Don't use generalized-vector functions in uniform.c

* libguile/uniform.c
  - (scm_is_uniform_vector): replace scm_is_generalized_vector and
    scm_generalized_vector_get_handle by scm_is_array and manual rank check.
  - (scm_c_uniform_vector_length): inline length computation. This
    removes a redundant rank check.
  - (scm_c_uniform_vector_ref): inline impl->vref use. This removes
    a redundant rank check.
  - (scm_c_uniform_vector_set): inline impl->vset use. This removes
    a redundant rank check.
  - (scm_uniform_vector_writable_elements): replace
    scm_generalized_vector_get_handle by scm_array_get_handle.

* test-suite/test/arrays.test
  - rename uniform-vector-ref block to uniform-vector.
  - exercise uniform-vector-length and shared arrays remaining uniform.
This commit is contained in:
Daniel Llorens 2013-04-08 13:13:21 +02:00 committed by Andy Wingo
parent 07f4a9151e
commit 4569bbf7f6
2 changed files with 51 additions and 14 deletions

View file

@ -87,10 +87,11 @@ scm_is_uniform_vector (SCM obj)
scm_t_array_handle h; scm_t_array_handle h;
int ret = 0; int ret = 0;
if (scm_is_generalized_vector (obj)) if (scm_is_array (obj))
{ {
scm_generalized_vector_get_handle (obj, &h); scm_array_get_handle (obj, &h);
ret = SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED (h.element_type); ret = 1 == scm_array_handle_rank (&h)
&& SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED (h.element_type);
scm_array_handle_release (&h); scm_array_handle_release (&h);
} }
return ret; return ret;
@ -99,11 +100,16 @@ scm_is_uniform_vector (SCM obj)
size_t size_t
scm_c_uniform_vector_length (SCM uvec) scm_c_uniform_vector_length (SCM uvec)
{ {
scm_t_array_handle h;
size_t ret;
if (!scm_is_uniform_vector (uvec)) if (!scm_is_uniform_vector (uvec))
scm_wrong_type_arg_msg ("uniform-vector-length", 1, uvec, scm_wrong_type_arg_msg ("uniform-vector-length", 1, uvec,
"uniform vector"); "uniform vector");
return scm_c_generalized_vector_length (uvec); scm_array_get_handle (uvec, &h);
ret = h.dims[0].ubnd - h.dims[0].lbnd + 1;
scm_array_handle_release (&h);
return ret;
} }
SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0, SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0,
@ -169,11 +175,20 @@ SCM_DEFINE (scm_uniform_vector_element_size, "uniform-vector-element-size", 1, 0
#undef FUNC_NAME #undef FUNC_NAME
SCM SCM
scm_c_uniform_vector_ref (SCM v, size_t idx) scm_c_uniform_vector_ref (SCM v, size_t pos)
{ {
scm_t_array_handle h;
SCM ret;
if (!scm_is_uniform_vector (v)) if (!scm_is_uniform_vector (v))
scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
return scm_c_generalized_vector_ref (v, idx);
scm_array_get_handle (v, &h);
pos = h.base + h.dims[0].lbnd + pos * h.dims[0].inc;
ret = h.impl->vref (&h, pos);
scm_array_handle_release (&h);
return ret;
} }
SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0, SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
@ -187,11 +202,17 @@ SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
#undef FUNC_NAME #undef FUNC_NAME
void void
scm_c_uniform_vector_set_x (SCM v, size_t idx, SCM val) scm_c_uniform_vector_set_x (SCM v, size_t pos, SCM val)
{ {
scm_t_array_handle h;
if (!scm_is_uniform_vector (v)) if (!scm_is_uniform_vector (v))
scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
scm_c_generalized_vector_set_x (v, idx, val);
scm_array_get_handle (v, &h);
pos = h.base + h.dims[0].lbnd + pos * h.dims[0].inc;
h.impl->vset (&h, pos, val);
scm_array_handle_release (&h);
} }
SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0, SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0,
@ -225,12 +246,12 @@ scm_uniform_vector_elements (SCM uvec,
} }
void * void *
scm_uniform_vector_writable_elements (SCM uvec, scm_uniform_vector_writable_elements (SCM uvec,
scm_t_array_handle *h, scm_t_array_handle *h,
size_t *lenp, ssize_t *incp) size_t *lenp, ssize_t *incp)
{ {
void *ret; void *ret;
scm_generalized_vector_get_handle (uvec, h); scm_array_get_handle (uvec, h);
/* FIXME nonlocal exit */ /* FIXME nonlocal exit */
ret = scm_array_handle_uniform_writable_elements (h); ret = scm_array_handle_uniform_writable_elements (h);
if (lenp) if (lenp)

View file

@ -574,12 +574,12 @@
(eqv? 8 (array-ref s2 2)))))) (eqv? 8 (array-ref s2 2))))))
;;; ;;;
;;; uniform-vector-ref ;;; uniform-vector
;;; ;;;
(with-test-prefix "uniform-vector-ref" (with-test-prefix "uniform-vector"
(with-test-prefix "byte" (with-test-prefix "uniform-vector-ref byte"
(let ((a (make-s8vector 1))) (let ((a (make-s8vector 1)))
@ -594,7 +594,23 @@
(pass-if "-128" (pass-if "-128"
(begin (begin
(array-set! a -128 0) (array-set! a -128 0)
(= -128 (uniform-vector-ref a 0))))))) (= -128 (uniform-vector-ref a 0))))))
(with-test-prefix "shared with rank 1 remain uniform vectors"
(let ((a #f64(1 2 3 4)))
(pass-if "change offset"
(let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
(and (uniform-vector? b)
(= 3 (uniform-vector-length b))
(array-equal? b #f64(2 3 4)))))
(pass-if "change stride"
(let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
(and (uniform-vector? c)
(= 2 (uniform-vector-length c))
(array-equal? c #f64(1 3))))))))
;;; ;;;
;;; syntax ;;; syntax