summaryrefslogtreecommitdiffstats
path: root/include/asm-arm/unaligned.h
blob: 3e4912a5c490a89e534a48c85d81fe1b66197962 (plain)
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
#ifndef __ASM_ARM_UNALIGNED_H
#define __ASM_ARM_UNALIGNED_H

#include <linux/types.h>

#define get_unaligned(ptr) \
	((__typeof__(*(ptr)))__get_unaligned_size((ptr), sizeof(*(ptr))))

#define put_unaligned(val, ptr) \
	__put_unaligned_size((unsigned long)(val), (ptr), sizeof(*(ptr)))

/*
 * We use a similar method to the uaccess.h badness detection.
 *
 * These are actually never defined anywhere, and therefore
 * catch errors at compile/link time.  Don't be tempted to
 * provide a declaration for them; doing so will mask the
 * errors.
 */
extern unsigned long __get_unaligned_bad(void);
extern void __put_unaligned_bad(void);

extern __inline__ unsigned long __get_unaligned_size(const void *ptr, size_t size)
{
	const unsigned char *p = (const unsigned char *)ptr;
	unsigned long val = 0;

	switch (size) {
	case 4:		val  = p[2] << 16 | p[3] << 24;
	case 2:		val |= p[1] << 8;
	case 1:		val |= p[0];				break;
	default:	val = __get_unaligned_bad();		break;
	}
	return val;
}

extern __inline__ void __put_unaligned_size(unsigned long val, void *ptr, size_t size)
{
	switch (size) {
	case 4:		((unsigned char *)ptr)[3] = val >> 24;
			((unsigned char *)ptr)[2] = val >> 16;
	case 2:		((unsigned char *)ptr)[1] = val >> 8;
	case 1:		((unsigned char *)ptr)[0] = val;	break;
	default:	__put_unaligned_bad();			break;
	}
}

#endif