cvmx-swap.h revision 232812
1251212Spfg/***********************license start*************** 2122180Skan * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights 3132718Skan * reserved. 4122180Skan * 5132718Skan * 6122180Skan * Redistribution and use in source and binary forms, with or without 7122180Skan * modification, are permitted provided that the following conditions are 8122180Skan * met: 9122180Skan * 10132718Skan * * Redistributions of source code must retain the above copyright 11122180Skan * notice, this list of conditions and the following disclaimer. 12122180Skan * 13122180Skan * * Redistributions in binary form must reproduce the above 14122180Skan * copyright notice, this list of conditions and the following 15122180Skan * disclaimer in the documentation and/or other materials provided 16132718Skan * with the distribution. 17169689Skan 18169689Skan * * Neither the name of Cavium Inc. nor the names of 19122180Skan * its contributors may be used to endorse or promote products 20122180Skan * derived from this software without specific prior written 21122180Skan * permission. 22122180Skan 23122180Skan * This Software, including technical data, may be subject to U.S. export control 24122180Skan * laws, including the U.S. Export Administration Act and its associated 25122180Skan * regulations, and may be subject to export or import regulations in other 26122180Skan * countries. 27122180Skan 28169689Skan * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29122180Skan * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 30122180Skan * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31122180Skan * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32122180Skan * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33251212Spfg * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34251212Spfg * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35251212Spfg * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36251212Spfg * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37251212Spfg * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38122180Skan ***********************license end**************************************/ 39122180Skan 40122180Skan 41122180Skan 42122180Skan 43122180Skan 44122180Skan 45122180Skan 46122180Skan/** 47122180Skan * @file 48122180Skan * 49122180Skan * Utility functions for endian swapping 50122180Skan * 51169689Skan * <hr>$Revision: 32636 $<hr> 52122180Skan */ 53122180Skan 54122180Skan#ifndef __CVMX_SWAP_H__ 55122180Skan#define __CVMX_SWAP_H__ 56122180Skan 57169689Skan#ifdef __cplusplus 58122180Skanextern "C" { 59122180Skan#endif 60122180Skan 61122180Skan 62122180Skan/** 63169689Skan * Byte swap a 16 bit number 64122180Skan * 65122180Skan * @param x 16 bit number 66122180Skan * @return Byte swapped result 67122180Skan */ 68122180Skanstatic inline uint16_t cvmx_swap16(uint16_t x) 69169689Skan{ 70122180Skan return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | 71122180Skan (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) )); 72122180Skan} 73122180Skan 74122180Skan 75169689Skan/** 76122180Skan * Byte swap a 32 bit number 77122180Skan * 78122180Skan * @param x 32 bit number 79122180Skan * @return Byte swapped result 80122180Skan */ 81169689Skanstatic inline uint32_t cvmx_swap32(uint32_t x) 82122180Skan{ 83122180Skan return ((uint32_t)((((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | 84122180Skan (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | 85122180Skan (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | 86122180Skan (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) )); 87169689Skan} 88122180Skan 89122180Skan 90122180Skan/** 91122180Skan * Byte swap a 64 bit number 92122180Skan * 93169689Skan * @param x 64 bit number 94122180Skan * @return Byte swapped result 95122180Skan */ 96122180Skanstatic inline uint64_t cvmx_swap64(uint64_t x) 97122180Skan{ 98122180Skan return ((x >> 56) | 99169689Skan (((x >> 48) & 0xfful) << 8) | 100122180Skan (((x >> 40) & 0xfful) << 16) | 101122180Skan (((x >> 32) & 0xfful) << 24) | 102169689Skan (((x >> 24) & 0xfful) << 32) | 103122180Skan (((x >> 16) & 0xfful) << 40) | 104122180Skan (((x >> 8) & 0xfful) << 48) | 105169689Skan (((x >> 0) & 0xfful) << 56)); 106122180Skan} 107122180Skan 108169689Skan 109122180Skan#ifdef __BIG_ENDIAN_BITFIELD 110122180Skan 111169689Skan#define cvmx_cpu_to_le16(x) cvmx_swap16(x) 112122180Skan#define cvmx_cpu_to_le32(x) cvmx_swap32(x) 113122180Skan#define cvmx_cpu_to_le64(x) cvmx_swap64(x) 114122180Skan 115122180Skan#define cvmx_cpu_to_be16(x) (x) 116122180Skan#define cvmx_cpu_to_be32(x) (x) 117169689Skan#define cvmx_cpu_to_be64(x) (x) 118122180Skan 119122180Skan#else 120122180Skan 121122180Skan#define cvmx_cpu_to_le16(x) (x) 122122180Skan#define cvmx_cpu_to_le32(x) (x) 123169689Skan#define cvmx_cpu_to_le64(x) (x) 124122180Skan 125122180Skan#define cvmx_cpu_to_be16(x) cvmx_swap16(x) 126122180Skan#define cvmx_cpu_to_be32(x) cvmx_swap32(x) 127122180Skan#define cvmx_cpu_to_be64(x) cvmx_swap64(x) 128122180Skan 129132718Skan#endif 130122180Skan 131122180Skan#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