From patchwork Sat Dec 17 06:57:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 18531 Received: (qmail 93407 invoked by alias); 17 Dec 2016 06:57:54 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 92941 invoked by uid 89); 17 Dec 2016 06:57:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=bn, H*RU:74.125.83.65, Hx-spam-relays-external:74.125.83.65, simultaneously X-HELO: mail-pg0-f65.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=0OWt8ZDLtwmDBqVUGVR+4nQz83vfRypKpzBhlUGDln4=; b=cg28oKJbiNbnq94qYeP6cKEp3kqA2Lj7/CBtimshvLOfgbte/fNm9jSPhYg5GAaA9y qe2WhYw1idc5XdYi+0Iq8ZYK6SbCunx08Gprm6hrOlvKJlDyIxeqBrfwAZybSXMbBWNf v8xuy71Q5jw683rASIZRy2jTbDSus8FlDya938gNGt1ZKmFhcVYhKPjNlYKl1uVFimnR UfCq3lxsR2bnX14sD51FP20TBruQuZi4JbdxmNOy+5z0WMUYlgQGppXl/xTr453Izrvw 5LncLMliu+SUOg1O40V2kJ01jhUsJDuKQkDAWfuopqk+fRfNtiYW4o2X9PaFxY9+Tifp cNrQ== X-Gm-Message-State: AIkVDXIxlQCwuC52RYS+H7DB3e4Hltvm1XcklNQ3cApKuivazPlQeJkGbEYkbLh1GEGXqw== X-Received: by 10.84.132.129 with SMTP id e1mr8542560ple.167.1481957866882; Fri, 16 Dec 2016 22:57:46 -0800 (PST) From: Richard Henderson To: libc-alpha@sourceware.org Subject: [PATCH 10/11] hppa: Add haszero.h and whichzero.h Date: Fri, 16 Dec 2016 22:57:28 -0800 Message-Id: <20161217065729.28561-11-rth@twiddle.net> In-Reply-To: <20161217065729.28561-1-rth@twiddle.net> References: <20161217065729.28561-1-rth@twiddle.net> Use UXOR,SBZ to test for a zero byte within a word. While we can get semi-decent code out of asm-goto, we would do slightly better with a compiler builtin. For whichzero, sequential testing of bytes is less expensive than any tricks that involve a count-leading-zeros insn that we don't have. * sysdeps/hppa/haszero.h: New file. * sysdeps/hppa/whichzero.h: New file. --- sysdeps/hppa/haszero.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ sysdeps/hppa/whichzero.h | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 sysdeps/hppa/haszero.h create mode 100644 sysdeps/hppa/whichzero.h diff --git a/sysdeps/hppa/haszero.h b/sysdeps/hppa/haszero.h new file mode 100644 index 0000000..99cc6fc --- /dev/null +++ b/sysdeps/hppa/haszero.h @@ -0,0 +1,82 @@ +/* haszero.h -- function for zero byte detection. HPPA version. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef HASZERO_H +#define HASZERO_H 1 + +static inline unsigned long int +haszero(unsigned long int x) +{ +#if __GNUC_PREREQ(4, 5) + /* It's more useful to expose a control transfer to the compiler + than to expose a proper boolean result. */ + if (sizeof(x) == 8) + asm goto ("uxor,*sbz %%r0,%0,%%r0\n\tb,n %l1" : : "r"(x) : : nbz); + else + asm goto ("uxor,sbz %%r0,%0,%%r0\n\tb,n %l1" : : "r"(x) : : nbz); + return 1; + nbz: + return 0; +#else + unsigned long int ret; + if (sizeof(x) == 8) + asm ("uxor,*sbz %%r0,%1,%%r0\n\tcopy %%r0,%0" + : "=r"(ret) : "r"(x), "0"(1)); + else + asm ("uxor,sbz %%r0,%1,%%r0\n\tcopy %%r0,%0" + : "=r"(ret) : "r"(x), "0"(1)); + return ret; +#endif +} + +/* Likewise, but for two words simultaneously. */ + +static inline unsigned long int +haszero2(unsigned long int x1, unsigned long int x2) +{ +#if __GNUC_PREREQ(4, 5) + /* It's more useful to expose a control transfer to the compiler + than to expose a proper boolean result. */ + if (sizeof(x1) == 8) + asm goto ("uxor,*sbz %%r0,%0,%%r0\n\t" + "uxor,*nbz %%r0,%1,%%r0\n\t" + "b,n %l2" : : "r"(x1), "r"(x2) : : sbz); + else + asm goto ("uxor,sbz %%r0,%0,%%r0\n\t" + "uxor,nbz %%r0,%1,%%r0\n\t" + "b,n %l2" : : "r"(x1), "r"(x2) : : sbz); + return 0; + sbz: + return 1; +#else + unsigned long int ret; + if (sizeof(x1) == 8) + asm ("uxor,*sbz %%r0,%1,%%r0\n\t" + "uxor,*nbz %%r0,%2,%%r0\n\t" + "ldi 1,%0" + : "=r"(ret) : "r"(x1), "r"(x2), "0"(0)); + else + asm ("uxor,sbz %%r0,%1,%%r0\n\t" + "uxor,nbz %%r0,%2,%%r0\n\t" + "ldi 1,%0" + : "=r"(ret) : "r"(x1), "r"(x2), "0"(0)); + return ret; +#endif +} + +#endif /* haszero.h */ diff --git a/sysdeps/hppa/whichzero.h b/sysdeps/hppa/whichzero.h new file mode 100644 index 0000000..ef18cc7 --- /dev/null +++ b/sysdeps/hppa/whichzero.h @@ -0,0 +1,70 @@ +/* whichzero.h -- functions for zero byte searching. HPPA version. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef HPPA_WHICHZERO_H +#define HPPA_WHICHZERO_H 1 + +/* Given a long that is known to contain a zero byte, return the + index of the first such within the long in host memory order. */ + +static inline unsigned int +whichzero(unsigned long int x) +{ + unsigned int ret; + + _Static_assert (sizeof(x) == 4, "64-bit not supported"); + + /* Since we have no clz insn, direct tests of the bytes is faster + than loading up the constants to do the masking. */ + asm ("extrw,u,<> %1,23,8,%%r0\n\t" + "ldi 2,%0\n\t" + "extrw,u,<> %1,15,8,%%r0\n\t" + "ldi 1,%0\n\t" + "extrw,u,<> %1,7,8,%%r0\n\t" + "ldi 0,%0" + : "=r"(ret) : "r"(x), "0"(3)); + + return ret; +} + +/* Similarly, but perform the test for two longs simultaneously. */ + +static inline unsigned int +whichzero2(unsigned long int x1, unsigned long int x2) +{ + unsigned int ret; + + _Static_assert (sizeof(x1) == 4, "64-bit not supported"); + + /* Since we have no clz insn, direct tests of the bytes is faster + than loading up the constants to do the masking. */ + asm ("extrw,u,= %1,23,8,%%r0\n\t" + "extrw,u,<> %2,23,8,%%r0\n\t" + "ldi 2,%0\n\t" + "extrw,u,= %1,15,8,%%r0\n\t" + "extrw,u,<> %2,15,8,%%r0\n\t" + "ldi 1,%0\n\t" + "extrw,u,= %1,7,8,%%r0\n\t" + "extrw,u,<> %2,7,8,%%r0\n\t" + "ldi 0,%0" + : "=r"(ret) : "r"(x1), "r"(x2), "0"(3)); + + return ret; +} + +#endif /* whichzero.h */