cvmx-swap.h revision 210284
1/***********************license start*************** 2 * Copyright (c) 2003-2008 Cavium Networks (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 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 21 * permission. 22 * 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. 33 * 34 * 35 * For any questions regarding licensing please contact marketing@caviumnetworks.com 36 * 37 ***********************license end**************************************/ 38 39 40 41 42 43 44/** 45 * @file 46 * 47 * Utility functions for endian swapping 48 * 49 * <hr>$Revision: 32636 $<hr> 50 */ 51 52#ifndef __CVMX_SWAP_H__ 53#define __CVMX_SWAP_H__ 54 55#ifdef __cplusplus 56extern "C" { 57#endif 58 59 60/** 61 * Byte swap a 16 bit number 62 * 63 * @param x 16 bit number 64 * @return Byte swapped result 65 */ 66static inline uint16_t cvmx_swap16(uint16_t x) 67{ 68 return ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | 69 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) )); 70} 71 72 73/** 74 * Byte swap a 32 bit number 75 * 76 * @param x 32 bit number 77 * @return Byte swapped result 78 */ 79static inline uint32_t cvmx_swap32(uint32_t x) 80{ 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) )); 85} 86 87 88/** 89 * Byte swap a 64 bit number 90 * 91 * @param x 64 bit number 92 * @return Byte swapped result 93 */ 94static inline uint64_t cvmx_swap64(uint64_t x) 95{ 96 return ((x >> 56) | 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)); 104} 105 106 107#if __BYTE_ORDER == __BIG_ENDIAN 108 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) 112 113#define cvmx_cpu_to_be16(x) (x) 114#define cvmx_cpu_to_be32(x) (x) 115#define cvmx_cpu_to_be64(x) (x) 116 117#else 118 119#define cvmx_cpu_to_le16(x) (x) 120#define cvmx_cpu_to_le32(x) (x) 121#define cvmx_cpu_to_le64(x) (x) 122 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) 126 127#endif 128 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) 132 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) 136 137#ifdef __cplusplus 138} 139#endif 140 141#endif /* __CVMX_SWAP_H__ */ 142