xref: /openbmc/linux/arch/xtensa/include/uapi/asm/swab.h (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1*e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
283596729SDavid Howells /*
383596729SDavid Howells  * include/asm-xtensa/swab.h
483596729SDavid Howells  *
583596729SDavid Howells  * This file is subject to the terms and conditions of the GNU General Public
683596729SDavid Howells  * License.  See the file "COPYING" in the main directory of this archive
783596729SDavid Howells  * for more details.
883596729SDavid Howells  *
983596729SDavid Howells  * Copyright (C) 2001 - 2005 Tensilica Inc.
1083596729SDavid Howells  */
1183596729SDavid Howells 
1283596729SDavid Howells #ifndef _XTENSA_SWAB_H
1383596729SDavid Howells #define _XTENSA_SWAB_H
1483596729SDavid Howells 
1583596729SDavid Howells #include <linux/types.h>
1683596729SDavid Howells #include <linux/compiler.h>
1783596729SDavid Howells 
1883596729SDavid Howells #define __SWAB_64_THRU_32__
1983596729SDavid Howells 
__arch_swab32(__u32 x)2083596729SDavid Howells static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
2183596729SDavid Howells {
2283596729SDavid Howells     __u32 res;
2383596729SDavid Howells     /* instruction sequence from Xtensa ISA release 2/2000 */
2483596729SDavid Howells     __asm__("ssai     8           \n\t"
2583596729SDavid Howells 	    "srli     %0, %1, 16  \n\t"
2683596729SDavid Howells 	    "src      %0, %0, %1  \n\t"
2783596729SDavid Howells 	    "src      %0, %0, %0  \n\t"
2883596729SDavid Howells 	    "src      %0, %1, %0  \n"
2983596729SDavid Howells 	    : "=&a" (res)
3083596729SDavid Howells 	    : "a" (x)
3183596729SDavid Howells 	    );
3283596729SDavid Howells     return res;
3383596729SDavid Howells }
3483596729SDavid Howells #define __arch_swab32 __arch_swab32
3583596729SDavid Howells 
__arch_swab16(__u16 x)3683596729SDavid Howells static inline __attribute_const__ __u16 __arch_swab16(__u16 x)
3783596729SDavid Howells {
3883596729SDavid Howells     /* Given that 'short' values are signed (i.e., can be negative),
3983596729SDavid Howells      * we cannot assume that the upper 16-bits of the register are
4083596729SDavid Howells      * zero.  We are careful to mask values after shifting.
4183596729SDavid Howells      */
4283596729SDavid Howells 
4383596729SDavid Howells     /* There exists an anomaly between xt-gcc and xt-xcc.  xt-gcc
4483596729SDavid Howells      * inserts an extui instruction after putting this function inline
4583596729SDavid Howells      * to ensure that it uses only the least-significant 16 bits of
4683596729SDavid Howells      * the result.  xt-xcc doesn't use an extui, but assumes the
4783596729SDavid Howells      * __asm__ macro follows convention that the upper 16 bits of an
4883596729SDavid Howells      * 'unsigned short' result are still zero.  This macro doesn't
4983596729SDavid Howells      * follow convention; indeed, it leaves garbage in the upport 16
5083596729SDavid Howells      * bits of the register.
5183596729SDavid Howells 
5283596729SDavid Howells      * Declaring the temporary variables 'res' and 'tmp' to be 32-bit
5383596729SDavid Howells      * types while the return type of the function is a 16-bit type
5483596729SDavid Howells      * forces both compilers to insert exactly one extui instruction
5583596729SDavid Howells      * (or equivalent) to mask off the upper 16 bits. */
5683596729SDavid Howells 
5783596729SDavid Howells     __u32 res;
5883596729SDavid Howells     __u32 tmp;
5983596729SDavid Howells 
6083596729SDavid Howells     __asm__("extui    %1, %2, 8, 8\n\t"
6183596729SDavid Howells 	    "slli     %0, %2, 8   \n\t"
6283596729SDavid Howells 	    "or       %0, %0, %1  \n"
6383596729SDavid Howells 	    : "=&a" (res), "=&a" (tmp)
6483596729SDavid Howells 	    : "a" (x)
6583596729SDavid Howells 	    );
6683596729SDavid Howells 
6783596729SDavid Howells     return res;
6883596729SDavid Howells }
6983596729SDavid Howells #define __arch_swab16 __arch_swab16
7083596729SDavid Howells 
7183596729SDavid Howells #endif /* _XTENSA_SWAB_H */
72