summaryrefslogtreecommitdiff
path: root/libguile/uniform.c
blob: d3ecb1bc916b4ad7ff75371787d5b4a15bffe5fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */




#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "libguile/_scm.h"
#include "libguile/__scm.h"

#include "libguile/uniform.h"


const size_t scm_i_array_element_type_sizes[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] = {
  0,
  0,
  1,
  8,
  8, 8,
  16, 16,
  32, 32,
  64, 64,
  32, 64,
  64, 128
};

size_t
scm_array_handle_uniform_element_size (scm_t_array_handle *h)
{
  size_t ret = scm_i_array_element_type_sizes[h->element_type];
  if (ret && ret % 8 == 0)
    return ret / 8;
  else if (ret)
    scm_wrong_type_arg_msg (NULL, 0, h->array, "byte-aligned uniform array");
  else
    scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
}

size_t
scm_array_handle_uniform_element_bit_size (scm_t_array_handle *h)
{
  size_t ret = scm_i_array_element_type_sizes[h->element_type];
  if (ret)
    return ret;
  else
    scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
}

const void *
scm_array_handle_uniform_elements (scm_t_array_handle *h)
{
  return scm_array_handle_uniform_writable_elements (h);
}

void *
scm_array_handle_uniform_writable_elements (scm_t_array_handle *h)
{
  size_t esize;
  scm_t_uint8 *ret;

  esize = scm_array_handle_uniform_element_size (h);
  ret = ((scm_t_uint8*) h->writable_elements) + h->base * esize;
  return ret;
}

int
scm_is_uniform_vector (SCM obj)
{
  scm_t_array_handle h;
  int ret = 0;

  if (scm_is_generalized_vector (obj))
    {
      scm_generalized_vector_get_handle (obj, &h);
      ret = SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED (h.element_type);
      scm_array_handle_release (&h);
    }
  return ret;
}

size_t
scm_c_uniform_vector_length (SCM uvec)
{
  if (!scm_is_uniform_vector (uvec))
    scm_wrong_type_arg_msg ("uniform-vector-length", 1, uvec,
                            "uniform vector");

  return scm_c_generalized_vector_length (uvec);
}

SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0,
	    (SCM obj),
	    "Return @code{#t} if @var{obj} is a uniform vector.")
#define FUNC_NAME s_scm_uniform_vector_p
{
  return scm_from_bool (scm_is_uniform_vector (obj));
}
#undef FUNC_NAME

SCM_DEFINE (scm_uniform_vector_element_type, "uniform-vector-element-type", 1, 0, 0,
	    (SCM v),
	    "Return the type of the elements in the uniform vector, @var{v}.")
#define FUNC_NAME s_scm_uniform_vector_element_type
{
  scm_t_array_handle h;
  SCM ret;
  
  if (!scm_is_uniform_vector (v))
    scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, v, "uniform vector");
  scm_array_get_handle (v, &h);
  ret = scm_array_handle_element_type (&h);
  scm_array_handle_release (&h);
  return ret;
}
#undef FUNC_NAME

SCM_DEFINE (scm_uniform_vector_element_size, "uniform-vector-element-size", 1, 0, 0,
	    (SCM v),
	    "Return the number of bytes allocated to each element in the\n"
            "uniform vector, @var{v}.")
#define FUNC_NAME s_scm_uniform_vector_element_size
{
  scm_t_array_handle h;
  size_t len;
  ssize_t inc;
  SCM ret;
  scm_uniform_vector_elements (v, &h, &len, &inc);
  ret = scm_from_size_t (scm_array_handle_uniform_element_size (&h));
  scm_array_handle_release (&h);
  return ret;
}
#undef FUNC_NAME

SCM
scm_c_uniform_vector_ref (SCM v, size_t idx)
{
  if (!scm_is_uniform_vector (v))
    scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
  return scm_c_generalized_vector_ref (v, idx);
}

SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
	    (SCM v, SCM idx),
	    "Return the element at index @var{idx} of the\n"
	    "homogeneous numeric vector @var{v}.")
#define FUNC_NAME s_scm_uniform_vector_ref
{
  return scm_c_uniform_vector_ref (v, scm_to_size_t (idx));
}
#undef FUNC_NAME

void
scm_c_uniform_vector_set_x (SCM v, size_t idx, SCM val)
{
  if (!scm_is_uniform_vector (v))
    scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
  scm_c_generalized_vector_set_x (v, idx, val);
}

SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0,
	    (SCM v, SCM idx, SCM val),
	    "Set the element at index @var{idx} of the\n"
	    "homogeneous numeric vector @var{v} to @var{val}.")
#define FUNC_NAME s_scm_uniform_vector_set_x
{
  scm_c_uniform_vector_set_x (v, scm_to_size_t (idx), val);
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0,
            (SCM uvec),
	    "Convert the uniform numeric vector @var{uvec} to a list.")
#define FUNC_NAME s_scm_uniform_vector_to_list
{
  if (!scm_is_uniform_vector (uvec))
    scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec, "uniform vector");
  return scm_generalized_vector_to_list (uvec);
}
#undef FUNC_NAME

const void *
scm_uniform_vector_elements (SCM uvec, 
			     scm_t_array_handle *h,
			     size_t *lenp, ssize_t *incp)
{
  return scm_uniform_vector_writable_elements (uvec, h, lenp, incp);
}

void *
scm_uniform_vector_writable_elements (SCM uvec, 
				      scm_t_array_handle *h,
				      size_t *lenp, ssize_t *incp)
{
  void *ret;
  scm_generalized_vector_get_handle (uvec, h);
  /* FIXME nonlocal exit */
  ret = scm_array_handle_uniform_writable_elements (h);
  if (lenp)
    {
      scm_t_array_dim *dim = scm_array_handle_dims (h);
      *lenp = dim->ubnd - dim->lbnd + 1;
      *incp = dim->inc;
    }
  return ret;
}

SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0, 
	    (SCM v),
	    "Return the number of elements in the uniform vector @var{v}.")
#define FUNC_NAME s_scm_uniform_vector_length
{
  return scm_from_size_t (scm_c_uniform_vector_length (v));
}
#undef FUNC_NAME


void
scm_init_uniform (void)
{
#include "libguile/uniform.x"
}

/*
  Local Variables:
  c-file-style: "gnu"
  End:
*/