1fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes/*	$NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $	*/
257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
3fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes/*
4fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes * Copyright (c) 1988, 1993
557df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *	The Regents of the University of California.  All rights reserved.
657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *
7fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes * This code is derived from software contributed to Berkeley by
8fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes * Jeffrey Mogul.
9fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes *
1057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * Redistribution and use in source and binary forms, with or without
1157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * modification, are permitted provided that the following conditions
1257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * are met:
1357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 1. Redistributions of source code must retain the above copyright
1457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *    notice, this list of conditions and the following disclaimer.
1557df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 2. Redistributions in binary form must reproduce the above copyright
1657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *    notice, this list of conditions and the following disclaimer in the
1757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *    documentation and/or other materials provided with the distribution.
1857df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 3. Neither the name of the University nor the names of its contributors
1957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *    may be used to endorse or promote products derived from this software
2057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *    without specific prior written permission.
2157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey *
2257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2557df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2857df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * SUCH DAMAGE.
3357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey */
3457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
35fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#include <sys/cdefs.h>
36fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#if defined(LIBC_SCCS) && !defined(lint)
37fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#if 0
38fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughesstatic char sccsid[] = "@(#)swab.c	8.1 (Berkeley) 6/4/93";
39fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#else
40fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes__RCSID("$NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $");
41fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#endif
42fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#endif /* LIBC_SCCS and not lint */
4357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
44fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#include <assert.h>
45fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#include <unistd.h>
4657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
47fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughesvoid
48fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughesswab(const void * __restrict from, void * __restrict to, ssize_t len)
49fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes{
50fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	char temp;
51fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	const char *fp;
52fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	char *tp;
5357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
54fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	if (len <= 1)
55fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		return;
56fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes
57fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	_DIAGASSERT(from != NULL);
58fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	_DIAGASSERT(to != NULL);
59fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes
60fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	len /= 2;
61fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	fp = (const char *)from;
62fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	tp = (char *)to;
63fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes#define	STEP	temp = *fp++,*tp++ = *fp++,*tp++ = temp
64fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes
65fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	if (__predict_false(len == 1)) {
66fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		STEP;
67fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		return;
68fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	}
6957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey
70fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	/* round to multiple of 8 */
71fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	while ((--len % 8) != 0)
72fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		STEP;
73fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	len /= 8;
74fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	if (len == 0)
75fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		return;
76fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	while (len-- != 0) {
77fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		STEP; STEP; STEP; STEP;
78fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes		STEP; STEP; STEP; STEP;
79fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes	}
80fd4c6b0a3a25921a9fe24691a695d715aecb6afeElliott Hughes}
81