blob: c646855dfcd4538699dc1fa408d269b8944ec839 [file] [log] [blame]
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02001#include "swap.h"
2
3#include <stdlib.h>
4#include <string.h>
5
Matthias Andreas Benkarde7fb7e12020-06-29 20:12:24 +02006void swap_loop(char* __restrict a, char* __restrict b, size_t len) {
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02007 while (len--) {
8 char tmp = *a;
9 *a++ = *b;
10 *b++ = tmp;
11 };
12}
13
Matthias Andreas Benkarde7fb7e12020-06-29 20:12:24 +020014void swap_malloc(char* __restrict a, char* __restrict b, size_t len) {
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020015 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 Benkarde7fb7e12020-06-29 20:12:24 +020022void swap_alloca(char* __restrict a, char* __restrict b, size_t len) {
Matthias Andreas Benkard08586462020-06-28 22:42:43 +020023 char temp[len];
24 memmove(temp,a,len);
25 memmove(a,b,len);
26 memmove(b,temp,len);
27}