endian.h (b626f5a73a48f44a31a200291b141e1da408a2ff) endian.h (720dc6bcb5a8c4283802576e2ef54f42b33fa8d4)
1/*-
2 * Copyright (c) 2001 David E. O'Brien
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 20 unchanged lines hidden (view full) ---

29 * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
30 * $FreeBSD$
31 */
32
33#ifndef _MACHINE_ENDIAN_H_
34#define _MACHINE_ENDIAN_H_
35
36#include <sys/_types.h>
1/*-
2 * Copyright (c) 2001 David E. O'Brien
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 20 unchanged lines hidden (view full) ---

29 * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
30 * $FreeBSD$
31 */
32
33#ifndef _MACHINE_ENDIAN_H_
34#define _MACHINE_ENDIAN_H_
35
36#include <sys/_types.h>
37#include <sys/_endian.h>
37
38
38/*
39 * Definitions for byte order, according to byte significance from low
40 * address to high.
41 */
42#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
43#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
44#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
45
46#define _BYTE_ORDER _LITTLE_ENDIAN
47
48#if __BSD_VISIBLE
49#define LITTLE_ENDIAN _LITTLE_ENDIAN
50#define BIG_ENDIAN _BIG_ENDIAN
51#define PDP_ENDIAN _PDP_ENDIAN
52#define BYTE_ORDER _BYTE_ORDER
53#endif
54
55#define _QUAD_HIGHWORD 1
56#define _QUAD_LOWWORD 0
57#define __ntohl(x) (__bswap32(x))
58#define __ntohs(x) (__bswap16(x))
59#define __htonl(x) (__bswap32(x))
60#define __htons(x) (__bswap16(x))
61
62static __inline __uint64_t
63__bswap64(__uint64_t _x)
64{
65 __uint64_t ret;
66
67 ret = (_x >> 56);
68 ret |= ((_x >> 40) & 0xff00);
69 ret |= ((_x >> 24) & 0xff0000);
70 ret |= ((_x >> 8) & 0xff000000);
71 ret |= ((_x << 8) & ((__uint64_t)0xff << 32));
72 ret |= ((_x << 24) & ((__uint64_t)0xff << 40));
73 ret |= ((_x << 40) & ((__uint64_t)0xff << 48));
74 ret |= (_x << 56);
75
76 return (ret);
77}
78
79static __inline __uint32_t
80__bswap32_var(__uint32_t _x)
81{
82
83 return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
84 ((_x << 24) & 0xff000000));
85}
86
87static __inline __uint16_t
88__bswap16_var(__uint16_t _x)
89{
90 __uint32_t ret;
91
92 ret = ((_x >> 8) | ((_x << 8) & 0xff00));
93
94 return ((__uint16_t)ret);
95}
96
97#ifdef __OPTIMIZE__
98
99#define __bswap32_constant(x) \
100 ((((x) & 0xff000000U) >> 24) | \
101 (((x) & 0x00ff0000U) >> 8) | \
102 (((x) & 0x0000ff00U) << 8) | \
103 (((x) & 0x000000ffU) << 24))
104
105#define __bswap16_constant(x) \
106 ((((x) & 0xff00) >> 8) | \
107 (((x) & 0x00ff) << 8))
108
109#define __bswap16(x) \
110 ((__uint16_t)(__builtin_constant_p(x) ? \
111 __bswap16_constant(x) : \
112 __bswap16_var(x)))
113
114#define __bswap32(x) \
115 ((__uint32_t)(__builtin_constant_p(x) ? \
116 __bswap32_constant(x) : \
117 __bswap32_var(x)))
118
119#else
120#define __bswap16(x) __bswap16_var(x)
121#define __bswap32(x) __bswap32_var(x)
122
123#endif /* __OPTIMIZE__ */
124#endif /* !_MACHINE_ENDIAN_H_ */
39#endif /* !_MACHINE_ENDIAN_H_ */