srfi-4.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* srfi-4.c --- Uniform numeric vector datatypes.
  2. *
  3. * Copyright (C) 2001, 2004, 2006, 2010, 2012 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include "libguile/_scm.h"
  26. #include "libguile/__scm.h"
  27. #include "libguile/srfi-4.h"
  28. #include "libguile/error.h"
  29. #include "libguile/read.h"
  30. #include "libguile/ports.h"
  31. #include "libguile/chars.h"
  32. #include "libguile/vectors.h"
  33. #include "libguile/unif.h"
  34. #include "libguile/strings.h"
  35. #include "libguile/strports.h"
  36. #include "libguile/dynwind.h"
  37. #include "libguile/deprecation.h"
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef HAVE_IO_H
  42. #include <io.h>
  43. #endif
  44. /* Smob type code for uniform numeric vectors. */
  45. int scm_tc16_uvec = 0;
  46. #define SCM_IS_UVEC(obj) SCM_SMOB_PREDICATE (scm_tc16_uvec, (obj))
  47. /* Accessor macros for the three components of a uniform numeric
  48. vector:
  49. - The type tag (one of the symbolic constants below).
  50. - The vector's length (counted in elements).
  51. - The address of the data area (holding the elements of the
  52. vector). */
  53. #define SCM_UVEC_TYPE(u) (SCM_CELL_WORD_1(u))
  54. #define SCM_UVEC_LENGTH(u) ((size_t)SCM_CELL_WORD_2(u))
  55. #define SCM_UVEC_BASE(u) ((void *)SCM_CELL_WORD_3(u))
  56. /* Symbolic constants encoding the various types of uniform
  57. numeric vectors. */
  58. #define SCM_UVEC_U8 0
  59. #define SCM_UVEC_S8 1
  60. #define SCM_UVEC_U16 2
  61. #define SCM_UVEC_S16 3
  62. #define SCM_UVEC_U32 4
  63. #define SCM_UVEC_S32 5
  64. #define SCM_UVEC_U64 6
  65. #define SCM_UVEC_S64 7
  66. #define SCM_UVEC_F32 8
  67. #define SCM_UVEC_F64 9
  68. #define SCM_UVEC_C32 10
  69. #define SCM_UVEC_C64 11
  70. /* This array maps type tags to the size of the elements. */
  71. static const int uvec_sizes[12] = {
  72. 1, 1,
  73. 2, 2,
  74. 4, 4,
  75. 8, 8,
  76. sizeof(float), sizeof(double),
  77. 2*sizeof(float), 2*sizeof(double)
  78. };
  79. static const char *uvec_tags[12] = {
  80. "u8", "s8",
  81. "u16", "s16",
  82. "u32", "s32",
  83. "u64", "s64",
  84. "f32", "f64",
  85. "c32", "c64",
  86. };
  87. static const char *uvec_names[12] = {
  88. "u8vector", "s8vector",
  89. "u16vector", "s16vector",
  90. "u32vector", "s32vector",
  91. "u64vector", "s64vector",
  92. "f32vector", "f64vector",
  93. "c32vector", "c64vector"
  94. };
  95. /* ================================================================ */
  96. /* SMOB procedures. */
  97. /* ================================================================ */
  98. /* Smob print hook for uniform vectors. */
  99. static int
  100. uvec_print (SCM uvec, SCM port, scm_print_state *pstate)
  101. {
  102. union {
  103. scm_t_uint8 *u8;
  104. scm_t_int8 *s8;
  105. scm_t_uint16 *u16;
  106. scm_t_int16 *s16;
  107. scm_t_uint32 *u32;
  108. scm_t_int32 *s32;
  109. scm_t_uint64 *u64;
  110. scm_t_int64 *s64;
  111. float *f32;
  112. double *f64;
  113. SCM *fake_64;
  114. } np;
  115. size_t i = 0;
  116. const size_t uvlen = SCM_UVEC_LENGTH (uvec);
  117. void *uptr = SCM_UVEC_BASE (uvec);
  118. switch (SCM_UVEC_TYPE (uvec))
  119. {
  120. case SCM_UVEC_U8: np.u8 = (scm_t_uint8 *) uptr; break;
  121. case SCM_UVEC_S8: np.s8 = (scm_t_int8 *) uptr; break;
  122. case SCM_UVEC_U16: np.u16 = (scm_t_uint16 *) uptr; break;
  123. case SCM_UVEC_S16: np.s16 = (scm_t_int16 *) uptr; break;
  124. case SCM_UVEC_U32: np.u32 = (scm_t_uint32 *) uptr; break;
  125. case SCM_UVEC_S32: np.s32 = (scm_t_int32 *) uptr; break;
  126. case SCM_UVEC_U64: np.u64 = (scm_t_uint64 *) uptr; break;
  127. case SCM_UVEC_S64: np.s64 = (scm_t_int64 *) uptr; break;
  128. case SCM_UVEC_F32: np.f32 = (float *) uptr; break;
  129. case SCM_UVEC_F64: np.f64 = (double *) uptr; break;
  130. case SCM_UVEC_C32: np.f32 = (float *) uptr; break;
  131. case SCM_UVEC_C64: np.f64 = (double *) uptr; break;
  132. default:
  133. abort (); /* Sanity check. */
  134. break;
  135. }
  136. scm_putc ('#', port);
  137. scm_puts (uvec_tags [SCM_UVEC_TYPE (uvec)], port);
  138. scm_putc ('(', port);
  139. while (i < uvlen)
  140. {
  141. if (i != 0) scm_puts (" ", port);
  142. switch (SCM_UVEC_TYPE (uvec))
  143. {
  144. case SCM_UVEC_U8: scm_uintprint (*np.u8, 10, port); np.u8++; break;
  145. case SCM_UVEC_S8: scm_intprint (*np.s8, 10, port); np.s8++; break;
  146. case SCM_UVEC_U16: scm_uintprint (*np.u16, 10, port); np.u16++; break;
  147. case SCM_UVEC_S16: scm_intprint (*np.s16, 10, port); np.s16++; break;
  148. case SCM_UVEC_U32: scm_uintprint (*np.u32, 10, port); np.u32++; break;
  149. case SCM_UVEC_S32: scm_intprint (*np.s32, 10, port); np.s32++; break;
  150. case SCM_UVEC_U64: scm_uintprint (*np.u64, 10, port); np.u64++; break;
  151. case SCM_UVEC_S64: scm_intprint (*np.s64, 10, port); np.s64++; break;
  152. case SCM_UVEC_F32: scm_i_print_double (*np.f32, port); np.f32++; break;
  153. case SCM_UVEC_F64: scm_i_print_double (*np.f64, port); np.f64++; break;
  154. case SCM_UVEC_C32:
  155. scm_i_print_complex (np.f32[0], np.f32[1], port);
  156. np.f32 += 2;
  157. break;
  158. case SCM_UVEC_C64:
  159. scm_i_print_complex (np.f64[0], np.f64[1], port);
  160. np.f64 += 2;
  161. break;
  162. default:
  163. abort (); /* Sanity check. */
  164. break;
  165. }
  166. i++;
  167. }
  168. scm_remember_upto_here_1 (uvec);
  169. scm_puts (")", port);
  170. return 1;
  171. }
  172. const char *
  173. scm_i_uniform_vector_tag (SCM uvec)
  174. {
  175. return uvec_tags[SCM_UVEC_TYPE (uvec)];
  176. }
  177. static SCM
  178. uvec_equalp (SCM a, SCM b)
  179. {
  180. SCM result = SCM_BOOL_T;
  181. if (SCM_UVEC_TYPE (a) != SCM_UVEC_TYPE (b))
  182. result = SCM_BOOL_F;
  183. else if (SCM_UVEC_LENGTH (a) != SCM_UVEC_LENGTH (b))
  184. result = SCM_BOOL_F;
  185. else if (memcmp (SCM_UVEC_BASE (a), SCM_UVEC_BASE (b),
  186. SCM_UVEC_LENGTH (a) * uvec_sizes[SCM_UVEC_TYPE(a)]) != 0)
  187. result = SCM_BOOL_F;
  188. scm_remember_upto_here_2 (a, b);
  189. return result;
  190. }
  191. /* Smob free hook for uniform numeric vectors. */
  192. static size_t
  193. uvec_free (SCM uvec)
  194. {
  195. int type = SCM_UVEC_TYPE (uvec);
  196. scm_gc_free (SCM_UVEC_BASE (uvec),
  197. SCM_UVEC_LENGTH (uvec) * uvec_sizes[type],
  198. uvec_names[type]);
  199. return 0;
  200. }
  201. /* ================================================================ */
  202. /* Utility procedures. */
  203. /* ================================================================ */
  204. static SCM_C_INLINE_KEYWORD int
  205. is_uvec (int type, SCM obj)
  206. {
  207. if (SCM_IS_UVEC (obj))
  208. return SCM_UVEC_TYPE (obj) == type;
  209. if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
  210. {
  211. SCM v = SCM_I_ARRAY_V (obj);
  212. return SCM_IS_UVEC (v) && SCM_UVEC_TYPE (v) == type;
  213. }
  214. return 0;
  215. }
  216. static SCM_C_INLINE_KEYWORD SCM
  217. uvec_p (int type, SCM obj)
  218. {
  219. return scm_from_bool (is_uvec (type, obj));
  220. }
  221. static SCM_C_INLINE_KEYWORD void
  222. uvec_assert (int type, SCM obj)
  223. {
  224. if (!is_uvec (type, obj))
  225. scm_wrong_type_arg_msg (NULL, 0, obj, uvec_names[type]);
  226. }
  227. static SCM
  228. take_uvec (int type, void *base, size_t len)
  229. {
  230. SCM_RETURN_NEWSMOB3 (scm_tc16_uvec, type, len, (scm_t_bits) base);
  231. }
  232. /* Create a new, uninitialized uniform numeric vector of type TYPE
  233. with space for LEN elements. */
  234. static SCM
  235. alloc_uvec (int type, size_t len)
  236. {
  237. void *base;
  238. if (len > SCM_I_SIZE_MAX / uvec_sizes[type])
  239. scm_out_of_range (NULL, scm_from_size_t (len));
  240. base = scm_gc_malloc (len * uvec_sizes[type], uvec_names[type]);
  241. return take_uvec (type, base, len);
  242. }
  243. /* GCC doesn't seem to want to optimize unused switch clauses away,
  244. so we use a big 'if' in the next two functions.
  245. */
  246. static SCM_C_INLINE_KEYWORD SCM
  247. uvec_fast_ref (int type, const void *base, size_t c_idx)
  248. {
  249. if (type == SCM_UVEC_U8)
  250. return scm_from_uint8 (((scm_t_uint8*)base)[c_idx]);
  251. else if (type == SCM_UVEC_S8)
  252. return scm_from_int8 (((scm_t_int8*)base)[c_idx]);
  253. else if (type == SCM_UVEC_U16)
  254. return scm_from_uint16 (((scm_t_uint16*)base)[c_idx]);
  255. else if (type == SCM_UVEC_S16)
  256. return scm_from_int16 (((scm_t_int16*)base)[c_idx]);
  257. else if (type == SCM_UVEC_U32)
  258. return scm_from_uint32 (((scm_t_uint32*)base)[c_idx]);
  259. else if (type == SCM_UVEC_S32)
  260. return scm_from_int32 (((scm_t_int32*)base)[c_idx]);
  261. else if (type == SCM_UVEC_U64)
  262. return scm_from_uint64 (((scm_t_uint64*)base)[c_idx]);
  263. else if (type == SCM_UVEC_S64)
  264. return scm_from_int64 (((scm_t_int64*)base)[c_idx]);
  265. else if (type == SCM_UVEC_F32)
  266. return scm_from_double (((float*)base)[c_idx]);
  267. else if (type == SCM_UVEC_F64)
  268. return scm_from_double (((double*)base)[c_idx]);
  269. else if (type == SCM_UVEC_C32)
  270. return scm_c_make_rectangular (((float*)base)[2*c_idx],
  271. ((float*)base)[2*c_idx+1]);
  272. else if (type == SCM_UVEC_C64)
  273. return scm_c_make_rectangular (((double*)base)[2*c_idx],
  274. ((double*)base)[2*c_idx+1]);
  275. else
  276. return SCM_BOOL_F;
  277. }
  278. static SCM_C_INLINE_KEYWORD void
  279. uvec_fast_set_x (int type, void *base, size_t c_idx, SCM val)
  280. {
  281. if (type == SCM_UVEC_U8)
  282. (((scm_t_uint8*)base)[c_idx]) = scm_to_uint8 (val);
  283. else if (type == SCM_UVEC_S8)
  284. (((scm_t_int8*)base)[c_idx]) = scm_to_int8 (val);
  285. else if (type == SCM_UVEC_U16)
  286. (((scm_t_uint16*)base)[c_idx]) = scm_to_uint16 (val);
  287. else if (type == SCM_UVEC_S16)
  288. (((scm_t_int16*)base)[c_idx]) = scm_to_int16 (val);
  289. else if (type == SCM_UVEC_U32)
  290. (((scm_t_uint32*)base)[c_idx]) = scm_to_uint32 (val);
  291. else if (type == SCM_UVEC_S32)
  292. (((scm_t_int32*)base)[c_idx]) = scm_to_int32 (val);
  293. else if (type == SCM_UVEC_U64)
  294. (((scm_t_uint64*)base)[c_idx]) = scm_to_uint64 (val);
  295. else if (type == SCM_UVEC_S64)
  296. (((scm_t_int64*)base)[c_idx]) = scm_to_int64 (val);
  297. else if (type == SCM_UVEC_F32)
  298. (((float*)base)[c_idx]) = scm_to_double (val);
  299. else if (type == SCM_UVEC_F64)
  300. (((double*)base)[c_idx]) = scm_to_double (val);
  301. else if (type == SCM_UVEC_C32)
  302. {
  303. (((float*)base)[2*c_idx]) = scm_c_real_part (val);
  304. (((float*)base)[2*c_idx+1]) = scm_c_imag_part (val);
  305. }
  306. else if (type == SCM_UVEC_C64)
  307. {
  308. (((double*)base)[2*c_idx]) = scm_c_real_part (val);
  309. (((double*)base)[2*c_idx+1]) = scm_c_imag_part (val);
  310. }
  311. }
  312. static SCM_C_INLINE_KEYWORD SCM
  313. make_uvec (int type, SCM len, SCM fill)
  314. {
  315. size_t c_len = scm_to_size_t (len);
  316. SCM uvec = alloc_uvec (type, c_len);
  317. if (!SCM_UNBNDP (fill))
  318. {
  319. size_t idx;
  320. void *base = SCM_UVEC_BASE (uvec);
  321. for (idx = 0; idx < c_len; idx++)
  322. uvec_fast_set_x (type, base, idx, fill);
  323. }
  324. return uvec;
  325. }
  326. static SCM_C_INLINE_KEYWORD void *
  327. uvec_writable_elements (int type, SCM uvec, scm_t_array_handle *handle,
  328. size_t *lenp, ssize_t *incp)
  329. {
  330. if (type >= 0)
  331. {
  332. SCM v = uvec;
  333. if (SCM_I_ARRAYP (v))
  334. v = SCM_I_ARRAY_V (v);
  335. uvec_assert (type, v);
  336. }
  337. return scm_uniform_vector_writable_elements (uvec, handle, lenp, incp);
  338. }
  339. static SCM_C_INLINE_KEYWORD const void *
  340. uvec_elements (int type, SCM uvec, scm_t_array_handle *handle,
  341. size_t *lenp, ssize_t *incp)
  342. {
  343. return uvec_writable_elements (type, uvec, handle, lenp, incp);
  344. }
  345. static int
  346. uvec_type (scm_t_array_handle *h)
  347. {
  348. SCM v = h->array;
  349. if (SCM_I_ARRAYP (v))
  350. v = SCM_I_ARRAY_V (v);
  351. return SCM_UVEC_TYPE (v);
  352. }
  353. static SCM
  354. uvec_to_list (int type, SCM uvec)
  355. {
  356. scm_t_array_handle handle;
  357. size_t len;
  358. ssize_t i, inc;
  359. SCM res = SCM_EOL;
  360. (void) uvec_elements (type, uvec, &handle, &len, &inc);
  361. for (i = len*inc; i > 0;)
  362. {
  363. i -= inc;
  364. res = scm_cons (scm_array_handle_ref (&handle, i), res);
  365. }
  366. scm_array_handle_release (&handle);
  367. return res;
  368. }
  369. static SCM_C_INLINE_KEYWORD SCM
  370. uvec_length (int type, SCM uvec)
  371. {
  372. scm_t_array_handle handle;
  373. size_t len;
  374. ssize_t inc;
  375. uvec_elements (type, uvec, &handle, &len, &inc);
  376. scm_array_handle_release (&handle);
  377. return scm_from_size_t (len);
  378. }
  379. static SCM_C_INLINE_KEYWORD SCM
  380. uvec_ref (int type, SCM uvec, SCM idx)
  381. {
  382. scm_t_array_handle handle;
  383. size_t i, len;
  384. ssize_t inc;
  385. const void *elts;
  386. SCM res;
  387. elts = uvec_elements (type, uvec, &handle, &len, &inc);
  388. if (type < 0)
  389. type = uvec_type (&handle);
  390. i = scm_to_unsigned_integer (idx, 0, len-1);
  391. res = uvec_fast_ref (type, elts, i*inc);
  392. scm_array_handle_release (&handle);
  393. return res;
  394. }
  395. static SCM_C_INLINE_KEYWORD SCM
  396. uvec_set_x (int type, SCM uvec, SCM idx, SCM val)
  397. {
  398. scm_t_array_handle handle;
  399. size_t i, len;
  400. ssize_t inc;
  401. void *elts;
  402. elts = uvec_writable_elements (type, uvec, &handle, &len, &inc);
  403. if (type < 0)
  404. type = uvec_type (&handle);
  405. i = scm_to_unsigned_integer (idx, 0, len-1);
  406. uvec_fast_set_x (type, elts, i*inc, val);
  407. scm_array_handle_release (&handle);
  408. return SCM_UNSPECIFIED;
  409. }
  410. static SCM_C_INLINE_KEYWORD SCM
  411. list_to_uvec (int type, SCM list)
  412. {
  413. SCM uvec;
  414. void *base;
  415. long idx;
  416. long len = scm_ilength (list);
  417. if (len < 0)
  418. scm_wrong_type_arg_msg (NULL, 0, list, "proper list");
  419. uvec = alloc_uvec (type, len);
  420. base = SCM_UVEC_BASE (uvec);
  421. idx = 0;
  422. while (scm_is_pair (list) && idx < len)
  423. {
  424. uvec_fast_set_x (type, base, idx, SCM_CAR (list));
  425. list = SCM_CDR (list);
  426. idx++;
  427. }
  428. return uvec;
  429. }
  430. static SCM
  431. coerce_to_uvec (int type, SCM obj)
  432. {
  433. if (is_uvec (type, obj))
  434. return obj;
  435. else if (scm_is_pair (obj))
  436. return list_to_uvec (type, obj);
  437. else if (scm_is_generalized_vector (obj))
  438. {
  439. scm_t_array_handle handle;
  440. size_t len = scm_c_generalized_vector_length (obj), i;
  441. SCM uvec = alloc_uvec (type, len);
  442. scm_array_get_handle (uvec, &handle);
  443. for (i = 0; i < len; i++)
  444. scm_array_handle_set (&handle, i,
  445. scm_c_generalized_vector_ref (obj, i));
  446. scm_array_handle_release (&handle);
  447. return uvec;
  448. }
  449. else
  450. scm_wrong_type_arg_msg (NULL, 0, obj, "list or generalized vector");
  451. }
  452. SCM_SYMBOL (scm_sym_a, "a");
  453. SCM_SYMBOL (scm_sym_b, "b");
  454. SCM
  455. scm_i_generalized_vector_type (SCM v)
  456. {
  457. if (scm_is_vector (v))
  458. return SCM_BOOL_T;
  459. else if (scm_is_string (v))
  460. return scm_sym_a;
  461. else if (scm_is_bitvector (v))
  462. return scm_sym_b;
  463. else if (scm_is_uniform_vector (v))
  464. return scm_from_locale_symbol (uvec_tags[SCM_UVEC_TYPE(v)]);
  465. else
  466. return SCM_BOOL_F;
  467. }
  468. int
  469. scm_is_uniform_vector (SCM obj)
  470. {
  471. if (SCM_IS_UVEC (obj))
  472. return 1;
  473. if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1)
  474. {
  475. SCM v = SCM_I_ARRAY_V (obj);
  476. return SCM_IS_UVEC (v);
  477. }
  478. return 0;
  479. }
  480. size_t
  481. scm_c_uniform_vector_length (SCM uvec)
  482. {
  483. /* scm_generalized_vector_get_handle will ultimately call us to get
  484. the length of uniform vectors, so we can't use uvec_elements for
  485. naked vectors.
  486. */
  487. if (SCM_IS_UVEC (uvec))
  488. return SCM_UVEC_LENGTH (uvec);
  489. else
  490. {
  491. scm_t_array_handle handle;
  492. size_t len;
  493. ssize_t inc;
  494. uvec_elements (-1, uvec, &handle, &len, &inc);
  495. scm_array_handle_release (&handle);
  496. return len;
  497. }
  498. }
  499. SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0,
  500. (SCM obj),
  501. "Return @code{#t} if @var{obj} is a uniform vector.")
  502. #define FUNC_NAME s_scm_uniform_vector_p
  503. {
  504. return scm_from_bool (scm_is_uniform_vector (obj));
  505. }
  506. #undef FUNC_NAME
  507. SCM
  508. scm_c_uniform_vector_ref (SCM v, size_t idx)
  509. {
  510. scm_t_array_handle handle;
  511. size_t len;
  512. ssize_t inc;
  513. SCM res;
  514. uvec_elements (-1, v, &handle, &len, &inc);
  515. if (idx >= len)
  516. scm_out_of_range (NULL, scm_from_size_t (idx));
  517. res = scm_array_handle_ref (&handle, idx*inc);
  518. scm_array_handle_release (&handle);
  519. return res;
  520. }
  521. SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0,
  522. (SCM v, SCM idx),
  523. "Return the element at index @var{idx} of the\n"
  524. "homogenous numeric vector @var{v}.")
  525. #define FUNC_NAME s_scm_uniform_vector_ref
  526. {
  527. #if SCM_ENABLE_DEPRECATED
  528. /* Support old argument convention.
  529. */
  530. if (scm_is_pair (idx))
  531. {
  532. scm_c_issue_deprecation_warning
  533. ("Using a list as the index to uniform-vector-ref is deprecated.");
  534. if (!scm_is_null (SCM_CDR (idx)))
  535. scm_wrong_num_args (NULL);
  536. idx = SCM_CAR (idx);
  537. }
  538. #endif
  539. return scm_c_uniform_vector_ref (v, scm_to_size_t (idx));
  540. }
  541. #undef FUNC_NAME
  542. void
  543. scm_c_uniform_vector_set_x (SCM v, size_t idx, SCM val)
  544. {
  545. scm_t_array_handle handle;
  546. size_t len;
  547. ssize_t inc;
  548. uvec_writable_elements (-1, v, &handle, &len, &inc);
  549. if (idx >= len)
  550. scm_out_of_range (NULL, scm_from_size_t (idx));
  551. scm_array_handle_set (&handle, idx*inc, val);
  552. scm_array_handle_release (&handle);
  553. }
  554. SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0,
  555. (SCM v, SCM idx, SCM val),
  556. "Set the element at index @var{idx} of the\n"
  557. "homogenous numeric vector @var{v} to @var{val}.")
  558. #define FUNC_NAME s_scm_uniform_vector_set_x
  559. {
  560. #if SCM_ENABLE_DEPRECATED
  561. /* Support old argument convention.
  562. */
  563. if (scm_is_pair (idx))
  564. {
  565. scm_c_issue_deprecation_warning
  566. ("Using a list as the index to uniform-vector-set! is deprecated.");
  567. if (!scm_is_null (SCM_CDR (idx)))
  568. scm_wrong_num_args (NULL);
  569. idx = SCM_CAR (idx);
  570. }
  571. #endif
  572. scm_c_uniform_vector_set_x (v, scm_to_size_t (idx), val);
  573. return SCM_UNSPECIFIED;
  574. }
  575. #undef FUNC_NAME
  576. SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0,
  577. (SCM uvec),
  578. "Convert the uniform numeric vector @var{uvec} to a list.")
  579. #define FUNC_NAME s_scm_uniform_vector_to_list
  580. {
  581. return uvec_to_list (-1, uvec);
  582. }
  583. #undef FUNC_NAME
  584. size_t
  585. scm_array_handle_uniform_element_size (scm_t_array_handle *h)
  586. {
  587. SCM vec = h->array;
  588. if (SCM_I_ARRAYP (vec))
  589. vec = SCM_I_ARRAY_V (vec);
  590. if (scm_is_uniform_vector (vec))
  591. return uvec_sizes[SCM_UVEC_TYPE(vec)];
  592. scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
  593. }
  594. #if SCM_ENABLE_DEPRECATED
  595. /* return the size of an element in a uniform array or 0 if type not
  596. found. */
  597. size_t
  598. scm_uniform_element_size (SCM obj)
  599. {
  600. scm_c_issue_deprecation_warning
  601. ("scm_uniform_element_size is deprecated. "
  602. "Use scm_array_handle_uniform_element_size instead.");
  603. if (SCM_IS_UVEC (obj))
  604. return uvec_sizes[SCM_UVEC_TYPE(obj)];
  605. else
  606. return 0;
  607. }
  608. #endif
  609. const void *
  610. scm_array_handle_uniform_elements (scm_t_array_handle *h)
  611. {
  612. return scm_array_handle_uniform_writable_elements (h);
  613. }
  614. void *
  615. scm_array_handle_uniform_writable_elements (scm_t_array_handle *h)
  616. {
  617. SCM vec = h->array;
  618. if (SCM_I_ARRAYP (vec))
  619. vec = SCM_I_ARRAY_V (vec);
  620. if (SCM_IS_UVEC (vec))
  621. {
  622. size_t size = uvec_sizes[SCM_UVEC_TYPE(vec)];
  623. char *elts = SCM_UVEC_BASE (vec);
  624. return (void *) (elts + size*h->base);
  625. }
  626. scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array");
  627. }
  628. const void *
  629. scm_uniform_vector_elements (SCM uvec,
  630. scm_t_array_handle *h,
  631. size_t *lenp, ssize_t *incp)
  632. {
  633. return scm_uniform_vector_writable_elements (uvec, h, lenp, incp);
  634. }
  635. void *
  636. scm_uniform_vector_writable_elements (SCM uvec,
  637. scm_t_array_handle *h,
  638. size_t *lenp, ssize_t *incp)
  639. {
  640. scm_generalized_vector_get_handle (uvec, h);
  641. if (lenp)
  642. {
  643. scm_t_array_dim *dim = scm_array_handle_dims (h);
  644. *lenp = dim->ubnd - dim->lbnd + 1;
  645. *incp = dim->inc;
  646. }
  647. return scm_array_handle_uniform_writable_elements (h);
  648. }
  649. SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0,
  650. (SCM v),
  651. "Return the number of elements in the uniform vector @var{v}.")
  652. #define FUNC_NAME s_scm_uniform_vector_length
  653. {
  654. return uvec_length (-1, v);
  655. }
  656. #undef FUNC_NAME
  657. SCM_DEFINE (scm_uniform_vector_read_x, "uniform-vector-read!", 1, 3, 0,
  658. (SCM uvec, SCM port_or_fd, SCM start, SCM end),
  659. "Fill the elements of @var{uvec} by reading\n"
  660. "raw bytes from @var{port-or-fdes}, using host byte order.\n\n"
  661. "The optional arguments @var{start} (inclusive) and @var{end}\n"
  662. "(exclusive) allow a specified region to be read,\n"
  663. "leaving the remainder of the vector unchanged.\n\n"
  664. "When @var{port-or-fdes} is a port, all specified elements\n"
  665. "of @var{uvec} are attempted to be read, potentially blocking\n"
  666. "while waiting formore input or end-of-file.\n"
  667. "When @var{port-or-fd} is an integer, a single call to\n"
  668. "read(2) is made.\n\n"
  669. "An error is signalled when the last element has only\n"
  670. "been partially filled before reaching end-of-file or in\n"
  671. "the single call to read(2).\n\n"
  672. "@code{uniform-vector-read!} returns the number of elements\n"
  673. "read.\n\n"
  674. "@var{port-or-fdes} may be omitted, in which case it defaults\n"
  675. "to the value returned by @code{(current-input-port)}.")
  676. #define FUNC_NAME s_scm_uniform_vector_read_x
  677. {
  678. scm_t_array_handle handle;
  679. size_t vlen, sz, ans;
  680. ssize_t inc;
  681. size_t cstart, cend;
  682. size_t remaining, off;
  683. char *base;
  684. if (SCM_UNBNDP (port_or_fd))
  685. port_or_fd = scm_current_input_port ();
  686. else
  687. SCM_ASSERT (scm_is_integer (port_or_fd)
  688. || (SCM_OPINPORTP (port_or_fd)),
  689. port_or_fd, SCM_ARG2, FUNC_NAME);
  690. if (!scm_is_uniform_vector (uvec))
  691. scm_wrong_type_arg_msg (NULL, 0, uvec, "uniform vector");
  692. base = scm_uniform_vector_writable_elements (uvec, &handle, &vlen, &inc);
  693. sz = scm_array_handle_uniform_element_size (&handle);
  694. if (inc != 1)
  695. {
  696. /* XXX - we should of course support non contiguous vectors. */
  697. scm_misc_error (NULL, "only contiguous vectors are supported: ~a",
  698. scm_list_1 (uvec));
  699. }
  700. cstart = 0;
  701. cend = vlen;
  702. if (!SCM_UNBNDP (start))
  703. {
  704. cstart = scm_to_unsigned_integer (start, 0, vlen);
  705. if (!SCM_UNBNDP (end))
  706. cend = scm_to_unsigned_integer (end, cstart, vlen);
  707. }
  708. remaining = (cend - cstart) * sz;
  709. off = cstart * sz;
  710. if (SCM_NIMP (port_or_fd))
  711. {
  712. ans = cend - cstart;
  713. remaining -= scm_c_read (port_or_fd, base + off, remaining);
  714. if (remaining % sz != 0)
  715. SCM_MISC_ERROR ("unexpected EOF", SCM_EOL);
  716. ans -= remaining / sz;
  717. }
  718. else /* file descriptor. */
  719. {
  720. int fd = scm_to_int (port_or_fd);
  721. int n;
  722. SCM_SYSCALL (n = read (fd, base + off, remaining));
  723. if (n == -1)
  724. SCM_SYSERROR;
  725. if (n % sz != 0)
  726. SCM_MISC_ERROR ("unexpected EOF", SCM_EOL);
  727. ans = n / sz;
  728. }
  729. scm_array_handle_release (&handle);
  730. return scm_from_size_t (ans);
  731. }
  732. #undef FUNC_NAME
  733. SCM_DEFINE (scm_uniform_vector_write, "uniform-vector-write", 1, 3, 0,
  734. (SCM uvec, SCM port_or_fd, SCM start, SCM end),
  735. "Write the elements of @var{uvec} as raw bytes to\n"
  736. "@var{port-or-fdes}, in the host byte order.\n\n"
  737. "The optional arguments @var{start} (inclusive)\n"
  738. "and @var{end} (exclusive) allow\n"
  739. "a specified region to be written.\n\n"
  740. "When @var{port-or-fdes} is a port, all specified elements\n"
  741. "of @var{uvec} are attempted to be written, potentially blocking\n"
  742. "while waiting for more room.\n"
  743. "When @var{port-or-fd} is an integer, a single call to\n"
  744. "write(2) is made.\n\n"
  745. "An error is signalled when the last element has only\n"
  746. "been partially written in the single call to write(2).\n\n"
  747. "The number of objects actually written is returned.\n"
  748. "@var{port-or-fdes} may be\n"
  749. "omitted, in which case it defaults to the value returned by\n"
  750. "@code{(current-output-port)}.")
  751. #define FUNC_NAME s_scm_uniform_vector_write
  752. {
  753. scm_t_array_handle handle;
  754. size_t vlen, sz, ans;
  755. ssize_t inc;
  756. size_t cstart, cend;
  757. size_t amount, off;
  758. const char *base;
  759. port_or_fd = SCM_COERCE_OUTPORT (port_or_fd);
  760. if (SCM_UNBNDP (port_or_fd))
  761. port_or_fd = scm_current_output_port ();
  762. else
  763. SCM_ASSERT (scm_is_integer (port_or_fd)
  764. || (SCM_OPOUTPORTP (port_or_fd)),
  765. port_or_fd, SCM_ARG2, FUNC_NAME);
  766. base = scm_uniform_vector_elements (uvec, &handle, &vlen, &inc);
  767. sz = scm_array_handle_uniform_element_size (&handle);
  768. if (inc != 1)
  769. {
  770. /* XXX - we should of course support non contiguous vectors. */
  771. scm_misc_error (NULL, "only contiguous vectors are supported: ~a",
  772. scm_list_1 (uvec));
  773. }
  774. cstart = 0;
  775. cend = vlen;
  776. if (!SCM_UNBNDP (start))
  777. {
  778. cstart = scm_to_unsigned_integer (start, 0, vlen);
  779. if (!SCM_UNBNDP (end))
  780. cend = scm_to_unsigned_integer (end, cstart, vlen);
  781. }
  782. amount = (cend - cstart) * sz;
  783. off = cstart * sz;
  784. if (SCM_NIMP (port_or_fd))
  785. {
  786. scm_lfwrite (base + off, amount, port_or_fd);
  787. ans = cend - cstart;
  788. }
  789. else /* file descriptor. */
  790. {
  791. int fd = scm_to_int (port_or_fd), n;
  792. SCM_SYSCALL (n = write (fd, base + off, amount));
  793. if (n == -1)
  794. SCM_SYSERROR;
  795. if (n % sz != 0)
  796. SCM_MISC_ERROR ("last element only written partially", SCM_EOL);
  797. ans = n / sz;
  798. }
  799. scm_array_handle_release (&handle);
  800. return scm_from_size_t (ans);
  801. }
  802. #undef FUNC_NAME
  803. /* ================================================================ */
  804. /* Exported procedures. */
  805. /* ================================================================ */
  806. #define TYPE SCM_UVEC_U8
  807. #define TAG u8
  808. #define CTYPE scm_t_uint8
  809. #include "libguile/srfi-4.i.c"
  810. #define TYPE SCM_UVEC_S8
  811. #define TAG s8
  812. #define CTYPE scm_t_int8
  813. #include "libguile/srfi-4.i.c"
  814. #define TYPE SCM_UVEC_U16
  815. #define TAG u16
  816. #define CTYPE scm_t_uint16
  817. #include "libguile/srfi-4.i.c"
  818. #define TYPE SCM_UVEC_S16
  819. #define TAG s16
  820. #define CTYPE scm_t_int16
  821. #include "libguile/srfi-4.i.c"
  822. #define TYPE SCM_UVEC_U32
  823. #define TAG u32
  824. #define CTYPE scm_t_uint32
  825. #include "libguile/srfi-4.i.c"
  826. #define TYPE SCM_UVEC_S32
  827. #define TAG s32
  828. #define CTYPE scm_t_int32
  829. #include "libguile/srfi-4.i.c"
  830. #define TYPE SCM_UVEC_U64
  831. #define TAG u64
  832. #define CTYPE scm_t_uint64
  833. #include "libguile/srfi-4.i.c"
  834. #define TYPE SCM_UVEC_S64
  835. #define TAG s64
  836. #define CTYPE scm_t_int64
  837. #include "libguile/srfi-4.i.c"
  838. #define TYPE SCM_UVEC_F32
  839. #define TAG f32
  840. #define CTYPE float
  841. #include "libguile/srfi-4.i.c"
  842. #define TYPE SCM_UVEC_F64
  843. #define TAG f64
  844. #define CTYPE double
  845. #include "libguile/srfi-4.i.c"
  846. #define TYPE SCM_UVEC_C32
  847. #define TAG c32
  848. #define CTYPE float
  849. #include "libguile/srfi-4.i.c"
  850. #define TYPE SCM_UVEC_C64
  851. #define TAG c64
  852. #define CTYPE double
  853. #include "libguile/srfi-4.i.c"
  854. static scm_i_t_array_ref uvec_reffers[12] = {
  855. u8ref, s8ref,
  856. u16ref, s16ref,
  857. u32ref, s32ref,
  858. u64ref, s64ref,
  859. f32ref, f64ref,
  860. c32ref, c64ref
  861. };
  862. static scm_i_t_array_set uvec_setters[12] = {
  863. u8set, s8set,
  864. u16set, s16set,
  865. u32set, s32set,
  866. u64set, s64set,
  867. f32set, f64set,
  868. c32set, c64set
  869. };
  870. scm_i_t_array_ref
  871. scm_i_uniform_vector_ref_proc (SCM uvec)
  872. {
  873. return uvec_reffers[SCM_UVEC_TYPE(uvec)];
  874. }
  875. scm_i_t_array_set
  876. scm_i_uniform_vector_set_proc (SCM uvec)
  877. {
  878. return uvec_setters[SCM_UVEC_TYPE(uvec)];
  879. }
  880. void
  881. scm_init_srfi_4 (void)
  882. {
  883. scm_tc16_uvec = scm_make_smob_type ("uvec", 0);
  884. scm_set_smob_equalp (scm_tc16_uvec, uvec_equalp);
  885. scm_set_smob_free (scm_tc16_uvec, uvec_free);
  886. scm_set_smob_print (scm_tc16_uvec, uvec_print);
  887. #include "libguile/srfi-4.x"
  888. }
  889. /* End of srfi-4.c. */