1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (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 Networks 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 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
47 * Utility functions for endian swapping
49 * <hr>$Revision: 32636 $<hr>
52 #ifndef __CVMX_SWAP_H__
53 #define __CVMX_SWAP_H__
61 * Byte swap a 16 bit number
63 * @param x 16 bit number
64 * @return Byte swapped result
66 static inline uint16_t cvmx_swap16(uint16_t x)
68 return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |
69 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ));
74 * Byte swap a 32 bit number
76 * @param x 32 bit number
77 * @return Byte swapped result
79 static inline uint32_t cvmx_swap32(uint32_t x)
81 return ((uint32_t)((((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) |
82 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) |
83 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) |
84 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ));
89 * Byte swap a 64 bit number
91 * @param x 64 bit number
92 * @return Byte swapped result
94 static inline uint64_t cvmx_swap64(uint64_t x)
97 (((x >> 48) & 0xfful) << 8) |
98 (((x >> 40) & 0xfful) << 16) |
99 (((x >> 32) & 0xfful) << 24) |
100 (((x >> 24) & 0xfful) << 32) |
101 (((x >> 16) & 0xfful) << 40) |
102 (((x >> 8) & 0xfful) << 48) |
103 (((x >> 0) & 0xfful) << 56));
107 #if __BYTE_ORDER == __BIG_ENDIAN
109 #define cvmx_cpu_to_le16(x) cvmx_swap16(x)
110 #define cvmx_cpu_to_le32(x) cvmx_swap32(x)
111 #define cvmx_cpu_to_le64(x) cvmx_swap64(x)
113 #define cvmx_cpu_to_be16(x) (x)
114 #define cvmx_cpu_to_be32(x) (x)
115 #define cvmx_cpu_to_be64(x) (x)
119 #define cvmx_cpu_to_le16(x) (x)
120 #define cvmx_cpu_to_le32(x) (x)
121 #define cvmx_cpu_to_le64(x) (x)
123 #define cvmx_cpu_to_be16(x) cvmx_swap16(x)
124 #define cvmx_cpu_to_be32(x) cvmx_swap32(x)
125 #define cvmx_cpu_to_be64(x) cvmx_swap64(x)
129 #define cvmx_le16_to_cpu(x) cvmx_cpu_to_le16(x)
130 #define cvmx_le32_to_cpu(x) cvmx_cpu_to_le32(x)
131 #define cvmx_le64_to_cpu(x) cvmx_cpu_to_le64(x)
133 #define cvmx_be16_to_cpu(x) cvmx_cpu_to_be16(x)
134 #define cvmx_be32_to_cpu(x) cvmx_cpu_to_be32(x)
135 #define cvmx_be64_to_cpu(x) cvmx_cpu_to_be64(x)
141 #endif /* __CVMX_SWAP_H__ */