xref: /minix3/common/lib/libc/arch/mips/gen/byte_swap_8.S (revision b6cbf7203b080219de306404f8022a65b7884f33)
1*b6cbf720SGianluca Guida/*	$NetBSD: byte_swap_8.S,v 1.2 2009/12/14 00:39:00 matt Exp $	*/
2*b6cbf720SGianluca Guida
3*b6cbf720SGianluca Guida/*-
4*b6cbf720SGianluca Guida * Copyright (c) 1991, 1993
5*b6cbf720SGianluca Guida *	The Regents of the University of California.  All rights reserved.
6*b6cbf720SGianluca Guida *
7*b6cbf720SGianluca Guida * This code is derived from software contributed to Berkeley by
8*b6cbf720SGianluca Guida * Ralph Campbell.
9*b6cbf720SGianluca Guida *
10*b6cbf720SGianluca Guida * Redistribution and use in source and binary forms, with or without
11*b6cbf720SGianluca Guida * modification, are permitted provided that the following conditions
12*b6cbf720SGianluca Guida * are met:
13*b6cbf720SGianluca Guida * 1. Redistributions of source code must retain the above copyright
14*b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer.
15*b6cbf720SGianluca Guida * 2. Redistributions in binary form must reproduce the above copyright
16*b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer in the
17*b6cbf720SGianluca Guida *    documentation and/or other materials provided with the distribution.
18*b6cbf720SGianluca Guida * 3. Neither the name of the University nor the names of its contributors
19*b6cbf720SGianluca Guida *    may be used to endorse or promote products derived from this software
20*b6cbf720SGianluca Guida *    without specific prior written permission.
21*b6cbf720SGianluca Guida *
22*b6cbf720SGianluca Guida * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*b6cbf720SGianluca Guida * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b6cbf720SGianluca Guida * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b6cbf720SGianluca Guida * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*b6cbf720SGianluca Guida * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*b6cbf720SGianluca Guida * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*b6cbf720SGianluca Guida * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*b6cbf720SGianluca Guida * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*b6cbf720SGianluca Guida * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*b6cbf720SGianluca Guida * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b6cbf720SGianluca Guida * SUCH DAMAGE.
33*b6cbf720SGianluca Guida */
34*b6cbf720SGianluca Guida
35*b6cbf720SGianluca Guida#include <mips/asm.h>
36*b6cbf720SGianluca Guida
37*b6cbf720SGianluca Guida#if defined(LIBC_SCCS) && !defined(lint)
38*b6cbf720SGianluca Guida	RCSID("$NetBSD: byte_swap_8.S,v 1.2 2009/12/14 00:39:00 matt Exp $")
39*b6cbf720SGianluca Guida#endif /* LIBC_SCCS and not lint */
40*b6cbf720SGianluca Guida
41*b6cbf720SGianluca Guida#undef _LOCORE
42*b6cbf720SGianluca Guida#define _LOCORE		/* XXX not really, just assembly-code source */
43*b6cbf720SGianluca Guida#include <machine/endian.h>
44*b6cbf720SGianluca Guida
45*b6cbf720SGianluca GuidaNLEAF(bswap64)	# a0 = 0xffeeddccbbaa9988 return 0x8899aabbccddeeff
46*b6cbf720SGianluca Guida#if (__mips == 32 || __mips == 64) && __mips_isa_rev == 2
47*b6cbf720SGianluca Guida#if !defined(__mips_o32)
48*b6cbf720SGianluca Guida	/*
49*b6cbf720SGianluca Guida	 * If we are on MIPS32r2 or MIPS64r2 use the new instructions.
50*b6cbf720SGianluca Guida	 */
51*b6cbf720SGianluca Guida	dsbh	v0, a0			# dwords swap bytes within halfwords
52*b6cbf720SGianluca Guida	dshd	v0, v0			# dwords swap halwords within dwords
53*b6cbf720SGianluca Guida	j	ra
54*b6cbf720SGianluca Guida#else /* defined(__mips_o32) */
55*b6cbf720SGianluca Guida	/*
56*b6cbf720SGianluca Guida	 * If we are on MIPS32r2 or MIPS64r2 use the new instructions.
57*b6cbf720SGianluca Guida	 * (except we must use the 32bit versions)
58*b6cbf720SGianluca Guida	 */
59*b6cbf720SGianluca Guida	wsbh	v1, a0			# word swap bytes within halfwords
60*b6cbf720SGianluca Guida	wsbh	v0, a1			# word swap bytes within halfwords
61*b6cbf720SGianluca Guida	rotr	v1, v1, 16		# rotate word 16bits and swap word
62*b6cbf720SGianluca Guida	rotr	v0, v0, 16		# rotate word 16bits and swap word
63*b6cbf720SGianluca Guida	j	ra
64*b6cbf720SGianluca Guida#endif /* defined(__mips_o32) */
65*b6cbf720SGianluca Guida#elif !defined(__mips_o32)
66*b6cbf720SGianluca Guida					# a0  = 0xffeeddccbbaa9988
67*b6cbf720SGianluca Guida	li	t0, 0xffff		# t0  = 0x000000000000ffff
68*b6cbf720SGianluca Guida	dsll	t1, t0, 32		# t1  = 0x0000ffff00000000
69*b6cbf720SGianluca Guida	or	t0, t1			# t0  = 0x0000ffff0000ffff
70*b6cbf720SGianluca Guida	dsll	t2, t0, 8		# t2  = 0x00ffff0000ffff00
71*b6cbf720SGianluca Guida	xor	t2, t0			# t2  = 0x00ff00ff00ff00ff
72*b6cbf720SGianluca Guida	/*
73*b6cbf720SGianluca Guida	 * We could swap by halfword, but that would be one instruction longer.
74*b6cbf720SGianluca Guida	 */
75*b6cbf720SGianluca Guida	dsrl	ta0, a0, 32		# ta0 = 0x00000000ffeeddcc
76*b6cbf720SGianluca Guida	dsll	ta1, a0, 32		# ta1 = 0xbbaa998800000000
77*b6cbf720SGianluca Guida	or	a1, ta0, ta1		# a1  = 0xbbaa9988ffeeddcc
78*b6cbf720SGianluca Guida					# words swapped
79*b6cbf720SGianluca Guida	and	ta0, a1, t0		# ta0 = 0x000099880000ddcc
80*b6cbf720SGianluca Guida	dsrl	ta1, a1, 16		# ta1 = 0x0000bbaa9988ffee
81*b6cbf720SGianluca Guida	and	ta1, t0			# ta1 = 0x0000bbaa0000ffee
82*b6cbf720SGianluca Guida	dsll	a2, ta0, 16		# a2  = 0x99880000ddcc0000
83*b6cbf720SGianluca Guida	or	a2, ta1			# a2  = 0x9988bbaaddccffee
84*b6cbf720SGianluca Guida					# halfwords swapped
85*b6cbf720SGianluca Guida	and	ta0, a2, t2		# ta0 = 0x008800aa00cc00ee
86*b6cbf720SGianluca Guida	dsrl	ta1, a2, 8		# ta1 = 0x009988bbaaddccff
87*b6cbf720SGianluca Guida	and	ta1, t2			# ta1 = 0x009900bb00dd00ff
88*b6cbf720SGianluca Guida	dsll	v0, ta0, 8		# v0  = 0x8800aa00cc00ee00
89*b6cbf720SGianluca Guida	or	v0, ta1			# v0  = 0x8899aabbccddeeff
90*b6cbf720SGianluca Guida					# bytes swapped
91*b6cbf720SGianluca Guida	j	ra
92*b6cbf720SGianluca Guida#else /* defined(__mips_o32) */
93*b6cbf720SGianluca Guida	/*
94*b6cbf720SGianluca Guida	 * 32bit ABI.
95*b6cbf720SGianluca Guida	 */
96*b6cbf720SGianluca Guida					# a0  = 0xccddeeff
97*b6cbf720SGianluca Guida					# a1  = 0x8899aabb
98*b6cbf720SGianluca Guida	srl	t0, a0, 24		# t0  = 0x000000cc
99*b6cbf720SGianluca Guida	srl	t1, a1, 24		# t1  = 0x00000088
100*b6cbf720SGianluca Guida	sll	ta0, a0, 24		# ta0 = 0xff000000
101*b6cbf720SGianluca Guida	sll	ta1, a1, 24		# ta1 = 0xbb000000
102*b6cbf720SGianluca Guida	or	ta0, ta0, t0		# ta0 = 0xff0000cc
103*b6cbf720SGianluca Guida	or	ta1, ta1, t1		# ta1 = 0xbb000088
104*b6cbf720SGianluca Guida	and	t0, a0, 0xff00		# t0  = 0x0000ee00
105*b6cbf720SGianluca Guida	and	t1, a1, 0xff00		# t1  = 0x0000aa00
106*b6cbf720SGianluca Guida	sll	t0, t0, 8		# t0  = 0x00ee0000
107*b6cbf720SGianluca Guida	sll	t1, t1, 8		# t1  = 0x00aa0000
108*b6cbf720SGianluca Guida	or	ta0, ta0, t0		# ta0 = 0xffee00cc
109*b6cbf720SGianluca Guida	or	ta1, ta1, t1		# ta1 = 0xbbaa0088
110*b6cbf720SGianluca Guida	srl	t0, a0, 8		# t0  = 0x00ccddee
111*b6cbf720SGianluca Guida	srl	t1, a1, 8		# t1  = 0x008899aa
112*b6cbf720SGianluca Guida	and	t0, t0, 0xff00		# t0  = 0x0000dd00
113*b6cbf720SGianluca Guida	and	t1, t1, 0xff00		# t1  = 0x00009900
114*b6cbf720SGianluca Guida	or	v1, ta0, t0		# v1  = 0xffeeddcc
115*b6cbf720SGianluca Guida	or	v0, ta1, t1		# v0  = 0xbbaa9988
116*b6cbf720SGianluca Guida	j	ra
117*b6cbf720SGianluca Guida#endif /* defined(__mips_o32) */
118*b6cbf720SGianluca GuidaEND(bswap64)
119