File: Byteswapping.h

package info (click to toggle)
pose 3.5-7
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 14,136 kB
  • ctags: 29,490
  • sloc: cpp: 93,990; ansic: 62,838; sh: 27,519; perl: 1,891; python: 1,242; makefile: 652
file content (175 lines) | stat: -rw-r--r-- 3,789 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#ifndef _BYTESWAPPING_H_
#define _BYTESWAPPING_H_

#if !defined (BYTESWAP)
#error "Must define BYTESWAP"
#endif


struct regstruct;


/*
**	--------------------------------------------------------------------------------
**		Byte swapping
**	--------------------------------------------------------------------------------
*/

#define BYTE_SWAP_16(n)	((((uint16) n) >> 8) |	\
						 (((uint16) n) << 8))

#define BYTE_SWAP_32(n)	( (((uint32) n) << 24) |				\
						 ((((uint32) n) << 8) & 0x00FF0000) |	\
						 ((((uint32) n) >> 8) & 0x0000FF00) |	\
						  (((uint32) n) >> 24))

#ifdef _MSC_VER
#define BYTE_SWAP_64(n)	( (((uint64) n) << 56) |							\
						 ((((uint64) n) << 40) & 0x00FF000000000000i64) |	\
						 ((((uint64) n) << 24) & 0x0000FF0000000000i64) |	\
						 ((((uint64) n) <<  8) & 0x000000FF00000000i64) |	\
						 ((((uint64) n) >>  8) & 0x00000000FF000000i64) |	\
						 ((((uint64) n) >> 24) & 0x0000000000FF0000i64) |	\
						 ((((uint64) n) >> 40) & 0x000000000000FF00i64) |	\
						  (((uint64) n) >> 56))
#else
#define BYTE_SWAP_64(n)	( (((uint64) n) << 56) |							\
						 ((((uint64) n) << 40) & 0x00FF000000000000LL) |	\
						 ((((uint64) n) << 24) & 0x0000FF0000000000LL) |	\
						 ((((uint64) n) <<  8) & 0x000000FF00000000LL) |	\
						 ((((uint64) n) >>  8) & 0x00000000FF000000LL) |	\
						 ((((uint64) n) >> 24) & 0x0000000000FF0000LL) |	\
						 ((((uint64) n) >> 40) & 0x000000000000FF00LL) |	\
						  (((uint64) n) >> 56))
#endif


inline void Byteswap(bool&)
{
}

inline void Byteswap(char&)
{
}

inline void Byteswap(signed char&)
{
}

inline void Byteswap(unsigned char&)
{
}

inline void Byteswap(short& v)
{
	v = (short) BYTE_SWAP_16(v);
}

inline void Byteswap(unsigned short& v)
{
	v = (unsigned short) BYTE_SWAP_16(v);
}

inline void Byteswap(int& v)
{
	if (sizeof(int) == 4)
		v = (int) BYTE_SWAP_32(v);

	else if (sizeof(int) == 2)
		v = (int) BYTE_SWAP_16(v);
}

inline void Byteswap(unsigned int& v)
{
	if (sizeof(unsigned int) == 4)
		v = (unsigned int) BYTE_SWAP_32(v);

	else if (sizeof(unsigned int) == 2)
		v = (unsigned int) BYTE_SWAP_16(v);
}

inline void Byteswap(long& v)
{
	v = (long) BYTE_SWAP_32(v);
}

inline void Byteswap(unsigned long& v)
{
	v = (unsigned long) BYTE_SWAP_32(v);
}

inline void Byteswap(int64& v)
{
	v = (int64) BYTE_SWAP_64(v);
}

inline void Byteswap(uint64& v)
{
	v = (uint64) BYTE_SWAP_64(v);
}

template <class T>
inline void Byteswap(T*& v)
{
	Byteswap ((uint32&) v);
}

void Byteswap (HwrM68328Type&);
void Byteswap (HwrM68EZ328Type&);
void Byteswap (HwrM68VZ328Type&);
void Byteswap (regstruct& r);
void Byteswap (SED1375RegsType& p);

#if BYTESWAP

	template <class T>
	inline void Canonical(T& v)
	{
		Byteswap (v);
	}

#else

	template <class T>
	inline void Canonical(T&)
	{
	}

#endif


#if WORDSWAP_MEMORY

	inline void ByteswapWords (void* start, unsigned long length)
	{
		for (unsigned long ii = 0; ii < length / 2; ++ii)
		{
			unsigned short*	p = ((unsigned short*) start) + ii;
			unsigned char*	bp = (unsigned char*) p;
			*p = (((unsigned short) *bp) << 8) | (unsigned short) (*(bp + 1));
		}
	}

#else

	inline void ByteswapWords (void*, unsigned long)
	{
	}

#endif	// WORDSWAP_MEMORY

#endif /* _BYTESWAPPING_H_ */