bytevectors.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /* Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include <assert.h>
  23. #include <gmp.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/extensions.h"
  26. #include "libguile/bytevectors.h"
  27. #include "libguile/strings.h"
  28. #include "libguile/validate.h"
  29. #include "libguile/ieee-754.h"
  30. #include "libguile/arrays.h"
  31. #include "libguile/array-handle.h"
  32. #include "libguile/uniform.h"
  33. #include "libguile/srfi-4.h"
  34. #include <byteswap.h>
  35. #include <striconveh.h>
  36. #include <uniconv.h>
  37. #include <unistr.h>
  38. #ifdef HAVE_LIMITS_H
  39. # include <limits.h>
  40. #else
  41. /* Assuming 32-bit longs. */
  42. # define ULONG_MAX 4294967295UL
  43. #endif
  44. #include <string.h>
  45. /* Utilities. */
  46. /* Convenience macros. These are used by the various templates (macros) that
  47. are parameterized by integer signedness. */
  48. #define INT8_T_signed scm_t_int8
  49. #define INT8_T_unsigned scm_t_uint8
  50. #define INT16_T_signed scm_t_int16
  51. #define INT16_T_unsigned scm_t_uint16
  52. #define INT32_T_signed scm_t_int32
  53. #define INT32_T_unsigned scm_t_uint32
  54. #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
  55. #define is_unsigned_int8(_x) ((_x) <= 255UL)
  56. #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
  57. #define is_unsigned_int16(_x) ((_x) <= 65535UL)
  58. #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
  59. #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
  60. #define SIGNEDNESS_signed 1
  61. #define SIGNEDNESS_unsigned 0
  62. #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
  63. #define INT_SWAP(_size) bswap_ ## _size
  64. #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
  65. #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
  66. #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
  67. size_t c_len, c_index; \
  68. _sign char *c_bv; \
  69. \
  70. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  71. c_index = scm_to_uint (index); \
  72. \
  73. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  74. c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  75. \
  76. if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
  77. scm_out_of_range (FUNC_NAME, index);
  78. /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
  79. #define INTEGER_REF(_len, _sign) \
  80. SCM result; \
  81. \
  82. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  83. SCM_VALIDATE_SYMBOL (3, endianness); \
  84. \
  85. { \
  86. INT_TYPE (_len, _sign) c_result; \
  87. \
  88. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  89. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  90. c_result = INT_SWAP (_len) (c_result); \
  91. \
  92. result = SCM_I_MAKINUM (c_result); \
  93. } \
  94. \
  95. return result;
  96. /* Template for fixed-size integer access using the native endianness. */
  97. #define INTEGER_NATIVE_REF(_len, _sign) \
  98. SCM result; \
  99. \
  100. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  101. \
  102. { \
  103. INT_TYPE (_len, _sign) c_result; \
  104. \
  105. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  106. result = SCM_I_MAKINUM (c_result); \
  107. } \
  108. \
  109. return result;
  110. /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
  111. #define INTEGER_SET(_len, _sign) \
  112. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  113. SCM_VALIDATE_SYMBOL (3, endianness); \
  114. \
  115. { \
  116. scm_t_signed_bits c_value; \
  117. INT_TYPE (_len, _sign) c_value_short; \
  118. \
  119. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  120. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  121. \
  122. c_value = SCM_I_INUM (value); \
  123. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  124. scm_out_of_range (FUNC_NAME, value); \
  125. \
  126. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  127. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  128. c_value_short = INT_SWAP (_len) (c_value_short); \
  129. \
  130. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  131. } \
  132. \
  133. return SCM_UNSPECIFIED;
  134. /* Template for fixed-size integer modification using the native
  135. endianness. */
  136. #define INTEGER_NATIVE_SET(_len, _sign) \
  137. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  138. \
  139. { \
  140. scm_t_signed_bits c_value; \
  141. INT_TYPE (_len, _sign) c_value_short; \
  142. \
  143. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  144. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  145. \
  146. c_value = SCM_I_INUM (value); \
  147. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  148. scm_out_of_range (FUNC_NAME, value); \
  149. \
  150. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  151. \
  152. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  153. } \
  154. \
  155. return SCM_UNSPECIFIED;
  156. /* Bytevector type. */
  157. #define SCM_BYTEVECTOR_HEADER_BYTES \
  158. (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (SCM))
  159. #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
  160. SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
  161. #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
  162. SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
  163. #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
  164. SCM_SET_BYTEVECTOR_FLAGS ((bv), \
  165. SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
  166. | ((contiguous_p) << 8UL))
  167. #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
  168. SCM_SET_BYTEVECTOR_FLAGS ((bv), \
  169. (hint) \
  170. | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
  171. #define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
  172. SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
  173. #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
  174. (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
  175. #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
  176. (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
  177. /* The empty bytevector. */
  178. SCM scm_null_bytevector = SCM_UNSPECIFIED;
  179. static inline SCM
  180. make_bytevector (size_t len, scm_t_array_element_type element_type)
  181. {
  182. SCM ret;
  183. size_t c_len;
  184. if (SCM_UNLIKELY (element_type > SCM_ARRAY_ELEMENT_TYPE_LAST
  185. || scm_i_array_element_type_sizes[element_type] < 8
  186. || len >= (SCM_I_SIZE_MAX
  187. / (scm_i_array_element_type_sizes[element_type]/8))))
  188. /* This would be an internal Guile programming error */
  189. abort ();
  190. if (SCM_UNLIKELY (len == 0 && element_type == SCM_ARRAY_ELEMENT_TYPE_VU8
  191. && SCM_BYTEVECTOR_P (scm_null_bytevector)))
  192. ret = scm_null_bytevector;
  193. else
  194. {
  195. signed char *contents;
  196. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  197. contents = scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES + c_len,
  198. SCM_GC_BYTEVECTOR);
  199. ret = PTR2SCM (contents);
  200. contents += SCM_BYTEVECTOR_HEADER_BYTES;
  201. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  202. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  203. SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 1);
  204. SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
  205. SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
  206. }
  207. return ret;
  208. }
  209. /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
  210. values taken from CONTENTS. Assume that the storage for CONTENTS will be
  211. automatically reclaimed when it becomes unreachable. */
  212. static inline SCM
  213. make_bytevector_from_buffer (size_t len, void *contents,
  214. scm_t_array_element_type element_type)
  215. {
  216. SCM ret;
  217. if (SCM_UNLIKELY (len == 0))
  218. ret = make_bytevector (len, element_type);
  219. else
  220. {
  221. size_t c_len;
  222. ret = PTR2SCM (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES,
  223. SCM_GC_BYTEVECTOR));
  224. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  225. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  226. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  227. SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 0);
  228. SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
  229. SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
  230. }
  231. return ret;
  232. }
  233. /* Return a new bytevector of size LEN octets. */
  234. SCM
  235. scm_c_make_bytevector (size_t len)
  236. {
  237. return make_bytevector (len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  238. }
  239. /* Return a new bytevector of size LEN elements. */
  240. SCM
  241. scm_i_make_typed_bytevector (size_t len, scm_t_array_element_type element_type)
  242. {
  243. return make_bytevector (len, element_type);
  244. }
  245. /* Return a bytevector of size LEN made up of CONTENTS. The area
  246. pointed to by CONTENTS must be protected from GC somehow: either
  247. because it was allocated using `scm_gc_malloc ()', or because it is
  248. part of PARENT. */
  249. SCM
  250. scm_c_take_bytevector (signed char *contents, size_t len, SCM parent)
  251. {
  252. SCM ret;
  253. ret = make_bytevector_from_buffer (len, contents, SCM_ARRAY_ELEMENT_TYPE_VU8);
  254. SCM_BYTEVECTOR_SET_PARENT (ret, parent);
  255. return ret;
  256. }
  257. SCM
  258. scm_c_take_typed_bytevector (signed char *contents, size_t len,
  259. scm_t_array_element_type element_type, SCM parent)
  260. {
  261. SCM ret;
  262. ret = make_bytevector_from_buffer (len, contents, element_type);
  263. SCM_BYTEVECTOR_SET_PARENT (ret, parent);
  264. return ret;
  265. }
  266. /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
  267. size) and return the new bytevector (possibly different from BV). */
  268. SCM
  269. scm_c_shrink_bytevector (SCM bv, size_t c_new_len)
  270. {
  271. SCM new_bv;
  272. size_t c_len;
  273. if (SCM_UNLIKELY (c_new_len % SCM_BYTEVECTOR_TYPE_SIZE (bv)))
  274. /* This would be an internal Guile programming error */
  275. abort ();
  276. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  277. if (SCM_UNLIKELY (c_new_len > c_len))
  278. abort ();
  279. SCM_BYTEVECTOR_SET_LENGTH (bv, c_new_len);
  280. if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv))
  281. new_bv = PTR2SCM (scm_gc_realloc (SCM2PTR (bv),
  282. c_len + SCM_BYTEVECTOR_HEADER_BYTES,
  283. c_new_len + SCM_BYTEVECTOR_HEADER_BYTES,
  284. SCM_GC_BYTEVECTOR));
  285. else
  286. {
  287. signed char *c_bv;
  288. c_bv = scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv),
  289. c_len, c_new_len, SCM_GC_BYTEVECTOR);
  290. SCM_BYTEVECTOR_SET_CONTENTS (bv, c_bv);
  291. new_bv = bv;
  292. }
  293. return new_bv;
  294. }
  295. int
  296. scm_is_bytevector (SCM obj)
  297. {
  298. return SCM_BYTEVECTOR_P (obj);
  299. }
  300. size_t
  301. scm_c_bytevector_length (SCM bv)
  302. #define FUNC_NAME "scm_c_bytevector_length"
  303. {
  304. SCM_VALIDATE_BYTEVECTOR (1, bv);
  305. return SCM_BYTEVECTOR_LENGTH (bv);
  306. }
  307. #undef FUNC_NAME
  308. scm_t_uint8
  309. scm_c_bytevector_ref (SCM bv, size_t index)
  310. #define FUNC_NAME "scm_c_bytevector_ref"
  311. {
  312. size_t c_len;
  313. const scm_t_uint8 *c_bv;
  314. SCM_VALIDATE_BYTEVECTOR (1, bv);
  315. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  316. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  317. if (SCM_UNLIKELY (index >= c_len))
  318. scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
  319. return c_bv[index];
  320. }
  321. #undef FUNC_NAME
  322. void
  323. scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
  324. #define FUNC_NAME "scm_c_bytevector_set_x"
  325. {
  326. size_t c_len;
  327. scm_t_uint8 *c_bv;
  328. SCM_VALIDATE_BYTEVECTOR (1, bv);
  329. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  330. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  331. if (SCM_UNLIKELY (index >= c_len))
  332. scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
  333. c_bv[index] = value;
  334. }
  335. #undef FUNC_NAME
  336. int
  337. scm_i_print_bytevector (SCM bv, SCM port, scm_print_state *pstate SCM_UNUSED)
  338. {
  339. ssize_t ubnd, inc, i;
  340. scm_t_array_handle h;
  341. scm_array_get_handle (bv, &h);
  342. scm_putc ('#', port);
  343. scm_write (scm_array_handle_element_type (&h), port);
  344. scm_putc ('(', port);
  345. for (i = h.dims[0].lbnd, ubnd = h.dims[0].ubnd, inc = h.dims[0].inc;
  346. i <= ubnd; i += inc)
  347. {
  348. if (i > 0)
  349. scm_putc (' ', port);
  350. scm_write (scm_array_handle_ref (&h, i), port);
  351. }
  352. scm_putc (')', port);
  353. return 1;
  354. }
  355. /* General operations. */
  356. SCM_SYMBOL (scm_sym_big, "big");
  357. SCM_SYMBOL (scm_sym_little, "little");
  358. SCM scm_endianness_big, scm_endianness_little;
  359. /* Host endianness (a symbol). */
  360. SCM scm_i_native_endianness = SCM_UNSPECIFIED;
  361. /* Byte-swapping. */
  362. #ifndef bswap_24
  363. # define bswap_24(_x) \
  364. ((((_x) & 0xff0000) >> 16) | \
  365. (((_x) & 0x00ff00)) | \
  366. (((_x) & 0x0000ff) << 16))
  367. #endif
  368. SCM_DEFINE (scm_native_endianness, "native-endianness", 0, 0, 0,
  369. (void),
  370. "Return a symbol denoting the machine's native endianness.")
  371. #define FUNC_NAME s_scm_native_endianness
  372. {
  373. return scm_i_native_endianness;
  374. }
  375. #undef FUNC_NAME
  376. SCM_DEFINE (scm_bytevector_p, "bytevector?", 1, 0, 0,
  377. (SCM obj),
  378. "Return true if @var{obj} is a bytevector.")
  379. #define FUNC_NAME s_scm_bytevector_p
  380. {
  381. return scm_from_bool (scm_is_bytevector (obj));
  382. }
  383. #undef FUNC_NAME
  384. SCM_DEFINE (scm_make_bytevector, "make-bytevector", 1, 1, 0,
  385. (SCM len, SCM fill),
  386. "Return a newly allocated bytevector of @var{len} bytes, "
  387. "optionally filled with @var{fill}.")
  388. #define FUNC_NAME s_scm_make_bytevector
  389. {
  390. SCM bv;
  391. unsigned c_len;
  392. signed char c_fill = '\0';
  393. SCM_VALIDATE_UINT_COPY (1, len, c_len);
  394. if (!scm_is_eq (fill, SCM_UNDEFINED))
  395. {
  396. int value;
  397. value = scm_to_int (fill);
  398. if (SCM_UNLIKELY ((value < -128) || (value > 255)))
  399. scm_out_of_range (FUNC_NAME, fill);
  400. c_fill = (signed char) value;
  401. }
  402. bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  403. if (!scm_is_eq (fill, SCM_UNDEFINED))
  404. {
  405. unsigned i;
  406. signed char *contents;
  407. contents = SCM_BYTEVECTOR_CONTENTS (bv);
  408. for (i = 0; i < c_len; i++)
  409. contents[i] = c_fill;
  410. }
  411. else
  412. memset (SCM_BYTEVECTOR_CONTENTS (bv), 0, c_len);
  413. return bv;
  414. }
  415. #undef FUNC_NAME
  416. SCM_DEFINE (scm_bytevector_length, "bytevector-length", 1, 0, 0,
  417. (SCM bv),
  418. "Return the length (in bytes) of @var{bv}.")
  419. #define FUNC_NAME s_scm_bytevector_length
  420. {
  421. return scm_from_uint (scm_c_bytevector_length (bv));
  422. }
  423. #undef FUNC_NAME
  424. SCM_DEFINE (scm_bytevector_eq_p, "bytevector=?", 2, 0, 0,
  425. (SCM bv1, SCM bv2),
  426. "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
  427. "have the same length and contents.")
  428. #define FUNC_NAME s_scm_bytevector_eq_p
  429. {
  430. SCM result = SCM_BOOL_F;
  431. unsigned c_len1, c_len2;
  432. SCM_VALIDATE_BYTEVECTOR (1, bv1);
  433. SCM_VALIDATE_BYTEVECTOR (2, bv2);
  434. c_len1 = SCM_BYTEVECTOR_LENGTH (bv1);
  435. c_len2 = SCM_BYTEVECTOR_LENGTH (bv2);
  436. if (c_len1 == c_len2 && (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1)
  437. == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2)))
  438. {
  439. signed char *c_bv1, *c_bv2;
  440. c_bv1 = SCM_BYTEVECTOR_CONTENTS (bv1);
  441. c_bv2 = SCM_BYTEVECTOR_CONTENTS (bv2);
  442. result = scm_from_bool (!memcmp (c_bv1, c_bv2, c_len1));
  443. }
  444. return result;
  445. }
  446. #undef FUNC_NAME
  447. SCM_DEFINE (scm_bytevector_fill_x, "bytevector-fill!", 2, 0, 0,
  448. (SCM bv, SCM fill),
  449. "Fill bytevector @var{bv} with @var{fill}, a byte.")
  450. #define FUNC_NAME s_scm_bytevector_fill_x
  451. {
  452. unsigned c_len, i;
  453. signed char *c_bv, c_fill;
  454. SCM_VALIDATE_BYTEVECTOR (1, bv);
  455. c_fill = scm_to_int8 (fill);
  456. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  457. c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
  458. for (i = 0; i < c_len; i++)
  459. c_bv[i] = c_fill;
  460. return SCM_UNSPECIFIED;
  461. }
  462. #undef FUNC_NAME
  463. SCM_DEFINE (scm_bytevector_copy_x, "bytevector-copy!", 5, 0, 0,
  464. (SCM source, SCM source_start, SCM target, SCM target_start,
  465. SCM len),
  466. "Copy @var{len} bytes from @var{source} into @var{target}, "
  467. "starting reading from @var{source_start} (a positive index "
  468. "within @var{source}) and start writing at "
  469. "@var{target_start}.")
  470. #define FUNC_NAME s_scm_bytevector_copy_x
  471. {
  472. unsigned c_len, c_source_len, c_target_len;
  473. unsigned c_source_start, c_target_start;
  474. signed char *c_source, *c_target;
  475. SCM_VALIDATE_BYTEVECTOR (1, source);
  476. SCM_VALIDATE_BYTEVECTOR (3, target);
  477. c_len = scm_to_uint (len);
  478. c_source_start = scm_to_uint (source_start);
  479. c_target_start = scm_to_uint (target_start);
  480. c_source = SCM_BYTEVECTOR_CONTENTS (source);
  481. c_target = SCM_BYTEVECTOR_CONTENTS (target);
  482. c_source_len = SCM_BYTEVECTOR_LENGTH (source);
  483. c_target_len = SCM_BYTEVECTOR_LENGTH (target);
  484. if (SCM_UNLIKELY (c_source_start + c_len > c_source_len))
  485. scm_out_of_range (FUNC_NAME, source_start);
  486. if (SCM_UNLIKELY (c_target_start + c_len > c_target_len))
  487. scm_out_of_range (FUNC_NAME, target_start);
  488. memcpy (c_target + c_target_start,
  489. c_source + c_source_start,
  490. c_len);
  491. return SCM_UNSPECIFIED;
  492. }
  493. #undef FUNC_NAME
  494. SCM_DEFINE (scm_bytevector_copy, "bytevector-copy", 1, 0, 0,
  495. (SCM bv),
  496. "Return a newly allocated copy of @var{bv}.")
  497. #define FUNC_NAME s_scm_bytevector_copy
  498. {
  499. SCM copy;
  500. unsigned c_len;
  501. signed char *c_bv, *c_copy;
  502. SCM_VALIDATE_BYTEVECTOR (1, bv);
  503. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  504. c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
  505. copy = make_bytevector (c_len, SCM_BYTEVECTOR_ELEMENT_TYPE (bv));
  506. c_copy = SCM_BYTEVECTOR_CONTENTS (copy);
  507. memcpy (c_copy, c_bv, c_len);
  508. return copy;
  509. }
  510. #undef FUNC_NAME
  511. SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
  512. 1, 0, 0, (SCM array),
  513. "Return a newly allocated bytevector whose contents\n"
  514. "will be copied from the uniform array @var{array}.")
  515. #define FUNC_NAME s_scm_uniform_array_to_bytevector
  516. {
  517. SCM contents, ret;
  518. size_t len, sz, byte_len;
  519. scm_t_array_handle h;
  520. const void *elts;
  521. contents = scm_array_contents (array, SCM_BOOL_T);
  522. if (scm_is_false (contents))
  523. scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
  524. scm_array_get_handle (contents, &h);
  525. assert (h.base == 0);
  526. elts = h.elements;
  527. len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
  528. sz = scm_array_handle_uniform_element_bit_size (&h);
  529. if (sz >= 8 && ((sz % 8) == 0))
  530. byte_len = len * (sz / 8);
  531. else if (sz < 8)
  532. /* byte_len = ceil (len * sz / 8) */
  533. byte_len = (len * sz + 7) / 8;
  534. else
  535. /* an internal guile error, really */
  536. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  537. ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  538. memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
  539. scm_array_handle_release (&h);
  540. return ret;
  541. }
  542. #undef FUNC_NAME
  543. /* Operations on bytes and octets. */
  544. SCM_DEFINE (scm_bytevector_u8_ref, "bytevector-u8-ref", 2, 0, 0,
  545. (SCM bv, SCM index),
  546. "Return the octet located at @var{index} in @var{bv}.")
  547. #define FUNC_NAME s_scm_bytevector_u8_ref
  548. {
  549. INTEGER_NATIVE_REF (8, unsigned);
  550. }
  551. #undef FUNC_NAME
  552. SCM_DEFINE (scm_bytevector_s8_ref, "bytevector-s8-ref", 2, 0, 0,
  553. (SCM bv, SCM index),
  554. "Return the byte located at @var{index} in @var{bv}.")
  555. #define FUNC_NAME s_scm_bytevector_s8_ref
  556. {
  557. INTEGER_NATIVE_REF (8, signed);
  558. }
  559. #undef FUNC_NAME
  560. SCM_DEFINE (scm_bytevector_u8_set_x, "bytevector-u8-set!", 3, 0, 0,
  561. (SCM bv, SCM index, SCM value),
  562. "Return the octet located at @var{index} in @var{bv}.")
  563. #define FUNC_NAME s_scm_bytevector_u8_set_x
  564. {
  565. INTEGER_NATIVE_SET (8, unsigned);
  566. }
  567. #undef FUNC_NAME
  568. SCM_DEFINE (scm_bytevector_s8_set_x, "bytevector-s8-set!", 3, 0, 0,
  569. (SCM bv, SCM index, SCM value),
  570. "Return the octet located at @var{index} in @var{bv}.")
  571. #define FUNC_NAME s_scm_bytevector_s8_set_x
  572. {
  573. INTEGER_NATIVE_SET (8, signed);
  574. }
  575. #undef FUNC_NAME
  576. #undef OCTET_ACCESSOR_PROLOGUE
  577. SCM_DEFINE (scm_bytevector_to_u8_list, "bytevector->u8-list", 1, 0, 0,
  578. (SCM bv),
  579. "Return a newly allocated list of octets containing the "
  580. "contents of @var{bv}.")
  581. #define FUNC_NAME s_scm_bytevector_to_u8_list
  582. {
  583. SCM lst, pair;
  584. unsigned c_len, i;
  585. unsigned char *c_bv;
  586. SCM_VALIDATE_BYTEVECTOR (1, bv);
  587. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  588. c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  589. lst = scm_make_list (scm_from_uint (c_len), SCM_UNSPECIFIED);
  590. for (i = 0, pair = lst;
  591. i < c_len;
  592. i++, pair = SCM_CDR (pair))
  593. {
  594. SCM_SETCAR (pair, SCM_I_MAKINUM (c_bv[i]));
  595. }
  596. return lst;
  597. }
  598. #undef FUNC_NAME
  599. SCM_DEFINE (scm_u8_list_to_bytevector, "u8-list->bytevector", 1, 0, 0,
  600. (SCM lst),
  601. "Turn @var{lst}, a list of octets, into a bytevector.")
  602. #define FUNC_NAME s_scm_u8_list_to_bytevector
  603. {
  604. SCM bv, item;
  605. long c_len, i;
  606. unsigned char *c_bv;
  607. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len);
  608. bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  609. c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  610. for (i = 0; i < c_len; lst = SCM_CDR (lst), i++)
  611. {
  612. item = SCM_CAR (lst);
  613. if (SCM_LIKELY (SCM_I_INUMP (item)))
  614. {
  615. scm_t_signed_bits c_item;
  616. c_item = SCM_I_INUM (item);
  617. if (SCM_LIKELY ((c_item >= 0) && (c_item < 256)))
  618. c_bv[i] = (unsigned char) c_item;
  619. else
  620. goto type_error;
  621. }
  622. else
  623. goto type_error;
  624. }
  625. return bv;
  626. type_error:
  627. scm_wrong_type_arg (FUNC_NAME, 1, item);
  628. return SCM_BOOL_F;
  629. }
  630. #undef FUNC_NAME
  631. /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
  632. using (2^(SIZE * 8) - VALUE). */
  633. static inline void
  634. twos_complement (mpz_t value, size_t size)
  635. {
  636. unsigned long bit_count;
  637. /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
  638. checking on SIZE performed earlier. */
  639. bit_count = (unsigned long) size << 3UL;
  640. if (SCM_LIKELY (bit_count < sizeof (unsigned long)))
  641. mpz_ui_sub (value, 1UL << bit_count, value);
  642. else
  643. {
  644. mpz_t max;
  645. mpz_init (max);
  646. mpz_ui_pow_ui (max, 2, bit_count);
  647. mpz_sub (value, max, value);
  648. mpz_clear (max);
  649. }
  650. }
  651. static inline SCM
  652. bytevector_large_ref (const char *c_bv, size_t c_size, int signed_p,
  653. SCM endianness)
  654. {
  655. SCM result;
  656. mpz_t c_mpz;
  657. int c_endianness, negative_p = 0;
  658. if (signed_p)
  659. {
  660. if (scm_is_eq (endianness, scm_sym_big))
  661. negative_p = c_bv[0] & 0x80;
  662. else
  663. negative_p = c_bv[c_size - 1] & 0x80;
  664. }
  665. c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
  666. mpz_init (c_mpz);
  667. mpz_import (c_mpz, 1 /* 1 word */, 1 /* word order doesn't matter */,
  668. c_size /* word is C_SIZE-byte long */,
  669. c_endianness,
  670. 0 /* nails */, c_bv);
  671. if (signed_p && negative_p)
  672. {
  673. twos_complement (c_mpz, c_size);
  674. mpz_neg (c_mpz, c_mpz);
  675. }
  676. result = scm_from_mpz (c_mpz);
  677. mpz_clear (c_mpz); /* FIXME: Needed? */
  678. return result;
  679. }
  680. static inline int
  681. bytevector_large_set (char *c_bv, size_t c_size, int signed_p,
  682. SCM value, SCM endianness)
  683. {
  684. mpz_t c_mpz;
  685. int c_endianness, c_sign, err = 0;
  686. c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
  687. mpz_init (c_mpz);
  688. scm_to_mpz (value, c_mpz);
  689. c_sign = mpz_sgn (c_mpz);
  690. if (c_sign < 0)
  691. {
  692. if (SCM_LIKELY (signed_p))
  693. {
  694. mpz_neg (c_mpz, c_mpz);
  695. twos_complement (c_mpz, c_size);
  696. }
  697. else
  698. {
  699. err = -1;
  700. goto finish;
  701. }
  702. }
  703. if (c_sign == 0)
  704. /* Zero. */
  705. memset (c_bv, 0, c_size);
  706. else
  707. {
  708. size_t word_count, value_size;
  709. value_size = (mpz_sizeinbase (c_mpz, 2) + (8 * c_size)) / (8 * c_size);
  710. if (SCM_UNLIKELY (value_size > c_size))
  711. {
  712. err = -2;
  713. goto finish;
  714. }
  715. mpz_export (c_bv, &word_count, 1 /* word order doesn't matter */,
  716. c_size, c_endianness,
  717. 0 /* nails */, c_mpz);
  718. if (SCM_UNLIKELY (word_count != 1))
  719. /* Shouldn't happen since we already checked with VALUE_SIZE. */
  720. abort ();
  721. }
  722. finish:
  723. mpz_clear (c_mpz);
  724. return err;
  725. }
  726. #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
  727. unsigned long c_len, c_index, c_size; \
  728. char *c_bv; \
  729. \
  730. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  731. c_index = scm_to_ulong (index); \
  732. c_size = scm_to_ulong (size); \
  733. \
  734. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  735. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  736. \
  737. /* C_SIZE must have its 3 higher bits set to zero so that \
  738. multiplying it by 8 yields a number that fits in an \
  739. unsigned long. */ \
  740. if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
  741. scm_out_of_range (FUNC_NAME, size); \
  742. if (SCM_UNLIKELY (c_index + c_size > c_len)) \
  743. scm_out_of_range (FUNC_NAME, index);
  744. /* Template of an integer reference function. */
  745. #define GENERIC_INTEGER_REF(_sign) \
  746. SCM result; \
  747. \
  748. if (c_size < 3) \
  749. { \
  750. int swap; \
  751. _sign int value; \
  752. \
  753. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  754. switch (c_size) \
  755. { \
  756. case 1: \
  757. { \
  758. _sign char c_value8; \
  759. memcpy (&c_value8, c_bv, 1); \
  760. value = c_value8; \
  761. } \
  762. break; \
  763. case 2: \
  764. { \
  765. INT_TYPE (16, _sign) c_value16; \
  766. memcpy (&c_value16, c_bv, 2); \
  767. if (swap) \
  768. value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
  769. else \
  770. value = c_value16; \
  771. } \
  772. break; \
  773. default: \
  774. abort (); \
  775. } \
  776. \
  777. result = SCM_I_MAKINUM ((_sign int) value); \
  778. } \
  779. else \
  780. result = bytevector_large_ref ((char *) c_bv, \
  781. c_size, SIGNEDNESS (_sign), \
  782. endianness); \
  783. \
  784. return result;
  785. static inline SCM
  786. bytevector_signed_ref (const char *c_bv, size_t c_size, SCM endianness)
  787. {
  788. GENERIC_INTEGER_REF (signed);
  789. }
  790. static inline SCM
  791. bytevector_unsigned_ref (const char *c_bv, size_t c_size, SCM endianness)
  792. {
  793. GENERIC_INTEGER_REF (unsigned);
  794. }
  795. /* Template of an integer assignment function. */
  796. #define GENERIC_INTEGER_SET(_sign) \
  797. if (c_size < 3) \
  798. { \
  799. scm_t_signed_bits c_value; \
  800. \
  801. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  802. goto range_error; \
  803. \
  804. c_value = SCM_I_INUM (value); \
  805. switch (c_size) \
  806. { \
  807. case 1: \
  808. if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
  809. { \
  810. _sign char c_value8; \
  811. c_value8 = (_sign char) c_value; \
  812. memcpy (c_bv, &c_value8, 1); \
  813. } \
  814. else \
  815. goto range_error; \
  816. break; \
  817. \
  818. case 2: \
  819. if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
  820. { \
  821. int swap; \
  822. INT_TYPE (16, _sign) c_value16; \
  823. \
  824. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  825. \
  826. if (swap) \
  827. c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
  828. else \
  829. c_value16 = c_value; \
  830. \
  831. memcpy (c_bv, &c_value16, 2); \
  832. } \
  833. else \
  834. goto range_error; \
  835. break; \
  836. \
  837. default: \
  838. abort (); \
  839. } \
  840. } \
  841. else \
  842. { \
  843. int err; \
  844. \
  845. err = bytevector_large_set (c_bv, c_size, \
  846. SIGNEDNESS (_sign), \
  847. value, endianness); \
  848. if (err) \
  849. goto range_error; \
  850. } \
  851. \
  852. return; \
  853. \
  854. range_error: \
  855. scm_out_of_range (FUNC_NAME, value); \
  856. return;
  857. static inline void
  858. bytevector_signed_set (char *c_bv, size_t c_size,
  859. SCM value, SCM endianness,
  860. const char *func_name)
  861. #define FUNC_NAME func_name
  862. {
  863. GENERIC_INTEGER_SET (signed);
  864. }
  865. #undef FUNC_NAME
  866. static inline void
  867. bytevector_unsigned_set (char *c_bv, size_t c_size,
  868. SCM value, SCM endianness,
  869. const char *func_name)
  870. #define FUNC_NAME func_name
  871. {
  872. GENERIC_INTEGER_SET (unsigned);
  873. }
  874. #undef FUNC_NAME
  875. #undef GENERIC_INTEGER_SET
  876. #undef GENERIC_INTEGER_REF
  877. SCM_DEFINE (scm_bytevector_uint_ref, "bytevector-uint-ref", 4, 0, 0,
  878. (SCM bv, SCM index, SCM endianness, SCM size),
  879. "Return the @var{size}-octet long unsigned integer at index "
  880. "@var{index} in @var{bv}.")
  881. #define FUNC_NAME s_scm_bytevector_uint_ref
  882. {
  883. GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
  884. return (bytevector_unsigned_ref (&c_bv[c_index], c_size, endianness));
  885. }
  886. #undef FUNC_NAME
  887. SCM_DEFINE (scm_bytevector_sint_ref, "bytevector-sint-ref", 4, 0, 0,
  888. (SCM bv, SCM index, SCM endianness, SCM size),
  889. "Return the @var{size}-octet long unsigned integer at index "
  890. "@var{index} in @var{bv}.")
  891. #define FUNC_NAME s_scm_bytevector_sint_ref
  892. {
  893. GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
  894. return (bytevector_signed_ref (&c_bv[c_index], c_size, endianness));
  895. }
  896. #undef FUNC_NAME
  897. SCM_DEFINE (scm_bytevector_uint_set_x, "bytevector-uint-set!", 5, 0, 0,
  898. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  899. "Set the @var{size}-octet long unsigned integer at @var{index} "
  900. "to @var{value}.")
  901. #define FUNC_NAME s_scm_bytevector_uint_set_x
  902. {
  903. GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
  904. bytevector_unsigned_set (&c_bv[c_index], c_size, value, endianness,
  905. FUNC_NAME);
  906. return SCM_UNSPECIFIED;
  907. }
  908. #undef FUNC_NAME
  909. SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0,
  910. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  911. "Set the @var{size}-octet long signed integer at @var{index} "
  912. "to @var{value}.")
  913. #define FUNC_NAME s_scm_bytevector_sint_set_x
  914. {
  915. GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
  916. bytevector_signed_set (&c_bv[c_index], c_size, value, endianness,
  917. FUNC_NAME);
  918. return SCM_UNSPECIFIED;
  919. }
  920. #undef FUNC_NAME
  921. /* Operations on integers of arbitrary size. */
  922. #define INTEGERS_TO_LIST(_sign) \
  923. SCM lst, pair; \
  924. size_t i, c_len, c_size; \
  925. \
  926. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  927. SCM_VALIDATE_SYMBOL (2, endianness); \
  928. c_size = scm_to_uint (size); \
  929. \
  930. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  931. if (SCM_UNLIKELY (c_len == 0)) \
  932. lst = SCM_EOL; \
  933. else if (SCM_UNLIKELY (c_len < c_size)) \
  934. scm_out_of_range (FUNC_NAME, size); \
  935. else \
  936. { \
  937. const char *c_bv; \
  938. \
  939. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  940. \
  941. lst = scm_make_list (scm_from_uint (c_len / c_size), \
  942. SCM_UNSPECIFIED); \
  943. for (i = 0, pair = lst; \
  944. i <= c_len - c_size; \
  945. i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
  946. { \
  947. SCM_SETCAR (pair, \
  948. bytevector_ ## _sign ## _ref (c_bv, c_size, \
  949. endianness)); \
  950. } \
  951. } \
  952. \
  953. return lst;
  954. SCM_DEFINE (scm_bytevector_to_sint_list, "bytevector->sint-list",
  955. 3, 0, 0,
  956. (SCM bv, SCM endianness, SCM size),
  957. "Return a list of signed integers of @var{size} octets "
  958. "representing the contents of @var{bv}.")
  959. #define FUNC_NAME s_scm_bytevector_to_sint_list
  960. {
  961. INTEGERS_TO_LIST (signed);
  962. }
  963. #undef FUNC_NAME
  964. SCM_DEFINE (scm_bytevector_to_uint_list, "bytevector->uint-list",
  965. 3, 0, 0,
  966. (SCM bv, SCM endianness, SCM size),
  967. "Return a list of unsigned integers of @var{size} octets "
  968. "representing the contents of @var{bv}.")
  969. #define FUNC_NAME s_scm_bytevector_to_uint_list
  970. {
  971. INTEGERS_TO_LIST (unsigned);
  972. }
  973. #undef FUNC_NAME
  974. #undef INTEGER_TO_LIST
  975. #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
  976. SCM bv; \
  977. long c_len; \
  978. size_t c_size; \
  979. char *c_bv, *c_bv_ptr; \
  980. \
  981. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
  982. SCM_VALIDATE_SYMBOL (2, endianness); \
  983. c_size = scm_to_uint (size); \
  984. \
  985. if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
  986. scm_out_of_range (FUNC_NAME, size); \
  987. \
  988. bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  989. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  990. \
  991. for (c_bv_ptr = c_bv; \
  992. !scm_is_null (lst); \
  993. lst = SCM_CDR (lst), c_bv_ptr += c_size) \
  994. { \
  995. bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
  996. SCM_CAR (lst), endianness, \
  997. FUNC_NAME); \
  998. } \
  999. \
  1000. return bv;
  1001. SCM_DEFINE (scm_uint_list_to_bytevector, "uint-list->bytevector",
  1002. 3, 0, 0,
  1003. (SCM lst, SCM endianness, SCM size),
  1004. "Return a bytevector containing the unsigned integers "
  1005. "listed in @var{lst} and encoded on @var{size} octets "
  1006. "according to @var{endianness}.")
  1007. #define FUNC_NAME s_scm_uint_list_to_bytevector
  1008. {
  1009. INTEGER_LIST_TO_BYTEVECTOR (unsigned);
  1010. }
  1011. #undef FUNC_NAME
  1012. SCM_DEFINE (scm_sint_list_to_bytevector, "sint-list->bytevector",
  1013. 3, 0, 0,
  1014. (SCM lst, SCM endianness, SCM size),
  1015. "Return a bytevector containing the signed integers "
  1016. "listed in @var{lst} and encoded on @var{size} octets "
  1017. "according to @var{endianness}.")
  1018. #define FUNC_NAME s_scm_sint_list_to_bytevector
  1019. {
  1020. INTEGER_LIST_TO_BYTEVECTOR (signed);
  1021. }
  1022. #undef FUNC_NAME
  1023. #undef INTEGER_LIST_TO_BYTEVECTOR
  1024. /* Operations on 16-bit integers. */
  1025. SCM_DEFINE (scm_bytevector_u16_ref, "bytevector-u16-ref",
  1026. 3, 0, 0,
  1027. (SCM bv, SCM index, SCM endianness),
  1028. "Return the unsigned 16-bit integer from @var{bv} at "
  1029. "@var{index}.")
  1030. #define FUNC_NAME s_scm_bytevector_u16_ref
  1031. {
  1032. INTEGER_REF (16, unsigned);
  1033. }
  1034. #undef FUNC_NAME
  1035. SCM_DEFINE (scm_bytevector_s16_ref, "bytevector-s16-ref",
  1036. 3, 0, 0,
  1037. (SCM bv, SCM index, SCM endianness),
  1038. "Return the signed 16-bit integer from @var{bv} at "
  1039. "@var{index}.")
  1040. #define FUNC_NAME s_scm_bytevector_s16_ref
  1041. {
  1042. INTEGER_REF (16, signed);
  1043. }
  1044. #undef FUNC_NAME
  1045. SCM_DEFINE (scm_bytevector_u16_native_ref, "bytevector-u16-native-ref",
  1046. 2, 0, 0,
  1047. (SCM bv, SCM index),
  1048. "Return the unsigned 16-bit integer from @var{bv} at "
  1049. "@var{index} using the native endianness.")
  1050. #define FUNC_NAME s_scm_bytevector_u16_native_ref
  1051. {
  1052. INTEGER_NATIVE_REF (16, unsigned);
  1053. }
  1054. #undef FUNC_NAME
  1055. SCM_DEFINE (scm_bytevector_s16_native_ref, "bytevector-s16-native-ref",
  1056. 2, 0, 0,
  1057. (SCM bv, SCM index),
  1058. "Return the unsigned 16-bit integer from @var{bv} at "
  1059. "@var{index} using the native endianness.")
  1060. #define FUNC_NAME s_scm_bytevector_s16_native_ref
  1061. {
  1062. INTEGER_NATIVE_REF (16, signed);
  1063. }
  1064. #undef FUNC_NAME
  1065. SCM_DEFINE (scm_bytevector_u16_set_x, "bytevector-u16-set!",
  1066. 4, 0, 0,
  1067. (SCM bv, SCM index, SCM value, SCM endianness),
  1068. "Store @var{value} in @var{bv} at @var{index} according to "
  1069. "@var{endianness}.")
  1070. #define FUNC_NAME s_scm_bytevector_u16_set_x
  1071. {
  1072. INTEGER_SET (16, unsigned);
  1073. }
  1074. #undef FUNC_NAME
  1075. SCM_DEFINE (scm_bytevector_s16_set_x, "bytevector-s16-set!",
  1076. 4, 0, 0,
  1077. (SCM bv, SCM index, SCM value, SCM endianness),
  1078. "Store @var{value} in @var{bv} at @var{index} according to "
  1079. "@var{endianness}.")
  1080. #define FUNC_NAME s_scm_bytevector_s16_set_x
  1081. {
  1082. INTEGER_SET (16, signed);
  1083. }
  1084. #undef FUNC_NAME
  1085. SCM_DEFINE (scm_bytevector_u16_native_set_x, "bytevector-u16-native-set!",
  1086. 3, 0, 0,
  1087. (SCM bv, SCM index, SCM value),
  1088. "Store the unsigned integer @var{value} at index @var{index} "
  1089. "of @var{bv} using the native endianness.")
  1090. #define FUNC_NAME s_scm_bytevector_u16_native_set_x
  1091. {
  1092. INTEGER_NATIVE_SET (16, unsigned);
  1093. }
  1094. #undef FUNC_NAME
  1095. SCM_DEFINE (scm_bytevector_s16_native_set_x, "bytevector-s16-native-set!",
  1096. 3, 0, 0,
  1097. (SCM bv, SCM index, SCM value),
  1098. "Store the signed integer @var{value} at index @var{index} "
  1099. "of @var{bv} using the native endianness.")
  1100. #define FUNC_NAME s_scm_bytevector_s16_native_set_x
  1101. {
  1102. INTEGER_NATIVE_SET (16, signed);
  1103. }
  1104. #undef FUNC_NAME
  1105. /* Operations on 32-bit integers. */
  1106. /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
  1107. arbitrary 32-bit integers. Thus we fall back to using the
  1108. `large_{ref,set}' variants on 32-bit machines. */
  1109. #define LARGE_INTEGER_REF(_len, _sign) \
  1110. INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
  1111. SCM_VALIDATE_SYMBOL (3, endianness); \
  1112. \
  1113. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1114. SIGNEDNESS (_sign), endianness));
  1115. #define LARGE_INTEGER_SET(_len, _sign) \
  1116. int err; \
  1117. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  1118. SCM_VALIDATE_SYMBOL (4, endianness); \
  1119. \
  1120. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1121. SIGNEDNESS (_sign), value, endianness); \
  1122. if (SCM_UNLIKELY (err)) \
  1123. scm_out_of_range (FUNC_NAME, value); \
  1124. \
  1125. return SCM_UNSPECIFIED;
  1126. #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
  1127. INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
  1128. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1129. SIGNEDNESS (_sign), scm_i_native_endianness));
  1130. #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
  1131. int err; \
  1132. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  1133. \
  1134. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1135. SIGNEDNESS (_sign), value, \
  1136. scm_i_native_endianness); \
  1137. if (SCM_UNLIKELY (err)) \
  1138. scm_out_of_range (FUNC_NAME, value); \
  1139. \
  1140. return SCM_UNSPECIFIED;
  1141. SCM_DEFINE (scm_bytevector_u32_ref, "bytevector-u32-ref",
  1142. 3, 0, 0,
  1143. (SCM bv, SCM index, SCM endianness),
  1144. "Return the unsigned 32-bit integer from @var{bv} at "
  1145. "@var{index}.")
  1146. #define FUNC_NAME s_scm_bytevector_u32_ref
  1147. {
  1148. #if SIZEOF_VOID_P > 4
  1149. INTEGER_REF (32, unsigned);
  1150. #else
  1151. LARGE_INTEGER_REF (32, unsigned);
  1152. #endif
  1153. }
  1154. #undef FUNC_NAME
  1155. SCM_DEFINE (scm_bytevector_s32_ref, "bytevector-s32-ref",
  1156. 3, 0, 0,
  1157. (SCM bv, SCM index, SCM endianness),
  1158. "Return the signed 32-bit integer from @var{bv} at "
  1159. "@var{index}.")
  1160. #define FUNC_NAME s_scm_bytevector_s32_ref
  1161. {
  1162. #if SIZEOF_VOID_P > 4
  1163. INTEGER_REF (32, signed);
  1164. #else
  1165. LARGE_INTEGER_REF (32, signed);
  1166. #endif
  1167. }
  1168. #undef FUNC_NAME
  1169. SCM_DEFINE (scm_bytevector_u32_native_ref, "bytevector-u32-native-ref",
  1170. 2, 0, 0,
  1171. (SCM bv, SCM index),
  1172. "Return the unsigned 32-bit integer from @var{bv} at "
  1173. "@var{index} using the native endianness.")
  1174. #define FUNC_NAME s_scm_bytevector_u32_native_ref
  1175. {
  1176. #if SIZEOF_VOID_P > 4
  1177. INTEGER_NATIVE_REF (32, unsigned);
  1178. #else
  1179. LARGE_INTEGER_NATIVE_REF (32, unsigned);
  1180. #endif
  1181. }
  1182. #undef FUNC_NAME
  1183. SCM_DEFINE (scm_bytevector_s32_native_ref, "bytevector-s32-native-ref",
  1184. 2, 0, 0,
  1185. (SCM bv, SCM index),
  1186. "Return the unsigned 32-bit integer from @var{bv} at "
  1187. "@var{index} using the native endianness.")
  1188. #define FUNC_NAME s_scm_bytevector_s32_native_ref
  1189. {
  1190. #if SIZEOF_VOID_P > 4
  1191. INTEGER_NATIVE_REF (32, signed);
  1192. #else
  1193. LARGE_INTEGER_NATIVE_REF (32, signed);
  1194. #endif
  1195. }
  1196. #undef FUNC_NAME
  1197. SCM_DEFINE (scm_bytevector_u32_set_x, "bytevector-u32-set!",
  1198. 4, 0, 0,
  1199. (SCM bv, SCM index, SCM value, SCM endianness),
  1200. "Store @var{value} in @var{bv} at @var{index} according to "
  1201. "@var{endianness}.")
  1202. #define FUNC_NAME s_scm_bytevector_u32_set_x
  1203. {
  1204. #if SIZEOF_VOID_P > 4
  1205. INTEGER_SET (32, unsigned);
  1206. #else
  1207. LARGE_INTEGER_SET (32, unsigned);
  1208. #endif
  1209. }
  1210. #undef FUNC_NAME
  1211. SCM_DEFINE (scm_bytevector_s32_set_x, "bytevector-s32-set!",
  1212. 4, 0, 0,
  1213. (SCM bv, SCM index, SCM value, SCM endianness),
  1214. "Store @var{value} in @var{bv} at @var{index} according to "
  1215. "@var{endianness}.")
  1216. #define FUNC_NAME s_scm_bytevector_s32_set_x
  1217. {
  1218. #if SIZEOF_VOID_P > 4
  1219. INTEGER_SET (32, signed);
  1220. #else
  1221. LARGE_INTEGER_SET (32, signed);
  1222. #endif
  1223. }
  1224. #undef FUNC_NAME
  1225. SCM_DEFINE (scm_bytevector_u32_native_set_x, "bytevector-u32-native-set!",
  1226. 3, 0, 0,
  1227. (SCM bv, SCM index, SCM value),
  1228. "Store the unsigned integer @var{value} at index @var{index} "
  1229. "of @var{bv} using the native endianness.")
  1230. #define FUNC_NAME s_scm_bytevector_u32_native_set_x
  1231. {
  1232. #if SIZEOF_VOID_P > 4
  1233. INTEGER_NATIVE_SET (32, unsigned);
  1234. #else
  1235. LARGE_INTEGER_NATIVE_SET (32, unsigned);
  1236. #endif
  1237. }
  1238. #undef FUNC_NAME
  1239. SCM_DEFINE (scm_bytevector_s32_native_set_x, "bytevector-s32-native-set!",
  1240. 3, 0, 0,
  1241. (SCM bv, SCM index, SCM value),
  1242. "Store the signed integer @var{value} at index @var{index} "
  1243. "of @var{bv} using the native endianness.")
  1244. #define FUNC_NAME s_scm_bytevector_s32_native_set_x
  1245. {
  1246. #if SIZEOF_VOID_P > 4
  1247. INTEGER_NATIVE_SET (32, signed);
  1248. #else
  1249. LARGE_INTEGER_NATIVE_SET (32, signed);
  1250. #endif
  1251. }
  1252. #undef FUNC_NAME
  1253. /* Operations on 64-bit integers. */
  1254. /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
  1255. SCM_DEFINE (scm_bytevector_u64_ref, "bytevector-u64-ref",
  1256. 3, 0, 0,
  1257. (SCM bv, SCM index, SCM endianness),
  1258. "Return the unsigned 64-bit integer from @var{bv} at "
  1259. "@var{index}.")
  1260. #define FUNC_NAME s_scm_bytevector_u64_ref
  1261. {
  1262. LARGE_INTEGER_REF (64, unsigned);
  1263. }
  1264. #undef FUNC_NAME
  1265. SCM_DEFINE (scm_bytevector_s64_ref, "bytevector-s64-ref",
  1266. 3, 0, 0,
  1267. (SCM bv, SCM index, SCM endianness),
  1268. "Return the signed 64-bit integer from @var{bv} at "
  1269. "@var{index}.")
  1270. #define FUNC_NAME s_scm_bytevector_s64_ref
  1271. {
  1272. LARGE_INTEGER_REF (64, signed);
  1273. }
  1274. #undef FUNC_NAME
  1275. SCM_DEFINE (scm_bytevector_u64_native_ref, "bytevector-u64-native-ref",
  1276. 2, 0, 0,
  1277. (SCM bv, SCM index),
  1278. "Return the unsigned 64-bit integer from @var{bv} at "
  1279. "@var{index} using the native endianness.")
  1280. #define FUNC_NAME s_scm_bytevector_u64_native_ref
  1281. {
  1282. LARGE_INTEGER_NATIVE_REF (64, unsigned);
  1283. }
  1284. #undef FUNC_NAME
  1285. SCM_DEFINE (scm_bytevector_s64_native_ref, "bytevector-s64-native-ref",
  1286. 2, 0, 0,
  1287. (SCM bv, SCM index),
  1288. "Return the unsigned 64-bit integer from @var{bv} at "
  1289. "@var{index} using the native endianness.")
  1290. #define FUNC_NAME s_scm_bytevector_s64_native_ref
  1291. {
  1292. LARGE_INTEGER_NATIVE_REF (64, signed);
  1293. }
  1294. #undef FUNC_NAME
  1295. SCM_DEFINE (scm_bytevector_u64_set_x, "bytevector-u64-set!",
  1296. 4, 0, 0,
  1297. (SCM bv, SCM index, SCM value, SCM endianness),
  1298. "Store @var{value} in @var{bv} at @var{index} according to "
  1299. "@var{endianness}.")
  1300. #define FUNC_NAME s_scm_bytevector_u64_set_x
  1301. {
  1302. LARGE_INTEGER_SET (64, unsigned);
  1303. }
  1304. #undef FUNC_NAME
  1305. SCM_DEFINE (scm_bytevector_s64_set_x, "bytevector-s64-set!",
  1306. 4, 0, 0,
  1307. (SCM bv, SCM index, SCM value, SCM endianness),
  1308. "Store @var{value} in @var{bv} at @var{index} according to "
  1309. "@var{endianness}.")
  1310. #define FUNC_NAME s_scm_bytevector_s64_set_x
  1311. {
  1312. LARGE_INTEGER_SET (64, signed);
  1313. }
  1314. #undef FUNC_NAME
  1315. SCM_DEFINE (scm_bytevector_u64_native_set_x, "bytevector-u64-native-set!",
  1316. 3, 0, 0,
  1317. (SCM bv, SCM index, SCM value),
  1318. "Store the unsigned integer @var{value} at index @var{index} "
  1319. "of @var{bv} using the native endianness.")
  1320. #define FUNC_NAME s_scm_bytevector_u64_native_set_x
  1321. {
  1322. LARGE_INTEGER_NATIVE_SET (64, unsigned);
  1323. }
  1324. #undef FUNC_NAME
  1325. SCM_DEFINE (scm_bytevector_s64_native_set_x, "bytevector-s64-native-set!",
  1326. 3, 0, 0,
  1327. (SCM bv, SCM index, SCM value),
  1328. "Store the signed integer @var{value} at index @var{index} "
  1329. "of @var{bv} using the native endianness.")
  1330. #define FUNC_NAME s_scm_bytevector_s64_native_set_x
  1331. {
  1332. LARGE_INTEGER_NATIVE_SET (64, signed);
  1333. }
  1334. #undef FUNC_NAME
  1335. /* Operations on IEEE-754 numbers. */
  1336. /* There are two possible word endians, visible in glibc's <ieee754.h>.
  1337. However, in R6RS, when the endianness is `little', little endian is
  1338. assumed for both the byte order and the word order. This is clear from
  1339. Section 2.1 of R6RS-lib (in response to
  1340. http://www.r6rs.org/formal-comments/comment-187.txt). */
  1341. /* Convert to/from a floating-point number with different endianness. This
  1342. method is probably not the most efficient but it should be portable. */
  1343. static inline void
  1344. float_to_foreign_endianness (union scm_ieee754_float *target,
  1345. float source)
  1346. {
  1347. union scm_ieee754_float src;
  1348. src.f = source;
  1349. #ifdef WORDS_BIGENDIAN
  1350. /* Assuming little endian for both byte and word order. */
  1351. target->little_endian.negative = src.big_endian.negative;
  1352. target->little_endian.exponent = src.big_endian.exponent;
  1353. target->little_endian.mantissa = src.big_endian.mantissa;
  1354. #else
  1355. target->big_endian.negative = src.little_endian.negative;
  1356. target->big_endian.exponent = src.little_endian.exponent;
  1357. target->big_endian.mantissa = src.little_endian.mantissa;
  1358. #endif
  1359. }
  1360. static inline float
  1361. float_from_foreign_endianness (const union scm_ieee754_float *source)
  1362. {
  1363. union scm_ieee754_float result;
  1364. #ifdef WORDS_BIGENDIAN
  1365. /* Assuming little endian for both byte and word order. */
  1366. result.big_endian.negative = source->little_endian.negative;
  1367. result.big_endian.exponent = source->little_endian.exponent;
  1368. result.big_endian.mantissa = source->little_endian.mantissa;
  1369. #else
  1370. result.little_endian.negative = source->big_endian.negative;
  1371. result.little_endian.exponent = source->big_endian.exponent;
  1372. result.little_endian.mantissa = source->big_endian.mantissa;
  1373. #endif
  1374. return (result.f);
  1375. }
  1376. static inline void
  1377. double_to_foreign_endianness (union scm_ieee754_double *target,
  1378. double source)
  1379. {
  1380. union scm_ieee754_double src;
  1381. src.d = source;
  1382. #ifdef WORDS_BIGENDIAN
  1383. /* Assuming little endian for both byte and word order. */
  1384. target->little_little_endian.negative = src.big_endian.negative;
  1385. target->little_little_endian.exponent = src.big_endian.exponent;
  1386. target->little_little_endian.mantissa0 = src.big_endian.mantissa0;
  1387. target->little_little_endian.mantissa1 = src.big_endian.mantissa1;
  1388. #else
  1389. target->big_endian.negative = src.little_little_endian.negative;
  1390. target->big_endian.exponent = src.little_little_endian.exponent;
  1391. target->big_endian.mantissa0 = src.little_little_endian.mantissa0;
  1392. target->big_endian.mantissa1 = src.little_little_endian.mantissa1;
  1393. #endif
  1394. }
  1395. static inline double
  1396. double_from_foreign_endianness (const union scm_ieee754_double *source)
  1397. {
  1398. union scm_ieee754_double result;
  1399. #ifdef WORDS_BIGENDIAN
  1400. /* Assuming little endian for both byte and word order. */
  1401. result.big_endian.negative = source->little_little_endian.negative;
  1402. result.big_endian.exponent = source->little_little_endian.exponent;
  1403. result.big_endian.mantissa0 = source->little_little_endian.mantissa0;
  1404. result.big_endian.mantissa1 = source->little_little_endian.mantissa1;
  1405. #else
  1406. result.little_little_endian.negative = source->big_endian.negative;
  1407. result.little_little_endian.exponent = source->big_endian.exponent;
  1408. result.little_little_endian.mantissa0 = source->big_endian.mantissa0;
  1409. result.little_little_endian.mantissa1 = source->big_endian.mantissa1;
  1410. #endif
  1411. return (result.d);
  1412. }
  1413. /* Template macros to abstract over doubles and floats.
  1414. XXX: Guile can only convert to/from doubles. */
  1415. #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
  1416. #define IEEE754_TO_SCM(_c_type) scm_from_double
  1417. #define IEEE754_FROM_SCM(_c_type) scm_to_double
  1418. #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
  1419. _c_type ## _from_foreign_endianness
  1420. #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
  1421. _c_type ## _to_foreign_endianness
  1422. /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
  1423. #define VALIDATE_REAL(pos, v) \
  1424. do { \
  1425. SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
  1426. } while (0)
  1427. /* Templace getters and setters. */
  1428. #define IEEE754_ACCESSOR_PROLOGUE(_type) \
  1429. INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
  1430. #define IEEE754_REF(_type) \
  1431. _type c_result; \
  1432. \
  1433. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1434. SCM_VALIDATE_SYMBOL (3, endianness); \
  1435. \
  1436. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1437. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1438. else \
  1439. { \
  1440. IEEE754_UNION (_type) c_raw; \
  1441. \
  1442. memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
  1443. c_result = \
  1444. IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
  1445. } \
  1446. \
  1447. return (IEEE754_TO_SCM (_type) (c_result));
  1448. #define IEEE754_NATIVE_REF(_type) \
  1449. _type c_result; \
  1450. \
  1451. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1452. \
  1453. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1454. return (IEEE754_TO_SCM (_type) (c_result));
  1455. #define IEEE754_SET(_type) \
  1456. _type c_value; \
  1457. \
  1458. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1459. VALIDATE_REAL (3, value); \
  1460. SCM_VALIDATE_SYMBOL (4, endianness); \
  1461. c_value = IEEE754_FROM_SCM (_type) (value); \
  1462. \
  1463. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1464. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1465. else \
  1466. { \
  1467. IEEE754_UNION (_type) c_raw; \
  1468. \
  1469. IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
  1470. memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
  1471. } \
  1472. \
  1473. return SCM_UNSPECIFIED;
  1474. #define IEEE754_NATIVE_SET(_type) \
  1475. _type c_value; \
  1476. \
  1477. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1478. VALIDATE_REAL (3, value); \
  1479. c_value = IEEE754_FROM_SCM (_type) (value); \
  1480. \
  1481. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1482. return SCM_UNSPECIFIED;
  1483. /* Single precision. */
  1484. SCM_DEFINE (scm_bytevector_ieee_single_ref,
  1485. "bytevector-ieee-single-ref",
  1486. 3, 0, 0,
  1487. (SCM bv, SCM index, SCM endianness),
  1488. "Return the IEEE-754 single from @var{bv} at "
  1489. "@var{index}.")
  1490. #define FUNC_NAME s_scm_bytevector_ieee_single_ref
  1491. {
  1492. IEEE754_REF (float);
  1493. }
  1494. #undef FUNC_NAME
  1495. SCM_DEFINE (scm_bytevector_ieee_single_native_ref,
  1496. "bytevector-ieee-single-native-ref",
  1497. 2, 0, 0,
  1498. (SCM bv, SCM index),
  1499. "Return the IEEE-754 single from @var{bv} at "
  1500. "@var{index} using the native endianness.")
  1501. #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
  1502. {
  1503. IEEE754_NATIVE_REF (float);
  1504. }
  1505. #undef FUNC_NAME
  1506. SCM_DEFINE (scm_bytevector_ieee_single_set_x,
  1507. "bytevector-ieee-single-set!",
  1508. 4, 0, 0,
  1509. (SCM bv, SCM index, SCM value, SCM endianness),
  1510. "Store real @var{value} in @var{bv} at @var{index} according to "
  1511. "@var{endianness}.")
  1512. #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
  1513. {
  1514. IEEE754_SET (float);
  1515. }
  1516. #undef FUNC_NAME
  1517. SCM_DEFINE (scm_bytevector_ieee_single_native_set_x,
  1518. "bytevector-ieee-single-native-set!",
  1519. 3, 0, 0,
  1520. (SCM bv, SCM index, SCM value),
  1521. "Store the real @var{value} at index @var{index} "
  1522. "of @var{bv} using the native endianness.")
  1523. #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
  1524. {
  1525. IEEE754_NATIVE_SET (float);
  1526. }
  1527. #undef FUNC_NAME
  1528. /* Double precision. */
  1529. SCM_DEFINE (scm_bytevector_ieee_double_ref,
  1530. "bytevector-ieee-double-ref",
  1531. 3, 0, 0,
  1532. (SCM bv, SCM index, SCM endianness),
  1533. "Return the IEEE-754 double from @var{bv} at "
  1534. "@var{index}.")
  1535. #define FUNC_NAME s_scm_bytevector_ieee_double_ref
  1536. {
  1537. IEEE754_REF (double);
  1538. }
  1539. #undef FUNC_NAME
  1540. SCM_DEFINE (scm_bytevector_ieee_double_native_ref,
  1541. "bytevector-ieee-double-native-ref",
  1542. 2, 0, 0,
  1543. (SCM bv, SCM index),
  1544. "Return the IEEE-754 double from @var{bv} at "
  1545. "@var{index} using the native endianness.")
  1546. #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
  1547. {
  1548. IEEE754_NATIVE_REF (double);
  1549. }
  1550. #undef FUNC_NAME
  1551. SCM_DEFINE (scm_bytevector_ieee_double_set_x,
  1552. "bytevector-ieee-double-set!",
  1553. 4, 0, 0,
  1554. (SCM bv, SCM index, SCM value, SCM endianness),
  1555. "Store real @var{value} in @var{bv} at @var{index} according to "
  1556. "@var{endianness}.")
  1557. #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
  1558. {
  1559. IEEE754_SET (double);
  1560. }
  1561. #undef FUNC_NAME
  1562. SCM_DEFINE (scm_bytevector_ieee_double_native_set_x,
  1563. "bytevector-ieee-double-native-set!",
  1564. 3, 0, 0,
  1565. (SCM bv, SCM index, SCM value),
  1566. "Store the real @var{value} at index @var{index} "
  1567. "of @var{bv} using the native endianness.")
  1568. #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
  1569. {
  1570. IEEE754_NATIVE_SET (double);
  1571. }
  1572. #undef FUNC_NAME
  1573. #undef IEEE754_UNION
  1574. #undef IEEE754_TO_SCM
  1575. #undef IEEE754_FROM_SCM
  1576. #undef IEEE754_FROM_FOREIGN_ENDIANNESS
  1577. #undef IEEE754_TO_FOREIGN_ENDIANNESS
  1578. #undef IEEE754_REF
  1579. #undef IEEE754_NATIVE_REF
  1580. #undef IEEE754_SET
  1581. #undef IEEE754_NATIVE_SET
  1582. /* Operations on strings. */
  1583. /* Produce a function that returns the length of a UTF-encoded string. */
  1584. #define UTF_STRLEN_FUNCTION(_utf_width) \
  1585. static inline size_t \
  1586. utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
  1587. { \
  1588. size_t len = 0; \
  1589. const uint ## _utf_width ## _t *ptr; \
  1590. for (ptr = str; \
  1591. *ptr != 0; \
  1592. ptr++) \
  1593. { \
  1594. len++; \
  1595. } \
  1596. \
  1597. return (len * ((_utf_width) / 8)); \
  1598. }
  1599. UTF_STRLEN_FUNCTION (8)
  1600. /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
  1601. #define UTF_STRLEN(_utf_width, _str) \
  1602. utf ## _utf_width ## _strlen (_str)
  1603. /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
  1604. ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
  1605. encoding name). */
  1606. static inline void
  1607. utf_encoding_name (char *name, size_t utf_width, SCM endianness)
  1608. {
  1609. strcpy (name, "UTF-");
  1610. strcat (name, ((utf_width == 8)
  1611. ? "8"
  1612. : ((utf_width == 16)
  1613. ? "16"
  1614. : ((utf_width == 32)
  1615. ? "32"
  1616. : "??"))));
  1617. strcat (name,
  1618. ((scm_is_eq (endianness, scm_sym_big))
  1619. ? "BE"
  1620. : ((scm_is_eq (endianness, scm_sym_little))
  1621. ? "LE"
  1622. : "unknown")));
  1623. }
  1624. /* Maximum length of a UTF encoding name. */
  1625. #define MAX_UTF_ENCODING_NAME_LEN 16
  1626. /* Produce the body of a `string->utf' function. */
  1627. #define STRING_TO_UTF(_utf_width) \
  1628. SCM utf; \
  1629. int err; \
  1630. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1631. char *c_utf = NULL; \
  1632. size_t c_strlen, c_utf_len = 0; \
  1633. \
  1634. SCM_VALIDATE_STRING (1, str); \
  1635. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1636. endianness = scm_sym_big; \
  1637. else \
  1638. SCM_VALIDATE_SYMBOL (2, endianness); \
  1639. \
  1640. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1641. \
  1642. c_strlen = scm_i_string_length (str); \
  1643. if (scm_i_is_narrow_string (str)) \
  1644. { \
  1645. err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
  1646. "ISO-8859-1", c_utf_name, \
  1647. iconveh_question_mark, NULL, \
  1648. &c_utf, &c_utf_len); \
  1649. if (SCM_UNLIKELY (err)) \
  1650. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1651. scm_list_1 (str), err); \
  1652. } \
  1653. else \
  1654. { \
  1655. const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
  1656. c_utf = u32_conv_to_encoding (c_utf_name, \
  1657. iconveh_question_mark, \
  1658. (scm_t_uint32 *) wbuf, \
  1659. c_strlen, NULL, NULL, &c_utf_len); \
  1660. if (SCM_UNLIKELY (c_utf == NULL)) \
  1661. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1662. scm_list_1 (str), errno); \
  1663. } \
  1664. scm_dynwind_begin (0); \
  1665. scm_dynwind_free (c_utf); \
  1666. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  1667. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
  1668. scm_dynwind_end (); \
  1669. \
  1670. return (utf);
  1671. SCM_DEFINE (scm_string_to_utf8, "string->utf8",
  1672. 1, 0, 0,
  1673. (SCM str),
  1674. "Return a newly allocated bytevector that contains the UTF-8 "
  1675. "encoding of @var{str}.")
  1676. #define FUNC_NAME s_scm_string_to_utf8
  1677. {
  1678. SCM utf;
  1679. uint8_t *c_utf;
  1680. size_t c_strlen, c_utf_len = 0;
  1681. SCM_VALIDATE_STRING (1, str);
  1682. c_strlen = scm_i_string_length (str);
  1683. if (scm_i_is_narrow_string (str))
  1684. c_utf = u8_conv_from_encoding ("ISO-8859-1", iconveh_question_mark,
  1685. scm_i_string_chars (str), c_strlen,
  1686. NULL, NULL, &c_utf_len);
  1687. else
  1688. {
  1689. const scm_t_wchar *wbuf = scm_i_string_wide_chars (str);
  1690. c_utf = u32_to_u8 ((const uint32_t *) wbuf, c_strlen, NULL, &c_utf_len);
  1691. }
  1692. if (SCM_UNLIKELY (c_utf == NULL))
  1693. scm_syserror (FUNC_NAME);
  1694. else
  1695. {
  1696. scm_dynwind_begin (0);
  1697. scm_dynwind_free (c_utf);
  1698. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1699. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len);
  1700. scm_dynwind_end ();
  1701. }
  1702. return (utf);
  1703. }
  1704. #undef FUNC_NAME
  1705. SCM_DEFINE (scm_string_to_utf16, "string->utf16",
  1706. 1, 1, 0,
  1707. (SCM str, SCM endianness),
  1708. "Return a newly allocated bytevector that contains the UTF-16 "
  1709. "encoding of @var{str}.")
  1710. #define FUNC_NAME s_scm_string_to_utf16
  1711. {
  1712. STRING_TO_UTF (16);
  1713. }
  1714. #undef FUNC_NAME
  1715. SCM_DEFINE (scm_string_to_utf32, "string->utf32",
  1716. 1, 1, 0,
  1717. (SCM str, SCM endianness),
  1718. "Return a newly allocated bytevector that contains the UTF-32 "
  1719. "encoding of @var{str}.")
  1720. #define FUNC_NAME s_scm_string_to_utf32
  1721. {
  1722. STRING_TO_UTF (32);
  1723. }
  1724. #undef FUNC_NAME
  1725. /* Produce the body of a function that converts a UTF-encoded bytevector to a
  1726. string. */
  1727. #define UTF_TO_STRING(_utf_width) \
  1728. SCM str = SCM_BOOL_F; \
  1729. int err; \
  1730. char *c_str = NULL; \
  1731. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1732. char *c_utf; \
  1733. size_t c_strlen = 0, c_utf_len = 0; \
  1734. \
  1735. SCM_VALIDATE_BYTEVECTOR (1, utf); \
  1736. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1737. endianness = scm_sym_big; \
  1738. else \
  1739. SCM_VALIDATE_SYMBOL (2, endianness); \
  1740. \
  1741. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
  1742. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
  1743. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1744. \
  1745. err = mem_iconveh (c_utf, c_utf_len, \
  1746. c_utf_name, "UTF-8", \
  1747. iconveh_question_mark, NULL, \
  1748. &c_str, &c_strlen); \
  1749. if (SCM_UNLIKELY (err)) \
  1750. scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
  1751. scm_list_1 (utf), err); \
  1752. else \
  1753. { \
  1754. str = scm_from_stringn (c_str, c_strlen, "UTF-8", \
  1755. SCM_FAILED_CONVERSION_ERROR); \
  1756. free (c_str); \
  1757. } \
  1758. return (str);
  1759. SCM_DEFINE (scm_utf8_to_string, "utf8->string",
  1760. 1, 0, 0,
  1761. (SCM utf),
  1762. "Return a newly allocate string that contains from the UTF-8-"
  1763. "encoded contents of bytevector @var{utf}.")
  1764. #define FUNC_NAME s_scm_utf8_to_string
  1765. {
  1766. SCM str;
  1767. const char *c_utf;
  1768. size_t c_utf_len = 0;
  1769. SCM_VALIDATE_BYTEVECTOR (1, utf);
  1770. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf);
  1771. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf);
  1772. str = scm_from_stringn (c_utf, c_utf_len, "UTF-8",
  1773. SCM_FAILED_CONVERSION_ERROR);
  1774. return (str);
  1775. }
  1776. #undef FUNC_NAME
  1777. SCM_DEFINE (scm_utf16_to_string, "utf16->string",
  1778. 1, 1, 0,
  1779. (SCM utf, SCM endianness),
  1780. "Return a newly allocate string that contains from the UTF-16-"
  1781. "encoded contents of bytevector @var{utf}.")
  1782. #define FUNC_NAME s_scm_utf16_to_string
  1783. {
  1784. UTF_TO_STRING (16);
  1785. }
  1786. #undef FUNC_NAME
  1787. SCM_DEFINE (scm_utf32_to_string, "utf32->string",
  1788. 1, 1, 0,
  1789. (SCM utf, SCM endianness),
  1790. "Return a newly allocate string that contains from the UTF-32-"
  1791. "encoded contents of bytevector @var{utf}.")
  1792. #define FUNC_NAME s_scm_utf32_to_string
  1793. {
  1794. UTF_TO_STRING (32);
  1795. }
  1796. #undef FUNC_NAME
  1797. /* Bytevectors as generalized vectors & arrays. */
  1798. static SCM
  1799. bytevector_ref_c32 (SCM bv, SCM idx)
  1800. { /* FIXME add some checks */
  1801. const float *contents = (const float*)SCM_BYTEVECTOR_CONTENTS (bv);
  1802. size_t i = scm_to_size_t (idx);
  1803. return scm_c_make_rectangular (contents[i/4], contents[i/4 + 1]);
  1804. }
  1805. static SCM
  1806. bytevector_ref_c64 (SCM bv, SCM idx)
  1807. { /* FIXME add some checks */
  1808. const double *contents = (const double*)SCM_BYTEVECTOR_CONTENTS (bv);
  1809. size_t i = scm_to_size_t (idx);
  1810. return scm_c_make_rectangular (contents[i/8], contents[i/8 + 1]);
  1811. }
  1812. typedef SCM (*scm_t_bytevector_ref_fn)(SCM, SCM);
  1813. const scm_t_bytevector_ref_fn bytevector_ref_fns[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] =
  1814. {
  1815. NULL, /* SCM */
  1816. NULL, /* CHAR */
  1817. NULL, /* BIT */
  1818. scm_bytevector_u8_ref, /* VU8 */
  1819. scm_bytevector_u8_ref, /* U8 */
  1820. scm_bytevector_s8_ref,
  1821. scm_bytevector_u16_native_ref,
  1822. scm_bytevector_s16_native_ref,
  1823. scm_bytevector_u32_native_ref,
  1824. scm_bytevector_s32_native_ref,
  1825. scm_bytevector_u64_native_ref,
  1826. scm_bytevector_s64_native_ref,
  1827. scm_bytevector_ieee_single_native_ref,
  1828. scm_bytevector_ieee_double_native_ref,
  1829. bytevector_ref_c32,
  1830. bytevector_ref_c64
  1831. };
  1832. static SCM
  1833. bv_handle_ref (scm_t_array_handle *h, size_t index)
  1834. {
  1835. SCM byte_index;
  1836. scm_t_bytevector_ref_fn ref_fn;
  1837. ref_fn = bytevector_ref_fns[h->element_type];
  1838. byte_index =
  1839. scm_from_size_t (index * scm_array_handle_uniform_element_size (h));
  1840. return ref_fn (h->array, byte_index);
  1841. }
  1842. /* FIXME add checks!!! */
  1843. static SCM
  1844. bytevector_set_c32 (SCM bv, SCM idx, SCM val)
  1845. { float *contents = (float*)SCM_BYTEVECTOR_CONTENTS (bv);
  1846. size_t i = scm_to_size_t (idx);
  1847. contents[i/4] = scm_c_real_part (val);
  1848. contents[i/4 + 1] = scm_c_imag_part (val);
  1849. return SCM_UNSPECIFIED;
  1850. }
  1851. static SCM
  1852. bytevector_set_c64 (SCM bv, SCM idx, SCM val)
  1853. { double *contents = (double*)SCM_BYTEVECTOR_CONTENTS (bv);
  1854. size_t i = scm_to_size_t (idx);
  1855. contents[i/8] = scm_c_real_part (val);
  1856. contents[i/8 + 1] = scm_c_imag_part (val);
  1857. return SCM_UNSPECIFIED;
  1858. }
  1859. typedef SCM (*scm_t_bytevector_set_fn)(SCM, SCM, SCM);
  1860. const scm_t_bytevector_set_fn bytevector_set_fns[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] =
  1861. {
  1862. NULL, /* SCM */
  1863. NULL, /* CHAR */
  1864. NULL, /* BIT */
  1865. scm_bytevector_u8_set_x, /* VU8 */
  1866. scm_bytevector_u8_set_x, /* U8 */
  1867. scm_bytevector_s8_set_x,
  1868. scm_bytevector_u16_native_set_x,
  1869. scm_bytevector_s16_native_set_x,
  1870. scm_bytevector_u32_native_set_x,
  1871. scm_bytevector_s32_native_set_x,
  1872. scm_bytevector_u64_native_set_x,
  1873. scm_bytevector_s64_native_set_x,
  1874. scm_bytevector_ieee_single_native_set_x,
  1875. scm_bytevector_ieee_double_native_set_x,
  1876. bytevector_set_c32,
  1877. bytevector_set_c64
  1878. };
  1879. static void
  1880. bv_handle_set_x (scm_t_array_handle *h, size_t index, SCM val)
  1881. {
  1882. SCM byte_index;
  1883. scm_t_bytevector_set_fn set_fn;
  1884. set_fn = bytevector_set_fns[h->element_type];
  1885. byte_index =
  1886. scm_from_size_t (index * scm_array_handle_uniform_element_size (h));
  1887. set_fn (h->array, byte_index, val);
  1888. }
  1889. static void
  1890. bytevector_get_handle (SCM v, scm_t_array_handle *h)
  1891. {
  1892. h->array = v;
  1893. h->ndims = 1;
  1894. h->dims = &h->dim0;
  1895. h->dim0.lbnd = 0;
  1896. h->dim0.ubnd = SCM_BYTEVECTOR_TYPED_LENGTH (v) - 1;
  1897. h->dim0.inc = 1;
  1898. h->element_type = SCM_BYTEVECTOR_ELEMENT_TYPE (v);
  1899. h->elements = h->writable_elements = SCM_BYTEVECTOR_CONTENTS (v);
  1900. }
  1901. /* Initialization. */
  1902. void
  1903. scm_bootstrap_bytevectors (void)
  1904. {
  1905. /* This must be instantiated here because the generalized-vector API may
  1906. want to access bytevectors even though `(rnrs bytevectors)' hasn't been
  1907. loaded. */
  1908. scm_null_bytevector = make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1909. #ifdef WORDS_BIGENDIAN
  1910. scm_i_native_endianness = scm_from_latin1_symbol ("big");
  1911. #else
  1912. scm_i_native_endianness = scm_from_latin1_symbol ("little");
  1913. #endif
  1914. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1915. "scm_init_bytevectors",
  1916. (scm_t_extension_init_func) scm_init_bytevectors,
  1917. NULL);
  1918. {
  1919. scm_t_array_implementation impl;
  1920. impl.tag = scm_tc7_bytevector;
  1921. impl.mask = 0x7f;
  1922. impl.vref = bv_handle_ref;
  1923. impl.vset = bv_handle_set_x;
  1924. impl.get_handle = bytevector_get_handle;
  1925. scm_i_register_array_implementation (&impl);
  1926. scm_i_register_vector_constructor
  1927. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_VU8],
  1928. scm_make_bytevector);
  1929. }
  1930. }
  1931. void
  1932. scm_init_bytevectors (void)
  1933. {
  1934. #include "libguile/bytevectors.x"
  1935. scm_endianness_big = scm_sym_big;
  1936. scm_endianness_little = scm_sym_little;
  1937. }