forked from kevinlawler/kona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbswap.c
94 lines (79 loc) · 1.47 KB
/
bswap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "incs.h"
#include <stdint.h>
#include <string.h>
#include "bswap.h"
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/endian.h>
#endif
#if defined(__linux__) && defined(__GNUC__)
#include <byteswap.h>
#define bswap32 __bswap_32
#define bswap64 __bswap_64
#endif
#ifdef _MSC_VER
#include <stdlib.h>
#define bswap32 _byteswap_ulong
#define bswap64 _byteswap_uint64
#endif
#ifdef __APPLE__
#include <libkern/_OSByteOrder.h>
#define bswap32 _OSSwapInt32
#define bswap64 _OSSwapInt64
#endif
#ifndef bswap32
uint32_t bswap32(uint32_t n)
{
UC d[4];
memcpy(d,&n,4);
R ((uint32_t)(d[0])<<24)+((uint32_t)(d[1])<<16)+
((uint32_t)(d[2])<< 8)+ (uint32_t)(d[3]);
}
uint64_t bswap64(uint64_t n)
{
UC d[8];
memcpy(d,&n,8);
R ((uint64_t)(d[0])<<56)+((uint64_t)(d[1])<<48)+
((uint64_t)(d[2])<<40)+((uint64_t)(d[3])<<32)+
((uint64_t)(d[4])<<24)+((uint64_t)(d[5])<<16)+
((uint64_t)(d[6])<< 8)+ (uint64_t)(d[7]);
}
#endif
V membswp32(V d,V s,I n)
{
uint32_t *q=d,*p=s;
I i;
for(i=0;i<n;i=i+4)
*q++ = bswap32(*p++);
R d;
}
V membswp64(V d,V s,I n)
{
uint64_t *q=d,*p=s;
I i;
for(i=0;i<n;i=i+8)
*q++ = bswap64(*p++);
R d;
}
I bswapI(I n)
{
#ifdef __Kona32__
R bswap32(n);
#else
R bswap64(n);
#endif
}
V membswpI(V d,V s,I n,I x)
{
if(x){
#ifdef __Kona32__
R membswp32(d,s,n);
#else
R membswp64(d,s,n);
#endif
}
R memcpy(d,s,n);
}
V membswpF(V d,V s,I n,I x)
{
R x?membswp64(d,s,n):memcpy(d,s,n);
}