Browse Source

update swab() to match the current posix definition. "rationale: none."

rewrite the function to be simpler as well. the compiler can unroll the
loop for us if necessary.
ok schwarze
OPENBSD_5_7
tedu 9 years ago
parent
commit
628b1ad074
2 changed files with 26 additions and 52 deletions
  1. +2
    -2
      src/include/unistd.h
  2. +24
    -50
      src/lib/libc/string/swab.c

+ 2
- 2
src/include/unistd.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: unistd.h,v 1.95 2014/12/08 20:39:56 tedu Exp $ */
/* $OpenBSD: unistd.h,v 1.96 2014/12/11 23:05:38 tedu Exp $ */
/* $NetBSD: unistd.h,v 1.26.4.1 1996/05/28 02:31:51 mrg Exp $ */
/*-
@ -424,7 +424,7 @@ char *mktemp(char *);
int nice(int);
int setregid(gid_t, gid_t);
int setreuid(uid_t, uid_t);
void swab(const void *, void *, size_t);
void swab(const void *__restrict, void *__restrict, ssize_t);
void sync(void);
int truncate(const char *, off_t);
useconds_t ualarm(useconds_t, useconds_t);


+ 24
- 50
src/lib/libc/string/swab.c View File

@ -1,61 +1,35 @@
/* $OpenBSD: swab.c,v 1.8 2008/03/15 21:54:09 ray Exp $ */
/* $OpenBSD: swab.c,v 1.9 2014/12/11 23:05:38 tedu Exp $ */
/*
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
* Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
*
* This code is derived from software contributed to Berkeley by
* Jeffrey Mogul.
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <unistd.h>
void
swab(const void *from, void *to, size_t len)
swab(const void *__restrict from, void *__restrict to, ssize_t len)
{
size_t n;
char *fp, *tp;
char temp;
const unsigned char *src = from;
unsigned char *dst = to;
unsigned char t0, t1;
n = (len / 2) + 1;
fp = (char *)from;
tp = (char *)to;
#define STEP do { \
temp = *fp++; \
*tp++ = *fp++; \
*tp++ = temp; \
} while (0)
/* round to multiple of 8 */
while ((--n) & 07)
STEP;
n >>= 3;
if (n == 0)
return;
while (n-- != 0) {
STEP; STEP; STEP; STEP;
STEP; STEP; STEP; STEP;
while (len > 1) {
t0 = src[0];
t1 = src[1];
dst[0] = t1;
dst[1] = t0;
src += 2;
dst += 2;
len -= 2;
}
}

Loading…
Cancel
Save