1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Utility functions for endian swapping
51 * <hr>$Revision: 32636 $<hr>
54 #ifndef __CVMX_SWAP_H__
55 #define __CVMX_SWAP_H__
63 * Byte swap a 16 bit number
65 * @param x 16 bit number
66 * @return Byte swapped result
68 static inline uint16_t cvmx_swap16(uint16_t x)
70 return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |
71 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ));
76 * Byte swap a 32 bit number
78 * @param x 32 bit number
79 * @return Byte swapped result
81 static inline uint32_t cvmx_swap32(uint32_t x)
83 return ((uint32_t)((((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) |
84 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) |
85 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) |
86 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ));
91 * Byte swap a 64 bit number
93 * @param x 64 bit number
94 * @return Byte swapped result
96 static inline uint64_t cvmx_swap64(uint64_t x)
99 (((x >> 48) & 0xfful) << 8) |
100 (((x >> 40) & 0xfful) << 16) |
101 (((x >> 32) & 0xfful) << 24) |
102 (((x >> 24) & 0xfful) << 32) |
103 (((x >> 16) & 0xfful) << 40) |
104 (((x >> 8) & 0xfful) << 48) |
105 (((x >> 0) & 0xfful) << 56));
109 #ifdef __BIG_ENDIAN_BITFIELD
111 #define cvmx_cpu_to_le16(x) cvmx_swap16(x)
112 #define cvmx_cpu_to_le32(x) cvmx_swap32(x)
113 #define cvmx_cpu_to_le64(x) cvmx_swap64(x)
115 #define cvmx_cpu_to_be16(x) (x)
116 #define cvmx_cpu_to_be32(x) (x)
117 #define cvmx_cpu_to_be64(x) (x)
121 #define cvmx_cpu_to_le16(x) (x)
122 #define cvmx_cpu_to_le32(x) (x)
123 #define cvmx_cpu_to_le64(x) (x)
125 #define cvmx_cpu_to_be16(x) cvmx_swap16(x)
126 #define cvmx_cpu_to_be32(x) cvmx_swap32(x)
127 #define cvmx_cpu_to_be64(x) cvmx_swap64(x)
131 #define cvmx_le16_to_cpu(x) cvmx_cpu_to_le16(x)
132 #define cvmx_le32_to_cpu(x) cvmx_cpu_to_le32(x)
133 #define cvmx_le64_to_cpu(x) cvmx_cpu_to_le64(x)
135 #define cvmx_be16_to_cpu(x) cvmx_cpu_to_be16(x)
136 #define cvmx_be32_to_cpu(x) cvmx_cpu_to_be32(x)
137 #define cvmx_be64_to_cpu(x) cvmx_cpu_to_be64(x)
143 #endif /* __CVMX_SWAP_H__ */