Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 1 | #include "swap.h" |
| 2 | |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
Matthias Andreas Benkard | e7fb7e1 | 2020-06-29 20:12:24 +0200 | [diff] [blame] | 6 | void swap_loop(char* __restrict a, char* __restrict b, size_t len) { |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 7 | while (len--) { |
| 8 | char tmp = *a; |
| 9 | *a++ = *b; |
| 10 | *b++ = tmp; |
| 11 | }; |
| 12 | } |
| 13 | |
Matthias Andreas Benkard | e7fb7e1 | 2020-06-29 20:12:24 +0200 | [diff] [blame] | 14 | void swap_malloc(char* __restrict a, char* __restrict b, size_t len) { |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 15 | char *temp = malloc(len); |
| 16 | memmove(temp,a,len); |
| 17 | memmove(a,b,len); |
| 18 | memmove(b,temp,len); |
| 19 | free(temp); |
| 20 | } |
| 21 | |
Matthias Andreas Benkard | e7fb7e1 | 2020-06-29 20:12:24 +0200 | [diff] [blame] | 22 | void swap_alloca(char* __restrict a, char* __restrict b, size_t len) { |
Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 23 | char temp[len]; |
| 24 | memmove(temp,a,len); |
| 25 | memmove(a,b,len); |
| 26 | memmove(b,temp,len); |
| 27 | } |