Advertisement
YoungSideways

C swap funtions

Apr 8th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <math.h>
  3. #include <assert.h>
  4. #include <memory.h>
  5.  
  6. inline void swap(void* a, void* b, size_t size) {
  7.     for (; size % sizeof(long); size--) {
  8.         char temp = *(char*)a;
  9.         *(((char*)a)++) = *(char*)b;
  10.         *(((char*)b)++) = temp;
  11.     };
  12.     for (; size; size -= sizeof(long)) {
  13.         long temp = *(long*)a;
  14.         *(((long*)a)++) = *(long*)b;
  15.         *(((long*)b)++) = temp;
  16.     };
  17. }
  18. inline void swap_s(void* a, void* b, size_t size) {
  19.     if (abs((char*)a - b) >= size) {
  20.         long temp;
  21.         for (; size % sizeof(long); size--) {
  22.             temp = *(char*)a;
  23.             *(((char*)a)++) = *(char*)b;
  24.             *(((char*)b)++) = temp;
  25.         };
  26.         for (; size; size -= sizeof(long)) {
  27.             temp = *(long*)a;
  28.             *(((long*)a)++) = *(long*)b;
  29.             *(((long*)b)++) = temp;
  30.         };
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement