ieee_bytevect.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Harald Glab-Phlag, Mike Sperber
  5. */
  6. /* this file includes procedures to write a double to a byte - vector and read it
  7. out of the vect */
  8. #include <scheme48.h>
  9. static s48_ref_t
  10. r6rs_float_to_bytevect (s48_call_t call, s48_ref_t sch_float,
  11. s48_ref_t sch_bytevect, s48_ref_t sch_index)
  12. {
  13. long index = s48_extract_long_2(call, sch_index);
  14. char values[4];
  15. long ref = 0;
  16. *((float*)values) = (float)s48_extract_double_2(call, sch_float);
  17. while (ref < 4)
  18. {
  19. s48_byte_vector_set_2(call, sch_bytevect, index + ref, values[ref]);
  20. ++ref;
  21. }
  22. return s48_unspecific_2(call);
  23. }
  24. static s48_ref_t
  25. r6rs_bytevect_to_float (s48_call_t call, s48_ref_t sch_bytevect, s48_ref_t sch_index)
  26. {
  27. long index = s48_extract_long_2(call, sch_index);
  28. char values[4];
  29. long ref = 0;
  30. float resval;
  31. while (ref < 4)
  32. {
  33. values[ref] = s48_byte_vector_ref_2(call, sch_bytevect, index + ref);
  34. ++ref;
  35. }
  36. resval = *((float*) values);
  37. return s48_enter_double_2(call, (double)resval);
  38. }
  39. static s48_ref_t
  40. r6rs_double_to_bytevect (s48_call_t call, s48_ref_t sch_double,
  41. s48_ref_t sch_bytevect, s48_ref_t sch_index)
  42. {
  43. long index = s48_extract_long_2(call, sch_index);
  44. char values[8];
  45. long ref = 0;
  46. *((double*)values) = s48_extract_double_2(call, sch_double);
  47. while (ref < 8)
  48. {
  49. s48_byte_vector_set_2(call, sch_bytevect, index + ref, values[ref]);
  50. ++ref;
  51. }
  52. return s48_unspecific_2(call);
  53. }
  54. static s48_ref_t
  55. r6rs_bytevect_to_double (s48_call_t call, s48_ref_t sch_bytevect, s48_ref_t sch_index)
  56. {
  57. long index = s48_extract_long_2(call, sch_index);
  58. char values[8];
  59. double resval;
  60. long ref = 0;
  61. while (ref < 8)
  62. {
  63. values[ref] = s48_byte_vector_ref_2(call, sch_bytevect, index + ref);
  64. ++ref;
  65. }
  66. resval = *((double*) values);
  67. return s48_enter_double_2(call, resval);
  68. }
  69. static s48_ref_t
  70. r6rs_is_big_endian (s48_call_t call)
  71. {
  72. union {
  73. uint32_t i;
  74. char c[4];
  75. } bint = {0x01020304};
  76. return s48_enter_boolean_2(call, bint.c[0] == 1);
  77. }
  78. void s48_init_ieee_bytevect(void)
  79. {
  80. S48_EXPORT_FUNCTION(r6rs_float_to_bytevect);
  81. S48_EXPORT_FUNCTION(r6rs_bytevect_to_float);
  82. S48_EXPORT_FUNCTION(r6rs_double_to_bytevect);
  83. S48_EXPORT_FUNCTION(r6rs_bytevect_to_double);
  84. S48_EXPORT_FUNCTION(r6rs_is_big_endian);
  85. }