gim_memory.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef GIM_MEMORY_H_INCLUDED
  2. #define GIM_MEMORY_H_INCLUDED
  3. /*! \file gim_memory.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: [email protected]
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. #include "gim_math.h"
  30. #include <string.h>
  31. #ifdef PREFETCH
  32. #include <xmmintrin.h> // for prefetch
  33. #define pfval 64
  34. #define pfval2 128
  35. //! Prefetch 64
  36. #define pf(_x, _i) _mm_prefetch((void *)(_x + _i + pfval), 0)
  37. //! Prefetch 128
  38. #define pf2(_x, _i) _mm_prefetch((void *)(_x + _i + pfval2), 0)
  39. #else
  40. //! Prefetch 64
  41. #define pf(_x, _i)
  42. //! Prefetch 128
  43. #define pf2(_x, _i)
  44. #endif
  45. ///Functions for manip packed arrays of numbers
  46. #define GIM_COPY_ARRAYS(dest_array, source_array, element_count) \
  47. { \
  48. for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
  49. { \
  50. dest_array[_i_] = source_array[_i_]; \
  51. } \
  52. }
  53. #define GIM_COPY_ARRAYS_1(dest_array, source_array, element_count, copy_macro) \
  54. { \
  55. for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
  56. { \
  57. copy_macro(dest_array[_i_], source_array[_i_]); \
  58. } \
  59. }
  60. #define GIM_ZERO_ARRAY(array, element_count) \
  61. { \
  62. for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
  63. { \
  64. array[_i_] = 0; \
  65. } \
  66. }
  67. #define GIM_CONSTANT_ARRAY(array, element_count, constant) \
  68. { \
  69. for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
  70. { \
  71. array[_i_] = constant; \
  72. } \
  73. }
  74. ///Function prototypes to allocate and free memory.
  75. typedef void *gim_alloc_function(size_t size);
  76. typedef void *gim_alloca_function(size_t size); //Allocs on the heap
  77. typedef void *gim_realloc_function(void *ptr, size_t oldsize, size_t newsize);
  78. typedef void gim_free_function(void *ptr);
  79. ///Memory Function Handlers
  80. ///set new memory management functions. if fn is 0, the default handlers are used.
  81. void gim_set_alloc_handler(gim_alloc_function *fn);
  82. void gim_set_alloca_handler(gim_alloca_function *fn);
  83. void gim_set_realloc_handler(gim_realloc_function *fn);
  84. void gim_set_free_handler(gim_free_function *fn);
  85. ///get current memory management functions.
  86. gim_alloc_function *gim_get_alloc_handler(void);
  87. gim_alloca_function *gim_get_alloca_handler(void);
  88. gim_realloc_function *gim_get_realloc_handler(void);
  89. gim_free_function *gim_get_free_handler(void);
  90. ///Standar Memory functions
  91. void *gim_alloc(size_t size);
  92. void *gim_alloca(size_t size);
  93. void *gim_realloc(void *ptr, size_t oldsize, size_t newsize);
  94. void gim_free(void *ptr);
  95. #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  96. #define GIM_SIMD_MEMORY 1
  97. #endif
  98. //! SIMD POINTER INTEGER
  99. #define SIMD_T GUINT64
  100. //! SIMD INTEGER SIZE
  101. #define SIMD_T_SIZE sizeof(SIMD_T)
  102. inline void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
  103. {
  104. #ifdef GIM_SIMD_MEMORY
  105. /*
  106. //'long long int' is incompatible with visual studio 6...
  107. //copy words
  108. SIMD_T * ui_src_ptr = (SIMD_T *)src;
  109. SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
  110. while(copysize>=SIMD_T_SIZE)
  111. {
  112. *(ui_dst_ptr++) = *(ui_src_ptr++);
  113. copysize-=SIMD_T_SIZE;
  114. }
  115. if(copysize==0) return;
  116. */
  117. char *c_src_ptr = (char *)src;
  118. char *c_dst_ptr = (char *)dst;
  119. while (copysize > 0)
  120. {
  121. *(c_dst_ptr++) = *(c_src_ptr++);
  122. copysize--;
  123. }
  124. return;
  125. #else
  126. memcpy(dst, src, copysize);
  127. #endif
  128. }
  129. template <class T>
  130. inline void gim_swap_elements(T *_array, size_t _i, size_t _j)
  131. {
  132. T _e_tmp_ = _array[_i];
  133. _array[_i] = _array[_j];
  134. _array[_j] = _e_tmp_;
  135. }
  136. template <class T>
  137. inline void gim_swap_elements_memcpy(T *_array, size_t _i, size_t _j)
  138. {
  139. char _e_tmp_[sizeof(T)];
  140. gim_simd_memcpy(_e_tmp_, &_array[_i], sizeof(T));
  141. gim_simd_memcpy(&_array[_i], &_array[_j], sizeof(T));
  142. gim_simd_memcpy(&_array[_j], _e_tmp_, sizeof(T));
  143. }
  144. template <int SIZE>
  145. inline void gim_swap_elements_ptr(char *_array, size_t _i, size_t _j)
  146. {
  147. char _e_tmp_[SIZE];
  148. _i *= SIZE;
  149. _j *= SIZE;
  150. gim_simd_memcpy(_e_tmp_, _array + _i, SIZE);
  151. gim_simd_memcpy(_array + _i, _array + _j, SIZE);
  152. gim_simd_memcpy(_array + _j, _e_tmp_, SIZE);
  153. }
  154. #endif // GIM_MEMORY_H_INCLUDED