Import byte-order macros into DevLib.
[bertos.git] / mware / byteorder.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See devlib/README for information.
6  * -->
7  *
8  * \brief Functions to convert integers to/from host byte-order.
9  *
10  * \version $Id$
11  *
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  */
15
16 /*
17  * $Log$
18  * Revision 1.1  2004/07/20 16:26:15  bernie
19  * Import byte-order macros into DevLib.
20  *
21  */
22
23 #ifndef MWARE_BYTEORDER_H
24 #define MWARE_BYTEORDER_H
25
26 #include <compiler.h>
27 #include <cpu.h>
28
29 INLINE uint16_t cpu_to_be16(uint16_t n);
30 INLINE uint16_t cpu_to_be16(uint16_t n)
31 {
32         if (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN)
33                 n = n << 8 | n >> 8;
34
35         return n;
36 }
37
38 INLINE uint16_t cpu_to_le16(uint16_t n);
39 INLINE uint16_t cpu_to_le16(uint16_t n)
40 {
41         if (CPU_BYTE_ORDER == CPU_BIG_ENDIAN)
42                 n = n << 8 | n >> 8;
43
44         return n;
45 }
46
47 #endif /* MWARE_BYTEORDER_H */