srfi-4.c 30 KB

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