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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* Copyright (c) 2005 Marcel Moolenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <machine/float.h>
#include <string.h>
/* Memory accesses. */
#define Load 0x01
#define Store 0x02
/* Data type. */
#define Integer 0x11
#define FloatingPoint 0x12
/* Data size. */
#define Small 0x21
#define Medium 0x22
#define Large 0x23
/* Post increment. */
#define NoPostInc 0x31
#define MinConstPostInc 0x32
#define PlusConstPostInc 0x33
#define ScratchRegPostInc 0x34
#define PreservedRegPostInc 0x35
#if ACCESS == 0 || TYPE == 0 || SIZE == 0 || POSTINC == 0
#error define ACCESS, TYPE, SIZE and/or POSTINC
#endif
#if TYPE == Integer
# define REG "r8"
# if SIZE == Small
# define DATA_TYPE short
# define DATA_VALUE 0x1234
# define LD "ld2"
# define ST "st2"
# elif SIZE == Medium
# define DATA_TYPE int
# define DATA_VALUE 0x12345678
# define LD "ld4"
# define ST "st4"
# elif SIZE == Large
# define DATA_TYPE long
# define DATA_VALUE 0x1234567890ABCDEF
# define LD "ld8"
# define ST "st8"
# endif
#elif TYPE == FloatingPoint
# define REG "f6"
# if SIZE == Small
# define DATA_TYPE float
# define DATA_VALUE FLT_MIN
# define LD "ldfs"
# define ST "stfs"
# elif SIZE == Medium
# define DATA_TYPE double
# define DATA_VALUE DBL_MIN
# define LD "ldfd"
# define ST "stfd"
# elif SIZE == Large
# define DATA_TYPE long double
# define DATA_VALUE LDBL_MIN
# define LD "ldfe"
# define ST "stfe"
# endif
#endif
struct {
DATA_TYPE aligned;
char _;
char misaligned[sizeof(DATA_TYPE)];
} data;
DATA_TYPE *aligned = &data.aligned;
DATA_TYPE *misaligned = (DATA_TYPE *)data.misaligned;
DATA_TYPE value = DATA_VALUE;
void
block_copy(void *dst, void *src, size_t sz)
{
memcpy(dst, src, sz);
}
int
main()
{
/* Set PSR.ac. */
asm volatile("sum 8");
#if ACCESS == Load
/*
* LOAD
*/
block_copy(misaligned, &value, sizeof(DATA_TYPE));
# if POSTINC == NoPostInc
/* Misaligned load. */
*aligned = *misaligned;
# elif POSTINC == MinConstPostInc
asm volatile(
"ld8 r2=%0;;"
LD " " REG "=[r2],%2;;"
"st8 %0=r2;" ST " %1=" REG ";;"
: "=m"(misaligned), "=m"(*aligned)
: "i"(-sizeof(DATA_TYPE))
: REG, "r2", "memory");
# elif POSTINC == PlusConstPostInc
asm volatile(
"ld8 r2=%0;;"
LD " " REG "=[r2],%2;;"
"st8 %0=r2;" ST " %1=" REG ";;"
: "=m"(misaligned), "=m"(*aligned)
: "i"(sizeof(DATA_TYPE))
: REG, "r2", "memory");
# elif POSTINC == ScratchRegPostInc
asm volatile(
"ld8 r2=%0; mov r3=%2;;"
LD " " REG "=[r2],r3;;"
"st8 %0=r2;" ST " %1=" REG ";;"
: "=m"(misaligned), "=m"(*aligned)
: "i"(sizeof(DATA_TYPE))
: REG, "r2", "r3", "memory");
# elif POSTINC == PreservedRegPostInc
asm volatile(
"ld8 r2=%0; mov r4=%2;;"
LD " " REG "=[r2],r4;;"
"st8 %0=r2;" ST " %1=" REG ";;"
: "=m"(misaligned), "=m"(*aligned)
: "i"(sizeof(DATA_TYPE))
: REG, "r2", "r4", "memory");
# endif
#elif ACCESS == Store
/*
* STORE
*/
# if POSTINC == NoPostInc
/* Misaligned store. */
*misaligned = value;
# elif POSTINC == MinConstPostInc
asm volatile(
"ld8 r2=%0;" LD " " REG "=%1;;"
ST " [r2]=" REG ",%2;;"
"st8 %0=r2;;"
: "=m"(misaligned)
: "m"(value), "i"(-sizeof(DATA_TYPE))
: REG, "r2", "memory");
# elif POSTINC == PlusConstPostInc
asm volatile(
"ld8 r2=%0;" LD " " REG "=%1;;"
ST " [r2]=" REG ",%2;;"
"st8 %0=r2;;"
: "=m"(misaligned)
: "m"(value), "i"(sizeof(DATA_TYPE))
: REG, "r2", "memory");
# elif POSTINC == ScratchRegPostInc || POSTINC == PreservedRegPostInc
return (1);
# endif
block_copy(aligned, data.misaligned, sizeof(DATA_TYPE));
#endif
if (*aligned != value)
return (2);
#if POSTINC == NoPostInc
return (0);
#elif POSTINC == MinConstPostInc
return (((char *)misaligned == data.misaligned - sizeof(DATA_TYPE))
? 0 : 4);
#else
return (((char *)misaligned == data.misaligned + sizeof(DATA_TYPE))
? 0 : 4);
#endif
}
|