uniform.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/__scm.h"
  23. #include "libguile/uniform.h"
  24. const size_t scm_i_array_element_type_sizes[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] = {
  25. 0,
  26. 0,
  27. 1,
  28. 8,
  29. 8, 8,
  30. 16, 16,
  31. 32, 32,
  32. 64, 64,
  33. 32, 64,
  34. 64, 128
  35. };
  36. size_t
  37. scm_array_handle_uniform_element_size (scm_t_array_handle *h)
  38. {
  39. size_t ret = scm_i_array_element_type_sizes[h->element_type];
  40. if (ret && ret % 8 == 0)
  41. return ret / 8;
  42. else if (ret)
  43. scm_wrong_type_arg_msg (NULL, 0, h->array, "byte-aligned uniform array");
  44. else
  45. scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
  46. }
  47. size_t
  48. scm_array_handle_uniform_element_bit_size (scm_t_array_handle *h)
  49. {
  50. size_t ret = scm_i_array_element_type_sizes[h->element_type];
  51. if (ret)
  52. return ret;
  53. else
  54. scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
  55. }
  56. const void *
  57. scm_array_handle_uniform_elements (scm_t_array_handle *h)
  58. {
  59. return scm_array_handle_uniform_writable_elements (h);
  60. }
  61. void *
  62. scm_array_handle_uniform_writable_elements (scm_t_array_handle *h)
  63. {
  64. size_t esize;
  65. scm_t_uint8 *ret;
  66. esize = scm_array_handle_uniform_element_size (h);
  67. ret = ((scm_t_uint8*) h->writable_elements) + h->base * esize;
  68. return ret;
  69. }
  70. int
  71. scm_is_uniform_vector (SCM obj)
  72. {
  73. scm_t_array_handle h;
  74. int ret = 0;
  75. if (scm_is_generalized_vector (obj))
  76. {
  77. scm_generalized_vector_get_handle (obj, &h);
  78. ret = SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED (h.element_type);
  79. scm_array_handle_release (&h);
  80. }
  81. return ret;
  82. }
  83. size_t
  84. scm_c_uniform_vector_length (SCM uvec)
  85. {
  86. if (!scm_is_uniform_vector (uvec))
  87. scm_wrong_type_arg_msg ("uniform-vector-length", 1, uvec,
  88. "uniform vector");
  89. return scm_c_generalized_vector_length (uvec);
  90. }
  91. SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0,
  92. (SCM obj),
  93. "Return @code{#t} if @var{obj} is a uniform vector.")
  94. #define FUNC_NAME s_scm_uniform_vector_p
  95. {
  96. return scm_from_bool (scm_is_uniform_vector (obj));
  97. }
  98. #undef FUNC_NAME
  99. SCM_DEFINE (scm_uniform_vector_element_type, "uniform-vector-element-type", 1, 0, 0,
  100. (SCM v),
  101. "Return the type of the elements in the uniform vector, @var{v}.")
  102. #define FUNC_NAME s_scm_uniform_vector_element_type
  103. {
  104. scm_t_array_handle h;
  105. SCM ret;
  106. if (!scm_is_uniform_vector (v))
  107. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, v, "uniform vector");
  108. scm_array_get_handle (v, &h);
  109. ret = scm_array_handle_element_type (&h);
  110. scm_array_handle_release (&h);
  111. return ret;
  112. }
  113. #undef FUNC_NAME
  114. SCM_DEFINE (scm_uniform_vector_element_size, "uniform-vector-element-size", 1, 0, 0,
  115. (SCM v),
  116. "Return the number of bytes allocated to each element in the\n"
  117. "uniform vector, @var{v}.")
  118. #define FUNC_NAME s_scm_uniform_vector_element_size
  119. {
  120. scm_t_array_handle h;
  121. size_t len;
  122. ssize_t inc;
  123. SCM ret;
  124. scm_uniform_vector_elements (v, &h, &len, &inc);
  125. ret = scm_from_size_t (scm_array_handle_uniform_element_size (&h));
  126. scm_array_handle_release (&h);
  127. return ret;
  128. }
  129. #undef FUNC_NAME
  130. SCM
  131. scm_c_uniform_vector_ref (SCM v, size_t idx)
  132. {
  133. if (!scm_is_uniform_vector (v))
  134. scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
  135. return scm_c_generalized_vector_ref (v, idx);
  136. }
  137. SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
  138. (SCM v, SCM idx),
  139. "Return the element at index @var{idx} of the\n"
  140. "homogeneous numeric vector @var{v}.")
  141. #define FUNC_NAME s_scm_uniform_vector_ref
  142. {
  143. return scm_c_uniform_vector_ref (v, scm_to_size_t (idx));
  144. }
  145. #undef FUNC_NAME
  146. void
  147. scm_c_uniform_vector_set_x (SCM v, size_t idx, SCM val)
  148. {
  149. if (!scm_is_uniform_vector (v))
  150. scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector");
  151. scm_c_generalized_vector_set_x (v, idx, val);
  152. }
  153. SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0,
  154. (SCM v, SCM idx, SCM val),
  155. "Set the element at index @var{idx} of the\n"
  156. "homogeneous numeric vector @var{v} to @var{val}.")
  157. #define FUNC_NAME s_scm_uniform_vector_set_x
  158. {
  159. scm_c_uniform_vector_set_x (v, scm_to_size_t (idx), val);
  160. return SCM_UNSPECIFIED;
  161. }
  162. #undef FUNC_NAME
  163. SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0,
  164. (SCM uvec),
  165. "Convert the uniform numeric vector @var{uvec} to a list.")
  166. #define FUNC_NAME s_scm_uniform_vector_to_list
  167. {
  168. if (!scm_is_uniform_vector (uvec))
  169. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec, "uniform vector");
  170. return scm_generalized_vector_to_list (uvec);
  171. }
  172. #undef FUNC_NAME
  173. const void *
  174. scm_uniform_vector_elements (SCM uvec,
  175. scm_t_array_handle *h,
  176. size_t *lenp, ssize_t *incp)
  177. {
  178. return scm_uniform_vector_writable_elements (uvec, h, lenp, incp);
  179. }
  180. void *
  181. scm_uniform_vector_writable_elements (SCM uvec,
  182. scm_t_array_handle *h,
  183. size_t *lenp, ssize_t *incp)
  184. {
  185. void *ret;
  186. scm_generalized_vector_get_handle (uvec, h);
  187. /* FIXME nonlocal exit */
  188. ret = scm_array_handle_uniform_writable_elements (h);
  189. if (lenp)
  190. {
  191. scm_t_array_dim *dim = scm_array_handle_dims (h);
  192. *lenp = dim->ubnd - dim->lbnd + 1;
  193. *incp = dim->inc;
  194. }
  195. return ret;
  196. }
  197. SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0,
  198. (SCM v),
  199. "Return the number of elements in the uniform vector @var{v}.")
  200. #define FUNC_NAME s_scm_uniform_vector_length
  201. {
  202. return scm_from_size_t (scm_c_uniform_vector_length (v));
  203. }
  204. #undef FUNC_NAME
  205. void
  206. scm_init_uniform (void)
  207. {
  208. #include "libguile/uniform.x"
  209. }
  210. /*
  211. Local Variables:
  212. c-file-style: "gnu"
  213. End:
  214. */