copymem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* copymem.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef COPYMEM_H
  30. #define COPYMEM_H
  31. #include "typedefs.h"
  32. ///@TODO use optimized routines for this, depending on platform. these are just the standard ones
  33. #define copymem(m_to,m_from,m_count) \
  34. do { \
  35. unsigned char * _from=(unsigned char*)m_from; \
  36. unsigned char * _to=(unsigned char*)m_to; \
  37. int _count=m_count; \
  38. for (int _i=0;_i<_count;_i++) \
  39. _to[_i]=_from[_i]; \
  40. } while (0);
  41. /*
  42. case 0: *_dto++ = *_dfrom++; \
  43. case 7: *_dto++ = *_dfrom++; \
  44. case 6: *_dto++ = *_dfrom++; \
  45. case 5: *_dto++ = *_dfrom++; \
  46. case 4: *_dto++ = *_dfrom++; \
  47. case 3: *_dto++ = *_dfrom++; \
  48. case 2: *_dto++ = *_dfrom++; \
  49. case 1: *_dto++ = *_dfrom++; \
  50. */
  51. #define movemem_duff(m_to, m_from, m_count) \
  52. do { \
  53. if (m_to<m_from) { \
  54. unsigned char* _dto = (unsigned char*)m_to; \
  55. unsigned char* _dfrom = (unsigned char*)m_from; \
  56. int n = (m_count + 7) / 8; \
  57. switch (m_count % 8) { \
  58. do { \
  59. case 0: *_dto++ = *_dfrom++; \
  60. case 7: *_dto++ = *_dfrom++; \
  61. case 6: *_dto++ = *_dfrom++; \
  62. case 5: *_dto++ = *_dfrom++; \
  63. case 4: *_dto++ = *_dfrom++; \
  64. case 3: *_dto++ = *_dfrom++; \
  65. case 2: *_dto++ = *_dfrom++; \
  66. case 1: *_dto++ = *_dfrom++; \
  67. } while (--n > 0); \
  68. }; \
  69. } else if (m_to>m_from) { \
  70. unsigned char* _dto = &((unsigned char*)m_to)[m_count-1]; \
  71. unsigned char* _dfrom = &((unsigned char*)m_from)[m_count-1]; \
  72. int n = (m_count + 7) / 8; \
  73. switch (m_count % 8) { \
  74. do { \
  75. case 0: *_dto-- = *_dfrom--; \
  76. case 7: *_dto-- = *_dfrom--; \
  77. case 6: *_dto-- = *_dfrom--; \
  78. case 5: *_dto-- = *_dfrom--; \
  79. case 4: *_dto-- = *_dfrom--; \
  80. case 3: *_dto-- = *_dfrom--; \
  81. case 2: *_dto-- = *_dfrom--; \
  82. case 1: *_dto-- = *_dfrom--; \
  83. } while (--n > 0); \
  84. }; \
  85. } \
  86. } while(0) \
  87. #define movemem_conventional(m_to,m_from,m_count) \
  88. do { \
  89. if (m_to<m_from) { \
  90. unsigned char * _from=(unsigned char*)m_from; \
  91. unsigned char * _to=(unsigned char*)m_to; \
  92. int _count=m_count; \
  93. for (int _i=0;_i<_count;_i++) \
  94. _to[_i]=_from[_i]; \
  95. \
  96. } else if (m_to>m_from) { \
  97. unsigned char * _from=(unsigned char*)m_from; \
  98. unsigned char * _to=(unsigned char*)m_to; \
  99. int _count=m_count; \
  100. while (_count--) \
  101. _to[_count]=_from[_count]; \
  102. \
  103. \
  104. } \
  105. } while (0); \
  106. void movemem_system(void*,void*,int);
  107. #define movemem movemem_system
  108. void zeromem(void* p_mem,size_t p_bytes);
  109. #endif