xref: /netbsd-src/common/lib/libc/arch/mips/gen/byte_swap_4.S (revision 8daf714e68d332607ba2c61d8c1ff3868c564c61)
1*8daf714eSmatt/*	$NetBSD: byte_swap_4.S,v 1.3 2009/12/14 00:39:00 matt Exp $	*/
237c9f0a6Schristos
337c9f0a6Schristos/*-
437c9f0a6Schristos * Copyright (c) 1991, 1993
537c9f0a6Schristos *	The Regents of the University of California.  All rights reserved.
637c9f0a6Schristos *
737c9f0a6Schristos * This code is derived from software contributed to Berkeley by
837c9f0a6Schristos * Ralph Campbell.
937c9f0a6Schristos *
1037c9f0a6Schristos * Redistribution and use in source and binary forms, with or without
1137c9f0a6Schristos * modification, are permitted provided that the following conditions
1237c9f0a6Schristos * are met:
1337c9f0a6Schristos * 1. Redistributions of source code must retain the above copyright
1437c9f0a6Schristos *    notice, this list of conditions and the following disclaimer.
1537c9f0a6Schristos * 2. Redistributions in binary form must reproduce the above copyright
1637c9f0a6Schristos *    notice, this list of conditions and the following disclaimer in the
1737c9f0a6Schristos *    documentation and/or other materials provided with the distribution.
1837c9f0a6Schristos * 3. Neither the name of the University nor the names of its contributors
1937c9f0a6Schristos *    may be used to endorse or promote products derived from this software
2037c9f0a6Schristos *    without specific prior written permission.
2137c9f0a6Schristos *
2237c9f0a6Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2337c9f0a6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2437c9f0a6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2537c9f0a6Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2637c9f0a6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2737c9f0a6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2837c9f0a6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2937c9f0a6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3037c9f0a6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3137c9f0a6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3237c9f0a6Schristos * SUCH DAMAGE.
3337c9f0a6Schristos */
3437c9f0a6Schristos
3537c9f0a6Schristos#include <mips/asm.h>
3637c9f0a6Schristos
3737c9f0a6Schristos#if defined(LIBC_SCCS) && !defined(lint)
38*8daf714eSmatt#if 0
39*8daf714eSmatt	RCSID("from: @(#)htonl.s	8.1 (Berkeley) 6/4/93")
40*8daf714eSmatt#else
41*8daf714eSmatt	RCSID("$NetBSD: byte_swap_4.S,v 1.3 2009/12/14 00:39:00 matt Exp $")
42*8daf714eSmatt#endif
4337c9f0a6Schristos#endif /* LIBC_SCCS and not lint */
4437c9f0a6Schristos
4537c9f0a6Schristos#undef _LOCORE
4637c9f0a6Schristos#define _LOCORE		/* XXX not really, just assembly-code source */
4737c9f0a6Schristos#include <machine/endian.h>
4837c9f0a6Schristos
491ee1a1e6Ssimonb#if defined(_KERNEL) || defined(_STANDALONE)
5037c9f0a6Schristos#define	BSWAP32_NAME	bswap32
5137c9f0a6Schristos#else
5237c9f0a6Schristos#define	BSWAP32_NAME	__bswap32
5337c9f0a6Schristos#endif
5437c9f0a6Schristos
5537c9f0a6SchristosNLEAF(BSWAP32_NAME)			# a0 = 0x11223344, return 0x44332211
5637c9f0a6Schristos#if BYTE_ORDER == LITTLE_ENDIAN
5737c9f0a6SchristosALEAF(htonl)				# a0 = 0x11223344, return 0x44332211
5837c9f0a6SchristosALEAF(ntohl)
5937c9f0a6Schristos#endif
60*8daf714eSmatt#if (__mips == 32 || __mips == 64) && __mips_isa_rev == 2
61*8daf714eSmatt	/*
62*8daf714eSmatt	 * If we are on MIPS32R2 or MIPS64R2 it's much easier
63*8daf714eSmatt	 */
64*8daf714eSmatt	wsbh	a0, a0			# word swap bytes within halfwords
65*8daf714eSmatt	rotr	v0, a0, 16		# rotate word 16bits
66*8daf714eSmatt	j	ra
67*8daf714eSmatt#else
6837c9f0a6Schristos	srl	v1, a0, 24		# v1 = 0x00000011
6937c9f0a6Schristos	sll	v0, a0, 24		# v0 = 0x44000000
7037c9f0a6Schristos	or	v0, v0, v1
7137c9f0a6Schristos	and	v1, a0, 0xff00
7237c9f0a6Schristos	sll	v1, v1, 8		# v1 = 0x00330000
7337c9f0a6Schristos	or	v0, v0, v1
7437c9f0a6Schristos	srl	v1, a0, 8
7537c9f0a6Schristos	and	v1, v1, 0xff00		# v1 = 0x00002200
7637c9f0a6Schristos	or	v0, v0, v1
7737c9f0a6Schristos	j	ra
78*8daf714eSmatt#endif
7937c9f0a6SchristosEND(BSWAP32_NAME)
8037c9f0a6Schristos
8137c9f0a6Schristos#if BYTE_ORDER == BIG_ENDIAN
8237c9f0a6SchristosNLEAF(htonl)				# a0 = 0x11223344, return 0x44332211
8337c9f0a6SchristosALEAF(ntohl)
8437c9f0a6Schristos	move	v0, a0
8537c9f0a6Schristos	j	ra
8637c9f0a6SchristosEND(htonl)
8737c9f0a6Schristos#endif
88