2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ This program is free software; you can redistribute it and/or modify │
|
|
|
|
│ it under the terms of the GNU General Public License as published by │
|
|
|
|
│ the Free Software Foundation; version 2 of the License. │
|
|
|
|
│ │
|
|
|
|
│ This program 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 │
|
|
|
|
│ General Public License for more details. │
|
|
|
|
│ │
|
|
|
|
│ You should have received a copy of the GNU General Public License │
|
|
|
|
│ along with this program; if not, write to the Free Software │
|
|
|
|
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
|
|
|
│ 02110-1301 USA │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/bits/bits.h"
|
|
|
|
#include "libc/str/internal.h"
|
|
|
|
|
|
|
|
#define kVectorSize 32 /* x86+avx2 is 256-bit cpu */
|
|
|
|
|
|
|
|
typedef uint8_t uint8_v _Vector_size(kVectorSize);
|
|
|
|
typedef uint32_t vbitmask_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns how many bytes the utf16 string would be as utf8.
|
|
|
|
*/
|
|
|
|
int strcmp$avx2(const char *s1, const char *s2) {
|
|
|
|
if (s1 == s2) return 0;
|
|
|
|
const unsigned char *p1 = (const unsigned char *)s1;
|
|
|
|
const unsigned char *p2 = (const unsigned char *)s2;
|
|
|
|
size_t i = -kVectorSize;
|
|
|
|
vLoop:
|
|
|
|
i += kVectorSize;
|
|
|
|
bLoop:
|
|
|
|
if (!IsPointerDangerous(p1 + i) && !IsPointerDangerous(p2 + i)) {
|
|
|
|
unsigned char zf;
|
|
|
|
vbitmask_t r1;
|
|
|
|
uint8_v v1, v2;
|
|
|
|
const uint8_v kZero = {0};
|
2020-07-01 02:55:47 +00:00
|
|
|
asm(ZFLAG_ASM("vmovdqu\t%5,%2\n\t" /* move because gcc problematic */
|
|
|
|
"vpcmpeqb\t%4,%2,%1\n\t" /* check for equality in p1 and p2 */
|
|
|
|
"vpcmpeqb\t%6,%2,%2\n\t" /* check for nul in p1 */
|
|
|
|
"vpandn\t%7,%1,%2\n\t" /* most complicated bitwise not ever */
|
|
|
|
"vpor\t%2,%1,%1\n\t" /* check for nul in p2 */
|
|
|
|
"pmovmskb\t%1,%3\n\t" /* turn 256 bits into 32 bits */
|
|
|
|
"bsf\t%3,%3") /* find stop byte */
|
|
|
|
: ZFLAG_CONSTRAINT(zf), "=x"(v1), "=x"(v2), "=r"(r1)
|
2020-06-15 14:18:57 +00:00
|
|
|
: "m"(*(const uint8_v *)(p1 + i)), "m"(*(const uint8_v *)(p2 + i)),
|
|
|
|
"x"(kZero), "m"(kVectorSize));
|
|
|
|
if (zf) goto vLoop;
|
|
|
|
return p1[i + r1] - p2[i + r1];
|
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
int c;
|
|
|
|
if (!(c = p1[i - 1] - p2[i - 1]) && p1[i - 1] + p1[i - 1] != 0) goto bLoop;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|