INT.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // this header is intended for use with standard integers
  2. // byte-aligned, 8, 16, 32 (and probably 64) bits integers
  3. #ifndef INT_HEADER
  4. #define INT_HEADER
  5. // this header needs
  6. #include <stdio.h>
  7. #include "BITS.h"
  8. #include "../ARRAY.h"
  9. // functions to print bean default integers
  10. umax print_byte_num(void * ptr, umax size); // byte dec
  11. umax print_byte_hex(void * ptr, umax size); // byte hex
  12. umax print_char_t_num(void * ptr, umax size);
  13. umax print_umax_num(void * ptr, umax size); // umax dec
  14. umax print_umax_hex(void * ptr, umax size); // umax hex
  15. umax print_smax_num(void * ptr, umax size);
  16. // the below functions are functions to calculate, from bits, an integer.
  17. // it does not assume that the system integer representation is the same
  18. // as the integer that it is going to be read from memory. That is why some
  19. // math is to be done
  20. // basic functions for integers
  21. umax powerof_uint(umax base, byte exponent);
  22. smax powerof_sint(smax base, byte exponent);
  23. umax get_max_uint(umax uint1, umax uint2);
  24. umax get_min_uint(umax uint1, umax uint2);
  25. // calculate specific types of integers from binary digits
  26. // (from bits stored in BIT_HOLDER)
  27. // UNSIGNED
  28. umax bits_to_std_uint(umax bit_holder_pos);
  29. // SIGNED
  30. smax bits_to_sign_magnitude_sint(umax bit_holder_pos);
  31. smax bits_to_1_complement_sint(umax bit_holder_pos);
  32. smax bits_to_2_complement_sint(umax bit_holder_pos);
  33. smax bits_to_offset_binary_sint(umax bit_holder_pos);
  34. smax bits_to_base_minus2_sint(umax bit_holder_pos);
  35. // get std/non-std unsigned/signed integers from memory (size in
  36. // bits, aligned to the leftmost byte from src by l_bit_pos)
  37. // UNSIGNED
  38. umax calc_mem_std_uint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  39. // SIGNED
  40. // See https://en.wikipedia.org/wiki/Signed_number_representations
  41. smax calc_mem_sign_mag_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  42. smax calc_mem_1_compl_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  43. smax calc_mem_2_compl_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  44. smax calc_mem_off_bin_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  45. smax calc_mem_bminus2_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  46. // the functions below assume that the bits from integers in memory
  47. // follow the usual integer representation (2-complement for signed
  48. // and normal unsigned representation) and that the system uses the
  49. // same usual integer representation as well. Seems like a dumb thing
  50. // to assume, as it is going to be like that on most systems as of
  51. // today, but I do it to make this personal library as general as
  52. // possible for my future projects). Might be a lot of a struggle
  53. // for such a small project but I'll still do it
  54. // get std unsigned/signed integers from memory
  55. umax get_uint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  56. smax get_sint(umax byte_cnt, umax lshifts, umax bit_cnt, umax lpad_cnt, void * src, ENDIAN_T endian);
  57. // the functions below will work to get/write standard integers
  58. // byte aligned and 8, 16 and 32 bits long signed/unsigned integers
  59. // get std signed/unsigned integer from memory
  60. umax get_std_uint(umax bit_cnt, void * src, ENDIAN_T endian);
  61. smax get_std_sint(umax bit_cnt, void * src, ENDIAN_T endian);
  62. // functions to print std integers
  63. umax print_uint8(void * ptr, umax size);
  64. umax print_uint16be(void * ptr, umax size);
  65. umax print_uint16le(void * ptr, umax size);
  66. umax print_uint32be(void * ptr, umax size);
  67. umax print_uint32le(void * ptr, umax size);
  68. umax print_sint8(void * ptr, umax size);
  69. umax print_sint16be(void * ptr, umax size);
  70. umax print_sint16le(void * ptr, umax size);
  71. umax print_sint32be(void * ptr, umax size);
  72. umax print_sint32le(void * ptr, umax size);
  73. // comparison functions
  74. bool umax_equal(void * umax1, void * umax2);
  75. // TODO
  76. // print functions for the list adt
  77. //~ void print_uint8(void * ptr);
  78. //~ void print_uint16(void * ptr);
  79. //~ void print_uint32(void * ptr);
  80. //~ void print_sint8(void * ptr);
  81. //~ void print_sint16(void * ptr);
  82. //~ void print_sint32(void * ptr);
  83. // compare functions, for sort_array()
  84. //~ bool comp_uint8(void * ptr1, void * ptr2);
  85. //~ bool comp_uint16(void * ptr1, void * ptr2);
  86. //~ bool comp_uint32(void * ptr1, void * ptr2);
  87. //~ bool comp_sint8(void * ptr1, void * ptr2);
  88. //~ bool comp_sint16(void * ptr1, void * ptr2);
  89. //~ bool comp_sint32(void * ptr1, void * ptr2);
  90. #include "INT.c"
  91. #endif // INT_HEADER