srfi-4.c 29 KB

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