root / prex-0.9.0 / bsp / drv / lib / endian.c @ 03e9c04a
History | View | Annotate | Download (676 Bytes)
1 |
/*
|
---|---|
2 |
* Written by J.T. Conklin <jtc@NetBSD.org>.
|
3 |
* Public domain.
|
4 |
*/
|
5 |
|
6 |
#include <driver.h> |
7 |
#include <sys/types.h> |
8 |
#include <sys/endian.h> |
9 |
|
10 |
#undef htonl
|
11 |
#undef htons
|
12 |
#undef ntohl
|
13 |
#undef ntohs
|
14 |
|
15 |
#if BYTE_ORDER == LITTLE_ENDIAN
|
16 |
uint32_t |
17 |
htonl(uint32_t x) |
18 |
{ |
19 |
|
20 |
u_char *s = (u_char *)&x; |
21 |
return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); |
22 |
} |
23 |
|
24 |
uint16_t |
25 |
htons(uint16_t x) |
26 |
{ |
27 |
|
28 |
u_char *s = (u_char *) &x; |
29 |
return (uint16_t)(s[0] << 8 | s[1]); |
30 |
} |
31 |
|
32 |
uint32_t |
33 |
ntohl(uint32_t x) |
34 |
{ |
35 |
|
36 |
u_char *s = (u_char *)&x; |
37 |
return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); |
38 |
} |
39 |
|
40 |
uint16_t |
41 |
ntohs(uint16_t x) |
42 |
{ |
43 |
|
44 |
u_char *s = (u_char *) &x; |
45 |
return (uint16_t)(s[0] << 8 | s[1]); |
46 |
} |
47 |
#endif
|