srfi-4.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /* srfi-4.c --- Homogeneous numeric vector datatypes.
  2. *
  3. * Copyright (C) 2001, 2004 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2, or (at
  8. * your option) any later version.
  9. *
  10. * This program 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. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; see the file COPYING. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301 USA
  19. *
  20. * As a special exception, the Free Software Foundation gives
  21. * permission for additional uses of the text contained in its release
  22. * of GUILE.
  23. *
  24. * The exception is that, if you link the GUILE library with other
  25. * files to produce an executable, this does not by itself cause the
  26. * resulting executable to be covered by the GNU General Public
  27. * License. Your use of that executable is in no way restricted on
  28. * account of linking the GUILE library code into it.
  29. *
  30. * This exception does not however invalidate any other reasons why
  31. * the executable file might be covered by the GNU General Public
  32. * License.
  33. *
  34. * This exception applies only to the code released by the Free
  35. * Software Foundation under the name GUILE. If you copy code from
  36. * other Free Software Foundation releases into a copy of GUILE, as
  37. * the General Public License permits, the exception does not apply to
  38. * the code that you add in this way. To avoid misleading anyone as
  39. * to the status of such modified files, you must delete this
  40. * exception notice from them.
  41. *
  42. * If you write modifications of your own for GUILE, it is your choice
  43. * whether to permit this exception to apply to your modifications.
  44. * If you do not wish that, delete this exception notice. */
  45. #include <libguile.h>
  46. #include "srfi-4.h"
  47. #include <string.h>
  48. #include <stdio.h>
  49. /* For brevity and maintainability, we define our own types for the
  50. various integer and floating point types. */
  51. typedef unsigned char int_u8;
  52. typedef signed char int_s8;
  53. typedef unsigned short int_u16;
  54. typedef signed short int_s16;
  55. typedef unsigned int int_u32;
  56. typedef signed int int_s32;
  57. #if SIZEOF_LONG == 8
  58. typedef unsigned long int_u64;
  59. typedef signed long int_s64;
  60. #define HAVE_UVEC_64 1
  61. #elif SIZEOF_LONG_LONG == 8
  62. typedef unsigned long long int_u64;
  63. typedef signed long long int_s64;
  64. #define HAVE_UVEC_64 1
  65. #else
  66. #define HAVE_UVEC_64 0
  67. #endif /* no 64-bit integer support */
  68. typedef float float_f32;
  69. typedef double float_f64;
  70. /* Smob type code for homogeneous numeric vectors. */
  71. int scm_tc16_uvec = 0;
  72. /* Accessor macros for the three components of a homogeneous numeric
  73. vector:
  74. - The type tag (one of the symbolic constants below).
  75. - The vector's length (counted in elements).
  76. - The address of the data area (holding the elements of the
  77. vector). */
  78. #define SCM_UVEC_TYPE(u) (SCM_CELL_WORD_1(u))
  79. #define SCM_UVEC_LENGTH(u) (SCM_CELL_WORD_2(u))
  80. #define SCM_UVEC_BASE(u) (SCM_CELL_OBJECT_3(u))
  81. /* Symbolic constants encoding the various types of homogeneous
  82. numeric vectors. */
  83. #define SCM_UVEC_U8 0
  84. #define SCM_UVEC_S8 1
  85. #define SCM_UVEC_U16 2
  86. #define SCM_UVEC_S16 3
  87. #define SCM_UVEC_U32 4
  88. #define SCM_UVEC_S32 5
  89. #define SCM_UVEC_U64 6
  90. #define SCM_UVEC_S64 7
  91. #define SCM_UVEC_F32 8
  92. #define SCM_UVEC_F64 9
  93. #define VALIDATE_UVEC(pos, obj, type) \
  94. do { \
  95. SCM_VALIDATE_SMOB (pos, obj, uvec); \
  96. if (SCM_UVEC_TYPE (obj) != type) \
  97. scm_wrong_type_arg (FUNC_NAME, 1, obj); \
  98. } while (0)
  99. #define RANGE_CHECK_AND_COPY_UVEC_INDEX(pos, uvec, i, cindex) \
  100. do { \
  101. cindex = scm_num2size (i, pos, FUNC_NAME); \
  102. if (cindex < 0 || cindex >= SCM_UVEC_LENGTH (uvec)) \
  103. scm_out_of_range_pos (FUNC_NAME, i, SCM_MAKINUM (pos)); \
  104. } while (0)
  105. /* This array maps type tags to the size of the elements. */
  106. static const int uvec_sizes[] = {1, 1, 2, 2, 4, 4, 8, 8, 4, 8};
  107. /* This array maps type tags to the bit shifts related to the sizes. */
  108. static const int uvec_shifts[] = {0, 0, 1, 1, 2, 2, 3, 3, 2, 3};
  109. #if HAVE_UVEC_64
  110. /* This is a modified version of scm_iint2str and should go away once
  111. we have a public scm_print_integer or similar. */
  112. static void
  113. print_int64 (int_s64 num, SCM port)
  114. {
  115. char num_buf[SCM_INTBUFLEN];
  116. char *p = num_buf;
  117. const int rad = 10;
  118. size_t num_chars = 1;
  119. size_t i;
  120. int_u64 n = (num < 0) ? -num : num;
  121. for (n /= rad; n > 0; n /= rad)
  122. num_chars++;
  123. i = num_chars;
  124. if (num < 0)
  125. {
  126. *p++ = '-';
  127. num_chars++;
  128. n = -num;
  129. }
  130. else
  131. n = num;
  132. while (i--)
  133. {
  134. int d = n % rad;
  135. n /= rad;
  136. p[i] = d + ((d < 10) ? '0' : 'a' - 10);
  137. }
  138. scm_lfwrite (num_buf, num_chars, port);
  139. }
  140. /* This is a modified version of scm_iint2str and should go away once
  141. we have a public scm_print_integer or similar. */
  142. static void
  143. print_uint64 (int_u64 num, SCM port)
  144. {
  145. char num_buf[SCM_INTBUFLEN];
  146. char *p = num_buf;
  147. const int rad = 10;
  148. size_t num_chars = 1;
  149. size_t i;
  150. int_u64 n = num;
  151. for (n /= rad; n > 0; n /= rad)
  152. num_chars++;
  153. i = num_chars;
  154. n = num;
  155. while (i--)
  156. {
  157. int d = n % rad;
  158. n /= rad;
  159. p[i] = d + ((d < 10) ? '0' : 'a' - 10);
  160. }
  161. scm_lfwrite (num_buf, num_chars, port);
  162. }
  163. #endif /* HAVE_UVEC_64 */
  164. static void
  165. print_uint32 (int_u32 num, SCM port)
  166. {
  167. char num_buf[SCM_INTBUFLEN];
  168. char *p = num_buf;
  169. const int rad = 10;
  170. size_t num_chars = 1;
  171. size_t i;
  172. int_u32 n = num;
  173. for (n /= rad; n > 0; n /= rad)
  174. num_chars++;
  175. i = num_chars;
  176. n = num;
  177. while (i--)
  178. {
  179. int d = n % rad;
  180. n /= rad;
  181. p[i] = d + ((d < 10) ? '0' : 'a' - 10);
  182. }
  183. scm_lfwrite (num_buf, num_chars, port);
  184. }
  185. /* ================================================================ */
  186. /* SMOB procedures. */
  187. /* ================================================================ */
  188. /* Smob print hook for homogeneous vectors. */
  189. static int
  190. uvec_print (SCM uvec, SCM port, scm_print_state *pstate SCM_UNUSED)
  191. {
  192. union {
  193. int_u8 *u8;
  194. int_s8 *s8;
  195. int_u16 *u16;
  196. int_s16 *s16;
  197. int_u32 *u32;
  198. int_s32 *s32;
  199. #if HAVE_UVEC_64
  200. int_u64 *u64;
  201. int_s64 *s64;
  202. #endif
  203. float_f32 *f32;
  204. float_f64 *f64;
  205. } np;
  206. size_t i = 0; /* since SCM_UVEC_LENGTH will return something this size. */
  207. const size_t uvlen = SCM_UVEC_LENGTH (uvec);
  208. char *tagstr;
  209. void *uptr = SCM_UVEC_BASE (uvec);
  210. switch (SCM_UVEC_TYPE (uvec))
  211. {
  212. case SCM_UVEC_U8: tagstr = "u8"; np.u8 = (int_u8 *) uptr; break;
  213. case SCM_UVEC_S8: tagstr = "s8"; np.s8 = (int_s8 *) uptr; break;
  214. case SCM_UVEC_U16: tagstr = "u16"; np.u16 = (int_u16 *) uptr; break;
  215. case SCM_UVEC_S16: tagstr = "s16"; np.s16 = (int_s16 *) uptr; break;
  216. case SCM_UVEC_U32: tagstr = "u32"; np.u32 = (int_u32 *) uptr; break;
  217. case SCM_UVEC_S32: tagstr = "s32"; np.s32 = (int_s32 *) uptr; break;
  218. #if HAVE_UVEC_64
  219. case SCM_UVEC_U64: tagstr = "u64"; np.u64 = (int_u64 *) uptr; break;
  220. case SCM_UVEC_S64: tagstr = "s64"; np.s64 = (int_s64 *) uptr; break;
  221. #endif
  222. case SCM_UVEC_F32: tagstr = "f32"; np.f32 = (float_f32 *) uptr; break;
  223. case SCM_UVEC_F64: tagstr = "f64"; np.f64 = (float_f64 *) uptr; break;
  224. default:
  225. abort (); /* Sanity check. */
  226. break;
  227. }
  228. scm_putc ('#', port);
  229. scm_puts (tagstr, port);
  230. scm_putc ('(', port);
  231. while (i < uvlen)
  232. {
  233. if (i != 0) scm_puts (" ", port);
  234. switch (SCM_UVEC_TYPE (uvec))
  235. {
  236. case SCM_UVEC_U8: scm_intprint (*np.u8, 10, port); np.u8++; break;
  237. case SCM_UVEC_S8: scm_intprint (*np.s8, 10, port); np.s8++; break;
  238. case SCM_UVEC_U16: scm_intprint (*np.u16, 10, port); np.u16++; break;
  239. case SCM_UVEC_S16: scm_intprint (*np.s16, 10, port); np.s16++; break;
  240. case SCM_UVEC_U32: print_uint32 (*np.u32, port); np.u32++; break;
  241. case SCM_UVEC_S32: scm_intprint (*np.s32, 10, port); np.s32++; break;
  242. #if HAVE_UVEC_64
  243. case SCM_UVEC_U64: print_uint64(*np.u64, port); np.u64++; break;
  244. case SCM_UVEC_S64: print_int64(*np.s64, port); np.s64++; break;
  245. #endif
  246. case SCM_UVEC_F32: scm_iprin1 (scm_make_real (*np.f32), port, pstate);
  247. np.f32++;
  248. break;
  249. case SCM_UVEC_F64: scm_iprin1 (scm_make_real (*np.f64), port, pstate);
  250. np.f64++;
  251. break;
  252. default:
  253. abort (); /* Sanity check. */
  254. break;
  255. }
  256. i++;
  257. }
  258. scm_remember_upto_here_1 (uvec);
  259. scm_puts (")", port);
  260. return 1;
  261. }
  262. static SCM
  263. uvec_equalp (SCM a, SCM b)
  264. {
  265. SCM result = SCM_BOOL_T;
  266. if(SCM_UVEC_TYPE (a) != SCM_UVEC_TYPE (b))
  267. result = SCM_BOOL_F;
  268. else if(SCM_UVEC_LENGTH (a) != SCM_UVEC_LENGTH (b))
  269. result = SCM_BOOL_F;
  270. else if(memcmp(SCM_UVEC_BASE (a), SCM_UVEC_BASE (b),
  271. SCM_UVEC_LENGTH (a) * uvec_sizes[SCM_UVEC_TYPE(a)]) != 0)
  272. result = SCM_BOOL_F;
  273. scm_remember_upto_here_2 (a, b);
  274. return result;
  275. }
  276. static size_t
  277. uvec_free (SCM uvec)
  278. {
  279. scm_must_free (SCM_UVEC_BASE (uvec));
  280. return SCM_UVEC_LENGTH (uvec) * uvec_sizes[SCM_UVEC_TYPE (uvec)];
  281. }
  282. /* ================================================================ */
  283. /* Utility procedures. */
  284. /* ================================================================ */
  285. /* Create a new, uninitialized homogeneous numeric vector of type TYPE
  286. with space for LEN elements. */
  287. inline static SCM
  288. make_uvec (const int pos, const char * func_name, int type, size_t len)
  289. {
  290. if (len > (((size_t)-1) >> uvec_shifts[type]))
  291. {
  292. if(pos < 0)
  293. scm_out_of_range (func_name, scm_size2num (len));
  294. else
  295. scm_out_of_range_pos (func_name, scm_size2num (len), SCM_MAKINUM (pos));
  296. }
  297. else
  298. {
  299. void *p = scm_must_malloc (len * uvec_sizes[type], func_name);
  300. SCM_RETURN_NEWSMOB3 (scm_tc16_uvec, type, len, p);
  301. }
  302. }
  303. inline static SCM
  304. uvec_length (SCM uvec, const int required_type, const char *FUNC_NAME)
  305. {
  306. SCM result;
  307. VALIDATE_UVEC (1, uvec, required_type);
  308. result = scm_size2num (SCM_UVEC_LENGTH (uvec));
  309. scm_remember_upto_here_1 (uvec);
  310. return result;
  311. }
  312. /* ================================================================ */
  313. /* U8 procedures. */
  314. /* ================================================================ */
  315. SCM_DEFINE (scm_u8vector_p, "u8vector?", 1, 0, 0,
  316. (SCM obj),
  317. "Return @code{#t} if @var{obj} is a vector of type u8,\n"
  318. "@code{#f} otherwise.")
  319. #define FUNC_NAME s_scm_u8vector_p
  320. {
  321. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj)
  322. && (SCM_UVEC_TYPE (obj) == SCM_UVEC_U8));
  323. scm_remember_upto_here_1 (obj);
  324. return result;
  325. }
  326. #undef FUNC_NAME
  327. SCM_DEFINE (scm_make_u8vector, "make-u8vector", 1, 1, 0,
  328. (SCM n, SCM fill),
  329. "Create a newly allocated homogeneous numeric vector which can\n"
  330. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  331. "initialize the elements, otherwise the contents of the vector\n"
  332. "is unspecified.")
  333. #define FUNC_NAME s_scm_make_u8vector
  334. {
  335. SCM uvec;
  336. int_u8 f;
  337. const size_t count = scm_num2size (n, 1, s_scm_make_u8vector);
  338. uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_U8, count);
  339. if (SCM_UNBNDP (fill))
  340. f = 0;
  341. else
  342. {
  343. unsigned int s = scm_num2uint (fill, 2, FUNC_NAME);
  344. f = s;
  345. if ((unsigned int) f != s)
  346. scm_out_of_range_pos (FUNC_NAME, fill, SCM_MAKINUM (2));
  347. }
  348. memset(SCM_UVEC_BASE (uvec), f, count);
  349. return uvec;
  350. }
  351. #undef FUNC_NAME
  352. SCM_DEFINE (scm_u8vector, "u8vector", 0, 0, 1,
  353. (SCM l),
  354. "Create a newly allocated homogeneous numeric vector containing\n"
  355. "all argument values.")
  356. #define FUNC_NAME s_scm_u8vector
  357. {
  358. SCM_VALIDATE_REST_ARGUMENT (l);
  359. return scm_list_to_u8vector (l);
  360. }
  361. #undef FUNC_NAME
  362. SCM_DEFINE (scm_u8vector_length, "u8vector-length", 1, 0, 0,
  363. (SCM uvec),
  364. "Return the number of elements in the homogeneous numeric vector\n"
  365. "@var{uvec}.")
  366. #define FUNC_NAME s_scm_u8vector_length
  367. {
  368. return uvec_length (uvec, SCM_UVEC_U8, FUNC_NAME);
  369. }
  370. #undef FUNC_NAME
  371. SCM_DEFINE (scm_u8vector_ref, "u8vector-ref", 2, 0, 0,
  372. (SCM uvec, SCM index),
  373. "Return the element at @var{index} in the homogeneous numeric\n"
  374. "vector @var{uvec}.")
  375. #define FUNC_NAME s_scm_u8vector_ref
  376. {
  377. SCM result;
  378. size_t idx;
  379. VALIDATE_UVEC (1, uvec, SCM_UVEC_U8);
  380. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  381. result = scm_short2num (((int_u8 *) SCM_UVEC_BASE (uvec))[idx]);
  382. scm_remember_upto_here_1 (uvec);
  383. return result;
  384. }
  385. #undef FUNC_NAME
  386. SCM_DEFINE (scm_u8vector_set_x, "u8vector-set!", 3, 0, 0,
  387. (SCM uvec, SCM index, SCM value),
  388. "Set the element at @var{index} in the homogeneous numeric\n"
  389. "vector @var{uvec} to @var{value}. The return value is not\n"
  390. "specified.")
  391. #define FUNC_NAME s_scm_u8vector_set_x
  392. {
  393. size_t idx;
  394. int_u8 f;
  395. unsigned int s;
  396. VALIDATE_UVEC (1, uvec, SCM_UVEC_U8);
  397. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  398. s = scm_num2uint (value, 3, FUNC_NAME);
  399. f = s;
  400. if ((unsigned int) f != s)
  401. scm_out_of_range_pos (FUNC_NAME, value, SCM_MAKINUM (3));
  402. ((int_u8 *) SCM_UVEC_BASE (uvec))[idx] = f;
  403. scm_remember_upto_here_1 (uvec);
  404. return SCM_UNSPECIFIED;
  405. }
  406. #undef FUNC_NAME
  407. SCM_DEFINE (scm_u8vector_to_list, "u8vector->list", 1, 0, 0,
  408. (SCM uvec),
  409. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  410. #define FUNC_NAME s_scm_u8vector_to_list
  411. {
  412. size_t idx;
  413. int_u8 * p;
  414. SCM res = SCM_EOL;
  415. VALIDATE_UVEC (1, uvec, SCM_UVEC_U8);
  416. idx = SCM_UVEC_LENGTH (uvec);
  417. p = (int_u8 *) SCM_UVEC_BASE (uvec) + idx;
  418. while (idx-- > 0)
  419. {
  420. p--;
  421. res = scm_cons (SCM_MAKINUM (*p), res);
  422. }
  423. scm_remember_upto_here_1 (uvec);
  424. return res;
  425. }
  426. #undef FUNC_NAME
  427. SCM_DEFINE (scm_list_to_u8vector, "list->u8vector", 1, 0, 0,
  428. (SCM l),
  429. "Convert the list @var{l}, which must only contain unsigned\n"
  430. "8-bit values, to a numeric homogeneous vector.")
  431. #define FUNC_NAME s_scm_list_to_u8vector
  432. {
  433. SCM uvec;
  434. SCM tmp;
  435. int_u8 * p;
  436. long n; /* to match COPYLEN */
  437. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  438. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_U8, n);
  439. p = (int_u8 *) SCM_UVEC_BASE (uvec);
  440. tmp = l;
  441. while (SCM_CONSP (tmp))
  442. {
  443. int_u8 f;
  444. unsigned int s = scm_num2uint (SCM_CAR (tmp), 2, FUNC_NAME);
  445. f = s;
  446. if ((unsigned int) f != s)
  447. scm_out_of_range (FUNC_NAME, SCM_CAR (tmp));
  448. *p++ = f;
  449. tmp = SCM_CDR (tmp);
  450. }
  451. return uvec;
  452. }
  453. #undef FUNC_NAME
  454. /* ================================================================ */
  455. /* S8 procedures. */
  456. /* ================================================================ */
  457. SCM_DEFINE (scm_s8vector_p, "s8vector?", 1, 0, 0,
  458. (SCM obj),
  459. "Return @code{#t} if @var{obj} is a vector of type s8,\n"
  460. "@code{#f} otherwise.")
  461. #define FUNC_NAME s_scm_s8vector_p
  462. {
  463. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj)
  464. && (SCM_UVEC_TYPE (obj) == SCM_UVEC_S8));
  465. scm_remember_upto_here_1 (obj);
  466. return result;
  467. }
  468. #undef FUNC_NAME
  469. SCM_DEFINE (scm_make_s8vector, "make-s8vector", 1, 1, 0,
  470. (SCM n, SCM fill),
  471. "Create a newly allocated homogeneous numeric vector which can\n"
  472. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  473. "initialize the elements, otherwise the contents of the vector\n"
  474. "is unspecified.")
  475. #define FUNC_NAME s_scm_make_s8vector
  476. {
  477. SCM uvec;
  478. int_s8 f;
  479. size_t count = scm_num2size (n, 1, s_scm_make_s8vector);
  480. uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_S8, count);
  481. if (SCM_UNBNDP (fill))
  482. f = 0;
  483. else
  484. {
  485. int s = scm_num2int (fill, 2, FUNC_NAME);
  486. f = s;
  487. if ((signed int) f != s)
  488. scm_out_of_range_pos (FUNC_NAME, fill, SCM_MAKINUM (2));
  489. }
  490. memset(SCM_UVEC_BASE (uvec), f, count);
  491. return uvec;
  492. }
  493. #undef FUNC_NAME
  494. SCM_DEFINE (scm_s8vector, "s8vector", 0, 0, 1,
  495. (SCM l),
  496. "Create a newly allocated homogeneous numeric vector containing\n"
  497. "all argument values.")
  498. #define FUNC_NAME s_scm_s8vector
  499. {
  500. SCM_VALIDATE_REST_ARGUMENT (l);
  501. return scm_list_to_s8vector (l);
  502. }
  503. #undef FUNC_NAME
  504. SCM_DEFINE (scm_s8vector_length, "s8vector-length", 1, 0, 0,
  505. (SCM uvec),
  506. "Return the number of elements in the homogeneous numeric vector\n"
  507. "@var{uvec}.")
  508. #define FUNC_NAME s_scm_s8vector_length
  509. {
  510. return uvec_length (uvec, SCM_UVEC_S8, FUNC_NAME);
  511. }
  512. #undef FUNC_NAME
  513. SCM_DEFINE (scm_s8vector_ref, "s8vector-ref", 2, 0, 0,
  514. (SCM uvec, SCM index),
  515. "Return the element at @var{index} in the homogeneous numeric\n"
  516. "vector @var{uvec}.")
  517. #define FUNC_NAME s_scm_s8vector_ref
  518. {
  519. SCM result;
  520. size_t idx;
  521. VALIDATE_UVEC (1, uvec, SCM_UVEC_S8);
  522. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  523. result = scm_short2num (((int_s8 *) SCM_UVEC_BASE (uvec))[idx]);
  524. scm_remember_upto_here_1 (uvec);
  525. return result;
  526. }
  527. #undef FUNC_NAME
  528. SCM_DEFINE (scm_s8vector_set_x, "s8vector-set!", 3, 0, 0,
  529. (SCM uvec, SCM index, SCM value),
  530. "Set the element at @var{index} in the homogeneous numeric\n"
  531. "vector @var{uvec} to @var{value}. The return value is not\n"
  532. "specified.")
  533. #define FUNC_NAME s_scm_s8vector_set_x
  534. {
  535. size_t idx;
  536. int_s8 f;
  537. signed int s;
  538. VALIDATE_UVEC (1, uvec, SCM_UVEC_S8);
  539. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  540. s = scm_num2int (value, 3, FUNC_NAME);
  541. f = s;
  542. if ((signed int) f != s)
  543. scm_out_of_range_pos (FUNC_NAME, value, SCM_MAKINUM (3));
  544. ((int_s8 *) SCM_UVEC_BASE (uvec))[idx] = f;
  545. scm_remember_upto_here_1 (uvec);
  546. return SCM_UNSPECIFIED;
  547. }
  548. #undef FUNC_NAME
  549. SCM_DEFINE (scm_s8vector_to_list, "s8vector->list", 1, 0, 0,
  550. (SCM uvec),
  551. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  552. #define FUNC_NAME s_scm_s8vector_to_list
  553. {
  554. size_t idx;
  555. int_s8 * p;
  556. SCM res = SCM_EOL;
  557. VALIDATE_UVEC (1, uvec, SCM_UVEC_S8);
  558. idx = SCM_UVEC_LENGTH (uvec);
  559. p = (int_s8 *) SCM_UVEC_BASE (uvec) + idx;
  560. while (idx-- > 0)
  561. {
  562. p--;
  563. res = scm_cons (SCM_MAKINUM (*p), res);
  564. }
  565. scm_remember_upto_here_1 (uvec);
  566. return res;
  567. }
  568. #undef FUNC_NAME
  569. SCM_DEFINE (scm_list_to_s8vector, "list->s8vector", 1, 0, 0,
  570. (SCM l),
  571. "Convert the list @var{l}, which must only contain signed\n"
  572. "8-bit values, to a numeric homogeneous vector.")
  573. #define FUNC_NAME s_scm_list_to_s8vector
  574. {
  575. SCM uvec;
  576. SCM tmp;
  577. int_s8 * p;
  578. long n; /* to match COPYLEN */
  579. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  580. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_S8, n);
  581. p = (int_s8 *) SCM_UVEC_BASE (uvec);
  582. tmp = l;
  583. while (SCM_CONSP (tmp))
  584. {
  585. int_s8 f;
  586. signed int s;
  587. s = scm_num2int (SCM_CAR (tmp), 2, FUNC_NAME);
  588. f = s;
  589. if ((signed int) f != s)
  590. scm_out_of_range (FUNC_NAME, SCM_CAR (tmp));
  591. *p++ = f;
  592. tmp = SCM_CDR (tmp);
  593. }
  594. return uvec;
  595. }
  596. #undef FUNC_NAME
  597. /* ================================================================ */
  598. /* U16 procedures. */
  599. /* ================================================================ */
  600. SCM_DEFINE (scm_u16vector_p, "u16vector?", 1, 0, 0,
  601. (SCM obj),
  602. "Return @code{#t} if @var{obj} is a vector of type u16,\n"
  603. "@code{#f} otherwise.")
  604. #define FUNC_NAME s_scm_u16vector_p
  605. {
  606. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj)
  607. && SCM_UVEC_TYPE (obj) == SCM_UVEC_U16);
  608. scm_remember_upto_here_1 (obj);
  609. return result;
  610. }
  611. #undef FUNC_NAME
  612. SCM_DEFINE (scm_make_u16vector, "make-u16vector", 1, 1, 0,
  613. (SCM n, SCM fill),
  614. "Create a newly allocated homogeneous numeric vector which can\n"
  615. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  616. "initialize the elements, otherwise the contents of the vector\n"
  617. "is unspecified.")
  618. #define FUNC_NAME s_scm_make_u16vector
  619. {
  620. size_t count = scm_num2size (n, 1, s_scm_make_u16vector);
  621. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_U16, count);
  622. if (SCM_UNBNDP (fill))
  623. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_u16));
  624. else
  625. {
  626. /* if we had SIZEOF_SHORT we could be more efficient here */
  627. int_u16 * p;
  628. const unsigned int f = scm_num2uint (fill, 2, FUNC_NAME);
  629. #if SIZEOF_INT > 2
  630. SCM_ASSERT_RANGE (2, fill, (f <= (int_u16) 65535));
  631. #endif
  632. p = (int_u16 *) SCM_UVEC_BASE (uvec);
  633. while (count-- > 0)
  634. *p++ = f;
  635. }
  636. return uvec;
  637. }
  638. #undef FUNC_NAME
  639. SCM_DEFINE (scm_u16vector, "u16vector", 0, 0, 1,
  640. (SCM l),
  641. "Create a newly allocated homogeneous numeric vector containing\n"
  642. "all argument values.")
  643. #define FUNC_NAME s_scm_u16vector
  644. {
  645. SCM_VALIDATE_REST_ARGUMENT (l);
  646. return scm_list_to_u16vector (l);
  647. }
  648. #undef FUNC_NAME
  649. SCM_DEFINE (scm_u16vector_length, "u16vector-length", 1, 0, 0,
  650. (SCM uvec),
  651. "Return the number of elements in the homogeneous numeric vector\n"
  652. "@var{uvec}.")
  653. #define FUNC_NAME s_scm_u16vector_length
  654. {
  655. return uvec_length (uvec, SCM_UVEC_U16, FUNC_NAME);
  656. }
  657. #undef FUNC_NAME
  658. SCM_DEFINE (scm_u16vector_ref, "u16vector-ref", 2, 0, 0,
  659. (SCM uvec, SCM index),
  660. "Return the element at @var{index} in the homogeneous numeric\n"
  661. "vector @var{uvec}.")
  662. #define FUNC_NAME s_scm_u16vector_ref
  663. {
  664. SCM result;
  665. size_t idx;
  666. VALIDATE_UVEC (1, uvec, SCM_UVEC_U16);
  667. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  668. result = scm_ushort2num (((int_u16 *) SCM_UVEC_BASE (uvec))[idx]);
  669. scm_remember_upto_here_1 (result);
  670. return result;
  671. }
  672. #undef FUNC_NAME
  673. SCM_DEFINE (scm_u16vector_set_x, "u16vector-set!", 3, 0, 0,
  674. (SCM uvec, SCM index, SCM value),
  675. "Set the element at @var{index} in the homogeneous numeric\n"
  676. "vector @var{uvec} to @var{value}. The return value is not\n"
  677. "specified.")
  678. #define FUNC_NAME s_scm_u16vector_set_x
  679. {
  680. size_t idx;
  681. unsigned int f;
  682. VALIDATE_UVEC (1, uvec, SCM_UVEC_U16);
  683. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  684. /* if we had SIZEOF_SHORT we could be more efficient here */
  685. f = scm_num2uint (value, 3, FUNC_NAME);
  686. #if SIZEOF_INT > 2
  687. SCM_ASSERT_RANGE (2, value, (f <= (int_u16) 65535));
  688. #endif
  689. ((int_u16 *) SCM_UVEC_BASE (uvec))[idx] = (int_u16) f;
  690. scm_remember_upto_here_1 (uvec);
  691. return SCM_UNSPECIFIED;
  692. }
  693. #undef FUNC_NAME
  694. SCM_DEFINE (scm_u16vector_to_list, "u16vector->list", 1, 0, 0,
  695. (SCM uvec),
  696. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  697. #define FUNC_NAME s_scm_u16vector_to_list
  698. {
  699. size_t idx;
  700. int_u16 * p;
  701. SCM res = SCM_EOL;
  702. VALIDATE_UVEC (1, uvec, SCM_UVEC_U16);
  703. idx = SCM_UVEC_LENGTH (uvec);
  704. p = (int_u16 *) SCM_UVEC_BASE (uvec) + idx;
  705. while (idx-- > 0)
  706. {
  707. p--;
  708. res = scm_cons (SCM_MAKINUM (*p), res);
  709. }
  710. scm_remember_upto_here_1 (uvec);
  711. return res;
  712. }
  713. #undef FUNC_NAME
  714. SCM_DEFINE (scm_list_to_u16vector, "list->u16vector", 1, 0, 0,
  715. (SCM l),
  716. "Convert the list @var{l}, which must only contain unsigned\n"
  717. "16-bit values, to a numeric homogeneous vector.")
  718. #define FUNC_NAME s_scm_list_to_u16vector
  719. {
  720. SCM uvec;
  721. int_u16 * p;
  722. long n; /* to match COPYLEN */
  723. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  724. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_U16, n);
  725. p = (int_u16 *) SCM_UVEC_BASE (uvec);
  726. while (SCM_CONSP (l))
  727. {
  728. int_u16 f = scm_num2ushort (SCM_CAR (l), 2, FUNC_NAME);
  729. *p++ = f;
  730. l = SCM_CDR (l);
  731. }
  732. return uvec;
  733. }
  734. #undef FUNC_NAME
  735. /* ================================================================ */
  736. /* S16 procedures. */
  737. /* ================================================================ */
  738. SCM_DEFINE (scm_s16vector_p, "s16vector?", 1, 0, 0,
  739. (SCM obj),
  740. "Return @code{#t} if @var{obj} is a vector of type s16,\n"
  741. "@code{#f} otherwise.")
  742. #define FUNC_NAME s_scm_s16vector_p
  743. {
  744. const SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj)
  745. && SCM_UVEC_TYPE (obj) == SCM_UVEC_S16);
  746. scm_remember_upto_here_1 (obj);
  747. return result;
  748. }
  749. #undef FUNC_NAME
  750. SCM_DEFINE (scm_make_s16vector, "make-s16vector", 1, 1, 0,
  751. (SCM n, SCM fill),
  752. "Create a newly allocated homogeneous numeric vector which can\n"
  753. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  754. "initialize the elements, otherwise the contents of the vector\n"
  755. "is unspecified.")
  756. #define FUNC_NAME s_scm_make_s16vector
  757. {
  758. SCM uvec;
  759. size_t count = scm_num2size (n, 1, s_scm_make_s16vector);
  760. uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_S16, count);
  761. if (SCM_UNBNDP (fill))
  762. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_s16));
  763. else
  764. {
  765. /* if we had SIZEOF_SHORT we could be more efficient here */
  766. int_s16 * p;
  767. int f = scm_num2int (fill, 2, FUNC_NAME);
  768. #if SIZEOF_INT > 2
  769. SCM_ASSERT_RANGE (2, fill,
  770. (f >= (int_s16) -32768) && (f <= (int_s16) 32767));
  771. #endif
  772. p = (int_s16 *) SCM_UVEC_BASE (uvec);
  773. while (count-- > 0)
  774. *p++ = f;
  775. }
  776. return uvec;
  777. }
  778. #undef FUNC_NAME
  779. SCM_DEFINE (scm_s16vector, "s16vector", 0, 0, 1,
  780. (SCM l),
  781. "Create a newly allocated homogeneous numeric vector containing\n"
  782. "all argument values.")
  783. #define FUNC_NAME s_scm_s16vector
  784. {
  785. SCM_VALIDATE_REST_ARGUMENT (l);
  786. return scm_list_to_s16vector (l);
  787. }
  788. #undef FUNC_NAME
  789. SCM_DEFINE (scm_s16vector_length, "s16vector-length", 1, 0, 0,
  790. (SCM uvec),
  791. "Return the number of elements in the homogeneous numeric vector\n"
  792. "@var{uvec}.")
  793. #define FUNC_NAME s_scm_s16vector_length
  794. {
  795. return uvec_length (uvec, SCM_UVEC_S16, FUNC_NAME);
  796. }
  797. #undef FUNC_NAME
  798. SCM_DEFINE (scm_s16vector_ref, "s16vector-ref", 2, 0, 0,
  799. (SCM uvec, SCM index),
  800. "Return the element at @var{index} in the homogeneous numeric\n"
  801. "vector @var{uvec}.")
  802. #define FUNC_NAME s_scm_s16vector_ref
  803. {
  804. SCM result;
  805. size_t idx;
  806. VALIDATE_UVEC (1, uvec, SCM_UVEC_S16);
  807. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  808. result = scm_short2num (((int_s16 *) SCM_UVEC_BASE (uvec))[idx]);
  809. scm_remember_upto_here_1 (uvec);
  810. return result;
  811. }
  812. #undef FUNC_NAME
  813. SCM_DEFINE (scm_s16vector_set_x, "s16vector-set!", 3, 0, 0,
  814. (SCM uvec, SCM index, SCM value),
  815. "Set the element at @var{index} in the homogeneous numeric\n"
  816. "vector @var{uvec} to @var{value}. The return value is not\n"
  817. "specified.")
  818. #define FUNC_NAME s_scm_s16vector_set_x
  819. {
  820. size_t idx;
  821. int f;
  822. VALIDATE_UVEC (1, uvec, SCM_UVEC_S16);
  823. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  824. /* if we had SIZEOF_SHORT we could be more efficient here */
  825. f = scm_num2int (value, 3, FUNC_NAME);
  826. #if SIZEOF_INT > 2
  827. SCM_ASSERT_RANGE (2, value,
  828. (f >= (int_s16) -32768) && (f <= (int_s16) 32767));
  829. #endif
  830. ((int_s16 *) SCM_UVEC_BASE (uvec))[idx] = (int_s16) f;
  831. scm_remember_upto_here_1 (uvec);
  832. return SCM_UNSPECIFIED;
  833. }
  834. #undef FUNC_NAME
  835. SCM_DEFINE (scm_s16vector_to_list, "s16vector->list", 1, 0, 0,
  836. (SCM uvec),
  837. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  838. #define FUNC_NAME s_scm_s16vector_to_list
  839. {
  840. size_t idx;
  841. int_s16 * p;
  842. SCM res = SCM_EOL;
  843. VALIDATE_UVEC (1, uvec, SCM_UVEC_S16);
  844. idx = SCM_UVEC_LENGTH (uvec);
  845. p = (int_s16 *) SCM_UVEC_BASE (uvec) + idx;
  846. while (idx-- > 0)
  847. {
  848. p--;
  849. res = scm_cons (SCM_MAKINUM (*p), res);
  850. }
  851. scm_remember_upto_here_1 (uvec);
  852. return res;
  853. }
  854. #undef FUNC_NAME
  855. SCM_DEFINE (scm_list_to_s16vector, "list->s16vector", 1, 0, 0,
  856. (SCM l),
  857. "Convert the list @var{l}, which must only contain signed\n"
  858. "16-bit values, to a numeric homogeneous vector.")
  859. #define FUNC_NAME s_scm_list_to_s16vector
  860. {
  861. SCM uvec;
  862. SCM tmp;
  863. int_s16 * p;
  864. long n; /* to match COPYLEN */
  865. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  866. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_S16, n);
  867. p = (int_s16 *) SCM_UVEC_BASE (uvec);
  868. tmp = l;
  869. while (SCM_CONSP (tmp))
  870. {
  871. int_s16 f = scm_num2short (SCM_CAR (tmp), 2, FUNC_NAME);
  872. *p++ = f;
  873. tmp = SCM_CDR (tmp);
  874. }
  875. return uvec;
  876. }
  877. #undef FUNC_NAME
  878. /* ================================================================ */
  879. /* U32 procedures. */
  880. /* ================================================================ */
  881. SCM_DEFINE (scm_u32vector_p, "u32vector?", 1, 0, 0,
  882. (SCM obj),
  883. "Return @code{#t} if @var{obj} is a vector of type u32,\n"
  884. "@code{#f} otherwise.")
  885. #define FUNC_NAME s_scm_u32vector_p
  886. {
  887. const SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj)
  888. && SCM_UVEC_TYPE (obj) == SCM_UVEC_U32);
  889. scm_remember_upto_here_1 (obj);
  890. return result;
  891. }
  892. #undef FUNC_NAME
  893. SCM_DEFINE (scm_make_u32vector, "make-u32vector", 1, 1, 0,
  894. (SCM n, SCM fill),
  895. "Create a newly allocated homogeneous numeric vector which can\n"
  896. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  897. "initialize the elements, otherwise the contents of the vector\n"
  898. "is unspecified.")
  899. #define FUNC_NAME s_scm_make_u32vector
  900. {
  901. size_t count = scm_num2size (n, 1, s_scm_make_u32vector);
  902. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_U32, count);
  903. if (SCM_UNBNDP (fill))
  904. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_u32));
  905. else
  906. {
  907. int_u32 * p;
  908. const unsigned long f = scm_num2ulong (fill, 2, FUNC_NAME);
  909. #if SIZEOF_LONG > 4
  910. SCM_ASSERT_RANGE (2, fill, (f <= (int_u32) 0xFFFFFFFFUL));
  911. #endif
  912. p = (int_u32 *) SCM_UVEC_BASE (uvec);
  913. while (count-- > 0)
  914. *p++ = f;
  915. }
  916. return uvec;
  917. }
  918. #undef FUNC_NAME
  919. SCM_DEFINE (scm_u32vector, "u32vector", 0, 0, 1,
  920. (SCM l),
  921. "Create a newly allocated homogeneous numeric vector containing\n"
  922. "all argument values.")
  923. #define FUNC_NAME s_scm_u32vector
  924. {
  925. SCM_VALIDATE_REST_ARGUMENT (l);
  926. return scm_list_to_u32vector (l);
  927. }
  928. #undef FUNC_NAME
  929. SCM_DEFINE (scm_u32vector_length, "u32vector-length", 1, 0, 0,
  930. (SCM uvec),
  931. "Return the number of elements in the homogeneous numeric vector\n"
  932. "@var{uvec}.")
  933. #define FUNC_NAME s_scm_u32vector_length
  934. {
  935. return uvec_length (uvec, SCM_UVEC_U32, FUNC_NAME);
  936. }
  937. #undef FUNC_NAME
  938. SCM_DEFINE (scm_u32vector_ref, "u32vector-ref", 2, 0, 0,
  939. (SCM uvec, SCM index),
  940. "Return the element at @var{index} in the homogeneous numeric\n"
  941. "vector @var{uvec}.")
  942. #define FUNC_NAME s_scm_u32vector_ref
  943. {
  944. SCM result;
  945. size_t idx;
  946. VALIDATE_UVEC (1, uvec, SCM_UVEC_U32);
  947. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  948. result = scm_ulong2num (((int_u32 *) SCM_UVEC_BASE (uvec))[idx]);
  949. scm_remember_upto_here_1 (uvec);
  950. return result;
  951. }
  952. #undef FUNC_NAME
  953. SCM_DEFINE (scm_u32vector_set_x, "u32vector-set!", 3, 0, 0,
  954. (SCM uvec, SCM index, SCM value),
  955. "Set the element at @var{index} in the homogeneous numeric\n"
  956. "vector @var{uvec} to @var{value}. The return value is not\n"
  957. "specified.")
  958. #define FUNC_NAME s_scm_u32vector_set_x
  959. {
  960. size_t idx;
  961. unsigned long f;
  962. VALIDATE_UVEC (1, uvec, SCM_UVEC_U32);
  963. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  964. f = scm_num2ulong (value, 3, FUNC_NAME);
  965. #if SIZEOF_LONG > 4
  966. SCM_ASSERT_RANGE (2, value, (f <= (int_u32) 0xFFFFFFFFUL));
  967. #endif
  968. ((int_u32 *) SCM_UVEC_BASE (uvec))[idx] = (int_u32) f;
  969. scm_remember_upto_here_1 (uvec);
  970. return SCM_UNSPECIFIED;
  971. }
  972. #undef FUNC_NAME
  973. SCM_DEFINE (scm_u32vector_to_list, "u32vector->list", 1, 0, 0,
  974. (SCM uvec),
  975. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  976. #define FUNC_NAME s_scm_u32vector_to_list
  977. {
  978. size_t idx;
  979. int_u32 * p;
  980. SCM res = SCM_EOL;
  981. VALIDATE_UVEC (1, uvec, SCM_UVEC_U32);
  982. idx = SCM_UVEC_LENGTH (uvec);
  983. p = (int_u32 *) SCM_UVEC_BASE (uvec) + idx;
  984. while (idx-- > 0)
  985. {
  986. p--;
  987. res = scm_cons (scm_ulong2num (*p), res);
  988. }
  989. scm_remember_upto_here_1 (uvec);
  990. return res;
  991. }
  992. #undef FUNC_NAME
  993. SCM_DEFINE (scm_list_to_u32vector, "list->u32vector", 1, 0, 0,
  994. (SCM l),
  995. "Convert the list @var{l}, which must only contain unsigned\n"
  996. "32-bit values, to a numeric homogeneous vector.")
  997. #define FUNC_NAME s_scm_list_to_u32vector
  998. {
  999. SCM uvec;
  1000. int_u32 *p;
  1001. long n; /* to match COPYLEN */
  1002. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1003. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_U32, n);
  1004. p = (int_u32 *) SCM_UVEC_BASE (uvec);
  1005. while (SCM_CONSP (l))
  1006. {
  1007. int_u32 f = scm_num2ulong (SCM_CAR (l), 2, FUNC_NAME);
  1008. *p++ = f;
  1009. l = SCM_CDR (l);
  1010. }
  1011. return uvec;
  1012. }
  1013. #undef FUNC_NAME
  1014. /* ================================================================ */
  1015. /* S32 procedures. */
  1016. /* ================================================================ */
  1017. SCM_DEFINE (scm_s32vector_p, "s32vector?", 1, 0, 0,
  1018. (SCM obj),
  1019. "Return @code{#t} if @var{obj} is a vector of type s32,\n"
  1020. "@code{#f} otherwise.")
  1021. #define FUNC_NAME s_scm_s32vector_p
  1022. {
  1023. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) &&
  1024. SCM_UVEC_TYPE (obj) == SCM_UVEC_S32);
  1025. scm_remember_upto_here_1 (result);
  1026. return result;
  1027. }
  1028. #undef FUNC_NAME
  1029. SCM_DEFINE (scm_make_s32vector, "make-s32vector", 1, 1, 0,
  1030. (SCM n, SCM fill),
  1031. "Create a newly allocated homogeneous numeric vector which can\n"
  1032. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  1033. "initialize the elements, otherwise the contents of the vector\n"
  1034. "is unspecified.")
  1035. #define FUNC_NAME s_scm_make_s32vector
  1036. {
  1037. size_t count = scm_num2size (n, 1, s_scm_make_s32vector);
  1038. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_S32, count);
  1039. if (SCM_UNBNDP (fill))
  1040. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_s32));
  1041. else
  1042. {
  1043. int_s32 * p;
  1044. const long f = scm_num2long (fill, 2, FUNC_NAME);
  1045. #if SIZEOF_LONG > 4
  1046. SCM_ASSERT_RANGE (2, fill,
  1047. (f >= (int_s32) -2147483648)
  1048. && (f <= (int_s32) 2147483647));
  1049. #endif
  1050. p = (int_s32 *) SCM_UVEC_BASE (uvec);
  1051. while (count-- > 0)
  1052. *p++ = f;
  1053. }
  1054. return uvec;
  1055. }
  1056. #undef FUNC_NAME
  1057. SCM_DEFINE (scm_s32vector, "s32vector", 0, 0, 1,
  1058. (SCM l),
  1059. "Create a newly allocated homogeneous numeric vector containing\n"
  1060. "all argument values.")
  1061. #define FUNC_NAME s_scm_s32vector
  1062. {
  1063. SCM_VALIDATE_REST_ARGUMENT (l);
  1064. return scm_list_to_s32vector (l);
  1065. }
  1066. #undef FUNC_NAME
  1067. SCM_DEFINE (scm_s32vector_length, "s32vector-length", 1, 0, 0,
  1068. (SCM uvec),
  1069. "Return the number of elements in the homogeneous numeric vector\n"
  1070. "@var{uvec}.")
  1071. #define FUNC_NAME s_scm_s32vector_length
  1072. {
  1073. return uvec_length (uvec, SCM_UVEC_S32, FUNC_NAME);
  1074. }
  1075. #undef FUNC_NAME
  1076. SCM_DEFINE (scm_s32vector_ref, "s32vector-ref", 2, 0, 0,
  1077. (SCM uvec, SCM index),
  1078. "Return the element at @var{index} in the homogeneous numeric\n"
  1079. "vector @var{uvec}.")
  1080. #define FUNC_NAME s_scm_s32vector_ref
  1081. {
  1082. SCM result;
  1083. size_t idx;
  1084. VALIDATE_UVEC (1, uvec, SCM_UVEC_S32);
  1085. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1086. result = scm_long2num (((int_s32 *) SCM_UVEC_BASE (uvec))[idx]);
  1087. scm_remember_upto_here_1 (uvec);
  1088. return result;
  1089. }
  1090. #undef FUNC_NAME
  1091. SCM_DEFINE (scm_s32vector_set_x, "s32vector-set!", 3, 0, 0,
  1092. (SCM uvec, SCM index, SCM value),
  1093. "Set the element at @var{index} in the homogeneous numeric\n"
  1094. "vector @var{uvec} to @var{value}. The return value is not\n"
  1095. "specified.")
  1096. #define FUNC_NAME s_scm_s32vector_set_x
  1097. {
  1098. size_t idx;
  1099. long f;
  1100. VALIDATE_UVEC (1, uvec, SCM_UVEC_S32);
  1101. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1102. f = scm_num2long (value, 3, FUNC_NAME);
  1103. #if SIZEOF_LONG > 4
  1104. SCM_ASSERT_RANGE (2, value,
  1105. (f >= (int_s32) -2147483648)
  1106. && (f <= (int_s32) 2147483647));
  1107. #endif
  1108. ((int_s32 *) SCM_UVEC_BASE (uvec))[idx] = (int_s32) f;
  1109. scm_remember_upto_here_1 (uvec);
  1110. return SCM_UNSPECIFIED;
  1111. }
  1112. #undef FUNC_NAME
  1113. SCM_DEFINE (scm_s32vector_to_list, "s32vector->list", 1, 0, 0,
  1114. (SCM uvec),
  1115. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  1116. #define FUNC_NAME s_scm_s32vector_to_list
  1117. {
  1118. size_t idx;
  1119. int_s32 * p;
  1120. SCM res = SCM_EOL;
  1121. VALIDATE_UVEC (1, uvec, SCM_UVEC_S32);
  1122. idx = SCM_UVEC_LENGTH (uvec);
  1123. p = (int_s32 *) SCM_UVEC_BASE (uvec) + idx;
  1124. while (idx-- > 0)
  1125. {
  1126. p--;
  1127. res = scm_cons (scm_long2num (*p), res);
  1128. }
  1129. scm_remember_upto_here_1 (uvec);
  1130. return res;
  1131. }
  1132. #undef FUNC_NAME
  1133. SCM_DEFINE (scm_list_to_s32vector, "list->s32vector", 1, 0, 0,
  1134. (SCM l),
  1135. "Convert the list @var{l}, which must only contain signed\n"
  1136. "32-bit values, to a numeric homogeneous vector.")
  1137. #define FUNC_NAME s_scm_list_to_s32vector
  1138. {
  1139. SCM uvec;
  1140. int_s32 *p;
  1141. long n; /* to match COPYLEN */
  1142. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1143. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_S32, n);
  1144. p = (int_s32 *) SCM_UVEC_BASE (uvec);
  1145. while (SCM_CONSP (l))
  1146. {
  1147. int_s32 f = scm_num2long (SCM_CAR (l), 2, FUNC_NAME);
  1148. *p++ = f;
  1149. l = SCM_CDR (l);
  1150. }
  1151. return uvec;
  1152. }
  1153. #undef FUNC_NAME
  1154. #if HAVE_UVEC_64
  1155. /* ================================================================ */
  1156. /* U64 procedures. */
  1157. /* ================================================================ */
  1158. SCM_DEFINE (scm_u64vector_p, "u64vector?", 1, 0, 0,
  1159. (SCM obj),
  1160. "Return @code{#t} if @var{obj} is a vector of type u64,\n"
  1161. "@code{#f} otherwise.")
  1162. #define FUNC_NAME s_scm_u64vector_p
  1163. {
  1164. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) &&
  1165. SCM_UVEC_TYPE (obj) == SCM_UVEC_U64);
  1166. scm_remember_upto_here_1 (obj);
  1167. return result;
  1168. }
  1169. #undef FUNC_NAME
  1170. SCM_DEFINE (scm_make_u64vector, "make-u64vector", 1, 1, 0,
  1171. (SCM n, SCM fill),
  1172. "Create a newly allocated homogeneous numeric vector which can\n"
  1173. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  1174. "initialize the elements, otherwise the contents of the vector\n"
  1175. "is unspecified.")
  1176. #define FUNC_NAME s_scm_make_u64vector
  1177. {
  1178. size_t count = scm_num2size (n, 1, s_scm_make_u64vector);
  1179. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_U64, count);
  1180. if (SCM_UNBNDP (fill))
  1181. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_u64));
  1182. else
  1183. {
  1184. int_u64 * p;
  1185. const unsigned long long f = scm_num2ulong_long (fill, 2, FUNC_NAME);
  1186. #if SIZEOF_LONG_LONG > 8
  1187. SCM_ASSERT_RANGE (2, fill, (f <= (int_u64) 0xFFFFFFFFFFFFFFFFUL));
  1188. #endif
  1189. p = (int_u64 *) SCM_UVEC_BASE (uvec);
  1190. while (count-- > 0)
  1191. *p++ = f;
  1192. }
  1193. return uvec;
  1194. }
  1195. #undef FUNC_NAME
  1196. SCM_DEFINE (scm_u64vector, "u64vector", 0, 0, 1,
  1197. (SCM l),
  1198. "Create a newly allocated homogeneous numeric vector containing\n"
  1199. "all argument values.")
  1200. #define FUNC_NAME s_scm_u64vector
  1201. {
  1202. SCM_VALIDATE_REST_ARGUMENT (l);
  1203. return scm_list_to_u64vector (l);
  1204. }
  1205. #undef FUNC_NAME
  1206. SCM_DEFINE (scm_u64vector_length, "u64vector-length", 1, 0, 0,
  1207. (SCM uvec),
  1208. "Return the number of elements in the homogeneous numeric vector\n"
  1209. "@var{uvec}.")
  1210. #define FUNC_NAME s_scm_u64vector_length
  1211. {
  1212. return uvec_length (uvec, SCM_UVEC_U64, FUNC_NAME);
  1213. }
  1214. #undef FUNC_NAME
  1215. SCM_DEFINE (scm_u64vector_ref, "u64vector-ref", 2, 0, 0,
  1216. (SCM uvec, SCM index),
  1217. "Return the element at @var{index} in the homogeneous numeric\n"
  1218. "vector @var{uvec}.")
  1219. #define FUNC_NAME s_scm_u64vector_ref
  1220. {
  1221. SCM result;
  1222. size_t idx;
  1223. VALIDATE_UVEC (1, uvec, SCM_UVEC_U64);
  1224. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1225. result = scm_ulong_long2num (((int_u64 *) SCM_UVEC_BASE (uvec))[idx]);
  1226. scm_remember_upto_here_1 (uvec);
  1227. return result;
  1228. }
  1229. #undef FUNC_NAME
  1230. SCM_DEFINE (scm_u64vector_set_x, "u64vector-set!", 3, 0, 0,
  1231. (SCM uvec, SCM index, SCM value),
  1232. "Set the element at @var{index} in the homogeneous numeric\n"
  1233. "vector @var{uvec} to @var{value}. The return value is not\n"
  1234. "specified.")
  1235. #define FUNC_NAME s_scm_u64vector_set_x
  1236. {
  1237. size_t idx;
  1238. unsigned long long f;
  1239. VALIDATE_UVEC (1, uvec, SCM_UVEC_U64);
  1240. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1241. f = scm_num2ulong_long (value, 3, FUNC_NAME);
  1242. #if SIZEOF_LONG_LONG > 8
  1243. SCM_ASSERT_RANGE (2, fill, (f <= (int_u64) 0xFFFFFFFFFFFFFFFFUL));
  1244. #endif
  1245. ((int_u64 *) SCM_UVEC_BASE (uvec))[idx] = (int_u64) f;
  1246. scm_remember_upto_here_1 (uvec);
  1247. return SCM_UNSPECIFIED;
  1248. }
  1249. #undef FUNC_NAME
  1250. SCM_DEFINE (scm_u64vector_to_list, "u64vector->list", 1, 0, 0,
  1251. (SCM uvec),
  1252. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  1253. #define FUNC_NAME s_scm_u64vector_to_list
  1254. {
  1255. size_t idx;
  1256. int_u64 * p;
  1257. SCM res = SCM_EOL;
  1258. VALIDATE_UVEC (1, uvec, SCM_UVEC_U64);
  1259. idx = SCM_UVEC_LENGTH (uvec);
  1260. p = (int_u64 *) SCM_UVEC_BASE (uvec) + idx;
  1261. while (idx-- > 0)
  1262. {
  1263. p--;
  1264. res = scm_cons (scm_ulong_long2num (*p), res);
  1265. }
  1266. scm_remember_upto_here_1 (uvec);
  1267. return res;
  1268. }
  1269. #undef FUNC_NAME
  1270. SCM_DEFINE (scm_list_to_u64vector, "list->u64vector", 1, 0, 0,
  1271. (SCM l),
  1272. "Convert the list @var{l}, which must only contain unsigned\n"
  1273. "64-bit values, to a numeric homogeneous vector.")
  1274. #define FUNC_NAME s_scm_list_to_u64vector
  1275. {
  1276. SCM uvec;
  1277. int_u64 *p;
  1278. long n; /* to match COPYLEN */
  1279. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1280. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_U64, n);
  1281. p = (int_u64 *) SCM_UVEC_BASE (uvec);
  1282. while (SCM_CONSP (l))
  1283. {
  1284. int_u64 f = scm_num2ulong_long (SCM_CAR (l), 2, FUNC_NAME);
  1285. *p++ = f;
  1286. l = SCM_CDR (l);
  1287. }
  1288. return uvec;
  1289. }
  1290. #undef FUNC_NAME
  1291. /* ================================================================ */
  1292. /* S64 procedures. */
  1293. /* ================================================================ */
  1294. SCM_DEFINE (scm_s64vector_p, "s64vector?", 1, 0, 0,
  1295. (SCM obj),
  1296. "Return @code{#t} if @var{obj} is a vector of type s64,\n"
  1297. "@code{#f} otherwise.")
  1298. #define FUNC_NAME s_scm_s64vector_p
  1299. {
  1300. const SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) &&
  1301. SCM_UVEC_TYPE (obj) == SCM_UVEC_S64);
  1302. scm_remember_upto_here_1 (obj);
  1303. return result;
  1304. }
  1305. #undef FUNC_NAME
  1306. SCM_DEFINE (scm_make_s64vector, "make-s64vector", 1, 1, 0,
  1307. (SCM n, SCM fill),
  1308. "Create a newly allocated homogeneous numeric vector which can\n"
  1309. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  1310. "initialize the elements, otherwise the contents of the vector\n"
  1311. "is unspecified.")
  1312. #define FUNC_NAME s_scm_make_s64vector
  1313. {
  1314. size_t count = scm_num2size (n, 1, s_scm_make_s64vector);
  1315. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_S64, count);
  1316. if (SCM_UNBNDP (fill))
  1317. memset (SCM_UVEC_BASE (uvec), 0, count * sizeof (int_s64));
  1318. else
  1319. {
  1320. int_s64 * p;
  1321. const long long f = scm_num2long_long (fill, 2, FUNC_NAME);
  1322. #if SIZEOF_LONG_LONG > 8
  1323. SCM_ASSERT_RANGE (2, fill,
  1324. (f >= (int_s64) -9223372036854775808)
  1325. && (f <= (int_s64) 9223372036854775807));
  1326. #endif
  1327. p = (int_s64 *) SCM_UVEC_BASE (uvec);
  1328. while (count-- > 0)
  1329. *p++ = f;
  1330. }
  1331. return uvec;
  1332. }
  1333. #undef FUNC_NAME
  1334. SCM_DEFINE (scm_s64vector, "s64vector", 0, 0, 1,
  1335. (SCM l),
  1336. "Create a newly allocated homogeneous numeric vector containing\n"
  1337. "all argument values.")
  1338. #define FUNC_NAME s_scm_s64vector
  1339. {
  1340. SCM_VALIDATE_REST_ARGUMENT (l);
  1341. return scm_list_to_s64vector (l);
  1342. }
  1343. #undef FUNC_NAME
  1344. SCM_DEFINE (scm_s64vector_length, "s64vector-length", 1, 0, 0,
  1345. (SCM uvec),
  1346. "Return the number of elements in the homogeneous numeric vector\n"
  1347. "@var{uvec}.")
  1348. #define FUNC_NAME s_scm_s64vector_length
  1349. {
  1350. return uvec_length (uvec, SCM_UVEC_S64, FUNC_NAME);
  1351. }
  1352. #undef FUNC_NAME
  1353. SCM_DEFINE (scm_s64vector_ref, "s64vector-ref", 2, 0, 0,
  1354. (SCM uvec, SCM index),
  1355. "Return the element at @var{index} in the homogeneous numeric\n"
  1356. "vector @var{uvec}.")
  1357. #define FUNC_NAME s_scm_s64vector_ref
  1358. {
  1359. SCM result;
  1360. size_t idx;
  1361. VALIDATE_UVEC (1, uvec, SCM_UVEC_S64);
  1362. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1363. result = scm_long_long2num (((int_s64 *) SCM_UVEC_BASE (uvec))[idx]);
  1364. scm_remember_upto_here_1 (uvec);
  1365. return result;
  1366. }
  1367. #undef FUNC_NAME
  1368. SCM_DEFINE (scm_s64vector_set_x, "s64vector-set!", 3, 0, 0,
  1369. (SCM uvec, SCM index, SCM value),
  1370. "Set the element at @var{index} in the homogeneous numeric\n"
  1371. "vector @var{uvec} to @var{value}. The return value is not\n"
  1372. "specified.")
  1373. #define FUNC_NAME s_scm_s64vector_set_x
  1374. {
  1375. size_t idx;
  1376. long long f;
  1377. VALIDATE_UVEC (1, uvec, SCM_UVEC_S64);
  1378. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1379. f = scm_num2long_long (value, 3, FUNC_NAME);
  1380. #if SIZEOF_LONG_LONG > 8
  1381. SCM_ASSERT_RANGE (2, value,
  1382. (f >= (int_s64) -9223372036854775808)
  1383. && (f <= (int_s64) 9223372036854775807));
  1384. #endif
  1385. ((int_s64 *) SCM_UVEC_BASE (uvec))[idx] = (int_s64) f;
  1386. scm_remember_upto_here_1 (uvec);
  1387. return SCM_UNSPECIFIED;
  1388. }
  1389. #undef FUNC_NAME
  1390. SCM_DEFINE (scm_s64vector_to_list, "s64vector->list", 1, 0, 0,
  1391. (SCM uvec),
  1392. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  1393. #define FUNC_NAME s_scm_s64vector_to_list
  1394. {
  1395. size_t idx;
  1396. int_s64 * p;
  1397. SCM res = SCM_EOL;
  1398. VALIDATE_UVEC (1, uvec, SCM_UVEC_S64);
  1399. idx = SCM_UVEC_LENGTH (uvec);
  1400. p = (int_s64 *) SCM_UVEC_BASE (uvec) + idx;
  1401. while (idx-- > 0)
  1402. {
  1403. p--;
  1404. res = scm_cons (scm_long_long2num (*p), res);
  1405. }
  1406. scm_remember_upto_here_1 (uvec);
  1407. return res;
  1408. }
  1409. #undef FUNC_NAME
  1410. SCM_DEFINE (scm_list_to_s64vector, "list->s64vector", 1, 0, 0,
  1411. (SCM l),
  1412. "Convert the list @var{l}, which must only contain signed\n"
  1413. "64-bit values, to a numeric homogeneous vector.")
  1414. #define FUNC_NAME s_scm_list_to_s64vector
  1415. {
  1416. SCM uvec;
  1417. int_s64 *p;
  1418. long n; /* to match COPYLEN */
  1419. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1420. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_S64, n);
  1421. p = (int_s64 *) SCM_UVEC_BASE (uvec);
  1422. while (SCM_CONSP (l))
  1423. {
  1424. int_s64 f = scm_num2long_long (SCM_CAR (l), 2, FUNC_NAME);
  1425. *p++ = f;
  1426. l = SCM_CDR (l);
  1427. }
  1428. return uvec;
  1429. }
  1430. #undef FUNC_NAME
  1431. #endif /* HAVE_UVEC_64 */
  1432. /* ================================================================ */
  1433. /* F32 procedures. */
  1434. /* ================================================================ */
  1435. SCM_DEFINE (scm_f32vector_p, "f32vector?", 1, 0, 0,
  1436. (SCM obj),
  1437. "Return @code{#t} if @var{obj} is a vector of type f32,\n"
  1438. "@code{#f} otherwise.")
  1439. #define FUNC_NAME s_scm_f32vector_p
  1440. {
  1441. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) &&
  1442. SCM_UVEC_TYPE (obj) == SCM_UVEC_F32);
  1443. scm_remember_upto_here_1 (obj);
  1444. return result;
  1445. }
  1446. #undef FUNC_NAME
  1447. SCM_DEFINE (scm_make_f32vector, "make-f32vector", 1, 1, 0,
  1448. (SCM n, SCM fill),
  1449. "Create a newly allocated homogeneous numeric vector which can\n"
  1450. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  1451. "initialize the elements, otherwise the contents of the vector\n"
  1452. "is unspecified.")
  1453. #define FUNC_NAME s_scm_make_f32vector
  1454. {
  1455. float_f32 f;
  1456. float_f32 *p;
  1457. size_t count = scm_num2size (n, 1, s_scm_make_f32vector);
  1458. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_F32, count);
  1459. if (SCM_UNBNDP (fill))
  1460. f = 0;
  1461. else
  1462. f = scm_num2float (fill, 2, FUNC_NAME);
  1463. p = (float_f32 *) SCM_UVEC_BASE (uvec);
  1464. while (count-- > 0)
  1465. *p++ = f;
  1466. return uvec;
  1467. }
  1468. #undef FUNC_NAME
  1469. SCM_DEFINE (scm_f32vector, "f32vector", 0, 0, 1,
  1470. (SCM l),
  1471. "Create a newly allocated homogeneous numeric vector containing\n"
  1472. "all argument values.")
  1473. #define FUNC_NAME s_scm_f32vector
  1474. {
  1475. SCM_VALIDATE_REST_ARGUMENT (l);
  1476. return scm_list_to_f32vector (l);
  1477. }
  1478. #undef FUNC_NAME
  1479. SCM_DEFINE (scm_f32vector_length, "f32vector-length", 1, 0, 0,
  1480. (SCM uvec),
  1481. "Return the number of elements in the homogeneous numeric vector\n"
  1482. "@var{uvec}.")
  1483. #define FUNC_NAME s_scm_f32vector_length
  1484. {
  1485. return uvec_length (uvec, SCM_UVEC_F32, FUNC_NAME);
  1486. }
  1487. #undef FUNC_NAME
  1488. SCM_DEFINE (scm_f32vector_ref, "f32vector-ref", 2, 0, 0,
  1489. (SCM uvec, SCM index),
  1490. "Return the element at @var{index} in the homogeneous numeric\n"
  1491. "vector @var{uvec}.")
  1492. #define FUNC_NAME s_scm_f32vector_ref
  1493. {
  1494. SCM result;
  1495. size_t idx;
  1496. VALIDATE_UVEC (1, uvec, SCM_UVEC_F32);
  1497. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1498. result = scm_make_real (((float_f32 *) SCM_UVEC_BASE (uvec))[idx]);
  1499. scm_remember_upto_here_1 (uvec);
  1500. return result;
  1501. }
  1502. #undef FUNC_NAME
  1503. SCM_DEFINE (scm_f32vector_set_x, "f32vector-set!", 3, 0, 0,
  1504. (SCM uvec, SCM index, SCM value),
  1505. "Set the element at @var{index} in the homogeneous numeric\n"
  1506. "vector @var{uvec} to @var{value}. The return value is not\n"
  1507. "specified.")
  1508. #define FUNC_NAME s_scm_f32vector_set_x
  1509. {
  1510. size_t idx;
  1511. float f;
  1512. VALIDATE_UVEC (1, uvec, SCM_UVEC_F32);
  1513. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1514. f = scm_num2float (value, 3, FUNC_NAME);
  1515. ((float_f32 *) SCM_UVEC_BASE (uvec))[idx] = f;
  1516. scm_remember_upto_here_1 (uvec);
  1517. return SCM_UNSPECIFIED;
  1518. }
  1519. #undef FUNC_NAME
  1520. SCM_DEFINE (scm_f32vector_to_list, "f32vector->list", 1, 0, 0,
  1521. (SCM uvec),
  1522. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  1523. #define FUNC_NAME s_scm_f32vector_to_list
  1524. {
  1525. size_t idx;
  1526. float_f32 * p;
  1527. SCM res = SCM_EOL;
  1528. VALIDATE_UVEC (1, uvec, SCM_UVEC_F32);
  1529. idx = SCM_UVEC_LENGTH (uvec);
  1530. p = (float_f32 *) SCM_UVEC_BASE (uvec) + idx;
  1531. while (idx-- > 0)
  1532. {
  1533. p--;
  1534. res = scm_cons (scm_float2num (*p), res);
  1535. }
  1536. scm_remember_upto_here_1 (uvec);
  1537. return res;
  1538. }
  1539. #undef FUNC_NAME
  1540. SCM_DEFINE (scm_list_to_f32vector, "list->f32vector", 1, 0, 0,
  1541. (SCM l),
  1542. "Convert the list @var{l}, which must only contain unsigned\n"
  1543. "8-bit values, to a numeric homogeneous vector.")
  1544. #define FUNC_NAME s_scm_list_to_f32vector
  1545. {
  1546. SCM uvec;
  1547. float_f32 *p;
  1548. long n; /* to match COPYLEN */
  1549. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1550. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_F32, n);
  1551. p = (float_f32 *) SCM_UVEC_BASE (uvec);
  1552. while (SCM_CONSP (l))
  1553. {
  1554. float_f32 f = scm_num2float (SCM_CAR (l), 2, FUNC_NAME);
  1555. *p++ = f;
  1556. l = SCM_CDR (l);
  1557. }
  1558. return uvec;
  1559. }
  1560. #undef FUNC_NAME
  1561. /* ================================================================ */
  1562. /* F64 procedures. */
  1563. /* ================================================================ */
  1564. SCM_DEFINE (scm_f64vector_p, "f64vector?", 1, 0, 0,
  1565. (SCM obj),
  1566. "Return @code{#t} if @var{obj} is a vector of type f64,\n"
  1567. "@code{#f} otherwise.")
  1568. #define FUNC_NAME s_scm_f64vector_p
  1569. {
  1570. SCM result = SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) &&
  1571. SCM_UVEC_TYPE (obj) == SCM_UVEC_F64);
  1572. scm_remember_upto_here_1 (obj);
  1573. return result;
  1574. }
  1575. #undef FUNC_NAME
  1576. SCM_DEFINE (scm_make_f64vector, "make-f64vector", 1, 1, 0,
  1577. (SCM n, SCM fill),
  1578. "Create a newly allocated homogeneous numeric vector which can\n"
  1579. "hold @var{len} elements. If @var{fill} is given, it is used to\n"
  1580. "initialize the elements, otherwise the contents of the vector\n"
  1581. "is unspecified.")
  1582. #define FUNC_NAME s_scm_make_f64vector
  1583. {
  1584. float_f64 f;
  1585. float_f64 *p;
  1586. size_t count = scm_num2size (n, 1, s_scm_make_f64vector);
  1587. SCM uvec = make_uvec (1, FUNC_NAME, SCM_UVEC_F64, count);
  1588. if (SCM_UNBNDP (fill))
  1589. f = 0;
  1590. else
  1591. f = scm_num2double (fill, 2, FUNC_NAME);
  1592. p = (float_f64 *) SCM_UVEC_BASE (uvec);
  1593. while (count-- > 0)
  1594. *p++ = f;
  1595. return uvec;
  1596. }
  1597. #undef FUNC_NAME
  1598. SCM_DEFINE (scm_f64vector, "f64vector", 0, 0, 1,
  1599. (SCM l),
  1600. "Create a newly allocated homogeneous numeric vector containing\n"
  1601. "all argument values.")
  1602. #define FUNC_NAME s_scm_f64vector
  1603. {
  1604. SCM_VALIDATE_REST_ARGUMENT (l);
  1605. return scm_list_to_f64vector (l);
  1606. }
  1607. #undef FUNC_NAME
  1608. SCM_DEFINE (scm_f64vector_length, "f64vector-length", 1, 0, 0,
  1609. (SCM uvec),
  1610. "Return the number of elements in the homogeneous numeric vector\n"
  1611. "@var{uvec}.")
  1612. #define FUNC_NAME s_scm_f64vector_length
  1613. {
  1614. return uvec_length (uvec, SCM_UVEC_F64, FUNC_NAME);
  1615. }
  1616. #undef FUNC_NAME
  1617. SCM_DEFINE (scm_f64vector_ref, "f64vector-ref", 2, 0, 0,
  1618. (SCM uvec, SCM index),
  1619. "Return the element at @var{index} in the homogeneous numeric\n"
  1620. "vector @var{uvec}.")
  1621. #define FUNC_NAME s_scm_f64vector_ref
  1622. {
  1623. SCM result;
  1624. size_t idx;
  1625. VALIDATE_UVEC (1, uvec, SCM_UVEC_F64);
  1626. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1627. result = scm_make_real (((float_f64 *) SCM_UVEC_BASE (uvec))[idx]);
  1628. scm_remember_upto_here_1 (uvec);
  1629. return result;
  1630. }
  1631. #undef FUNC_NAME
  1632. SCM_DEFINE (scm_f64vector_set_x, "f64vector-set!", 3, 0, 0,
  1633. (SCM uvec, SCM index, SCM value),
  1634. "Set the element at @var{index} in the homogeneous numeric\n"
  1635. "vector @var{uvec} to @var{value}. The return value is not\n"
  1636. "specified.")
  1637. #define FUNC_NAME s_scm_f64vector_set_x
  1638. {
  1639. size_t idx;
  1640. float f;
  1641. VALIDATE_UVEC (1, uvec, SCM_UVEC_F64);
  1642. RANGE_CHECK_AND_COPY_UVEC_INDEX (2, uvec, index, idx);
  1643. f = scm_num2double (value, 3, FUNC_NAME);
  1644. ((float_f64 *) SCM_UVEC_BASE (uvec))[idx] = f;
  1645. scm_remember_upto_here_1 (uvec);
  1646. return SCM_UNSPECIFIED;
  1647. }
  1648. #undef FUNC_NAME
  1649. SCM_DEFINE (scm_f64vector_to_list, "f64vector->list", 1, 0, 0,
  1650. (SCM uvec),
  1651. "Convert the homogeneous numeric vector @var{uvec} to a list.")
  1652. #define FUNC_NAME s_scm_f64vector_to_list
  1653. {
  1654. size_t idx;
  1655. float_f64 * p;
  1656. SCM res = SCM_EOL;
  1657. VALIDATE_UVEC (1, uvec, SCM_UVEC_F64);
  1658. idx = SCM_UVEC_LENGTH (uvec);
  1659. p = (float_f64 *) SCM_UVEC_BASE (uvec) + idx;
  1660. while (idx-- > 0)
  1661. {
  1662. p--;
  1663. res = scm_cons (scm_double2num (*p), res);
  1664. }
  1665. scm_remember_upto_here_1 (uvec);
  1666. return res;
  1667. }
  1668. #undef FUNC_NAME
  1669. SCM_DEFINE (scm_list_to_f64vector, "list->f64vector", 1, 0, 0,
  1670. (SCM l),
  1671. "Convert the list @var{l}, which must only contain signed\n"
  1672. "8-bit values, to a numeric homogeneous vector.")
  1673. #define FUNC_NAME s_scm_list_to_f64vector
  1674. {
  1675. SCM uvec;
  1676. float_f64 *p;
  1677. long n; /* to match COPYLEN */
  1678. SCM_VALIDATE_LIST_COPYLEN (1, l, n);
  1679. uvec = make_uvec (-1, FUNC_NAME, SCM_UVEC_F64, n);
  1680. p = (float_f64 *) SCM_UVEC_BASE (uvec);
  1681. while (SCM_CONSP (l))
  1682. {
  1683. float_f64 f = scm_num2double (SCM_CAR (l), 2, FUNC_NAME);
  1684. *p++ = f;
  1685. l = SCM_CDR (l);
  1686. }
  1687. return uvec;
  1688. }
  1689. #undef FUNC_NAME
  1690. /* Create the smob type for homogeneous numeric vectors and install
  1691. the primitives. */
  1692. void
  1693. scm_init_srfi_4 (void)
  1694. {
  1695. /* this will be a compilation-time test in future versions of guile */
  1696. if(sizeof(size_t) > sizeof(scm_t_bits))
  1697. {
  1698. fprintf(stderr, "fatal: size_t will not fit in a cell (srfi-4)\n");
  1699. abort();
  1700. }
  1701. scm_tc16_uvec = scm_make_smob_type ("uvec", 0);
  1702. scm_set_smob_equalp (scm_tc16_uvec, uvec_equalp);
  1703. scm_set_smob_free (scm_tc16_uvec, uvec_free);
  1704. scm_set_smob_print (scm_tc16_uvec, uvec_print);
  1705. #include "srfi/srfi-4.x"
  1706. }