/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matt Thomas of 3am Software Foundry.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* We have loaded the final bytes in x4 and x6 in host-endian. Now we have
* to figure what the difference is (if any). First we subtract. Any bytes
* that are the same will be 0. So to find the first non-zero byte we byterev
* and then use clz to find that byte.
* We mask the location to get the start of the byte. We shift both
* data dwords left to remove the equal part. Then we shift right to discard
* the trailing bytes. Then we subtract and return.
*/
Lmemcmp_last_compare0:
subs x0, x4, x6
b.eq .Lmemcmp_ret
Lmemcmp_last_compare:
#if __AARCH64EB__
clz x1, x0 /* find first non-zero byte */
rev x0, x0
#else
rev x1, x0
clz x1, x1 /* find first non-zero byte */
#endif
bfi x1, xzr, #0, #3 /* make it byte aligned */
lsr x1, x0, x1 /* shift to LSB */
#if __AARCH64EL__
rev x4, x4 /* byte reverse */
rev x6, x6 /* byte reverse */
#endif
subs x0, x4, x6
csetm x0, cc /* set mask bits as sign */
bfm x0, x1, #0, #7 /* extend with sign bit */
ret
END(memcmp)