Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

View file

@ -0,0 +1 @@
These files aren't intended to be compiled.

View file

@ -0,0 +1,56 @@
/*-*- 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/str/internal.h"
unsigned cescapec(int c) {
unsigned char ch = c;
switch (ch) {
case '\a':
return '\\' | 'a' << 8;
case '\b':
return '\\' | 'b' << 8;
case '\v':
return '\\' | 'v' << 8;
case '\f':
return '\\' | 'f' << 8;
case '\?':
return '\\' | '?' << 8;
case '\n':
return '\\' | 'n' << 8;
case '\r':
return '\\' | 'r' << 8;
case '\t':
return '\\' | 't' << 8;
case '\"':
return '\\' | '"' << 8;
case '\'':
return '\\' | '\'' << 8;
case '\\':
return '\\' | '\\' << 8;
default: {
if (ch >= 0x80 || !isprint(ch)) {
return '\\' | (ch / 64 + '0') << 8 | (ch % 64 / 8 + '0') << 16 |
(ch % 8 + '0') << 24;
} else {
return ch;
}
}
}
}

View file

@ -0,0 +1,57 @@
/*-*- 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/dce.h"
#include "libc/str/str.h"
#define N 32
typedef uint8_t uint8_v _Vector_size(N);
/**
* Searches for last instance of character in memory region.
*
* @param s is binary data to search
* @param c is treated as unsigned char
* @param n is byte length of s
* @return address of last c in s, or NULL if not found
*/
void *memrchr(const void *s, int c, size_t n) {
unsigned char ch = (unsigned char)c;
const unsigned char *p = (const unsigned char *)s;
if (n >= 32 && CheckAvx2()) {
uint8_v cv;
__builtin_memset(&cv, ch, sizeof(cv));
do {
uint32_t skip;
uint8_v sv, tv;
memcpy(&sv, s + n - N, N);
asm("vpcmpeqb\t%2,%3,%1\n\t"
"vpmovmskb\t%1,%0\n\t"
"lzcnt\t%0,%0"
: "=r"(skip), "=x"(tv)
: "x"(sv), "x"(cv));
n -= skip;
if (skip != 32) break;
} while (n >= 32);
}
while (n--) {
if (p[n] == ch) return (/* unconst */ void *)&p[n];
}
return NULL;
}

View file

@ -0,0 +1,62 @@
/*-*- 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};
asm(ZFLAG("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 */
: ZF(zf), "=x"(v1), "=x"(v2), "=r"(r1)
: "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;
}
}