1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
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.
17
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
21 *     permission.
22
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
26 * countries.
27
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**************************************/
39
40
41
42
43
44
45
46/**
47 * @file
48 *
49 * Utility functions for endian swapping
50 *
51 * <hr>$Revision: 32636 $<hr>
52 */
53
54#ifndef __CVMX_SWAP_H__
55#define __CVMX_SWAP_H__
56
57#ifdef	__cplusplus
58extern "C" {
59#endif
60
61
62/**
63 * Byte swap a 16 bit number
64 *
65 * @param x      16 bit number
66 * @return Byte swapped result
67 */
68static inline uint16_t cvmx_swap16(uint16_t x)
69{
70    return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |
71                       (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ));
72}
73
74
75/**
76 * Byte swap a 32 bit number
77 *
78 * @param x      32 bit number
79 * @return Byte swapped result
80 */
81static inline uint32_t cvmx_swap32(uint32_t x)
82{
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) ));
87}
88
89
90/**
91 * Byte swap a 64 bit number
92 *
93 * @param x      64 bit number
94 * @return Byte swapped result
95 */
96static inline uint64_t cvmx_swap64(uint64_t x)
97{
98    return ((x >> 56) |
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));
106}
107
108
109#ifdef __BIG_ENDIAN_BITFIELD
110
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)
114
115#define cvmx_cpu_to_be16(x) (x)
116#define cvmx_cpu_to_be32(x) (x)
117#define cvmx_cpu_to_be64(x) (x)
118
119#else
120
121#define cvmx_cpu_to_le16(x) (x)
122#define cvmx_cpu_to_le32(x) (x)
123#define cvmx_cpu_to_le64(x) (x)
124
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)
128
129#endif
130
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)
134
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)
138
139#ifdef	__cplusplus
140}
141#endif
142
143#endif  /* __CVMX_SWAP_H__ */
144