mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-02 18:52:29 +00:00
Make improvements
- Invent openatemp() API - Invent O_UNLINK open flag - Introduce getenv_secure() API - Remove `git pull` from cosmocc - Fix utimes() when path is NULL - Fix mktemp() to never return NULL - Fix utimensat() UTIME_OMIT on XNU - Improve utimensat() code for RHEL5 - Turn `argv[0]` C:/ to /C/ on Windows - Introduce tmpnam() and tmpnam_r() APIs - Fix more const issues with internal APIs - Permit utimes() on WIN32 in O_RDONLY mode - Fix fdopendir() to check fd is a directory - Fix recent crash regression in landlock make - Fix futimens(AT_FDCWD, NULL) to return EBADF - Use workaround so `make -j` doesn't fork bomb - Rename dontdiscard to __wur (just like glibc) - Fix st_size for WIN32 symlinks containing UTF-8 - Introduce stdio ext APIs needed by GNU coreutils - Fix lstat() on WIN32 for symlinks to directories - Move some constants from normalize.inc to limits.h - Fix segv with memchr() and memcmp() overlapping page - Implement POSIX fflush() behavior for reader streams - Implement AT_SYMLINK_NOFOLLOW for utimensat() on WIN32 - Don't change read-only status of existing files on WIN32 - Correctly handle `0x[^[:xdigit:]]` case in strtol() functions
This commit is contained in:
parent
8596e83cce
commit
f531acc8f9
297 changed files with 1920 additions and 1681 deletions
115
libc/str/bcmp.c
115
libc/str/bcmp.c
|
@ -16,128 +16,15 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/likely.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
typedef uint64_t xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
|
||||
|
||||
#if !defined(__chibicc__)
|
||||
static int bcmp_sse(const char *p, const char *q, size_t n) {
|
||||
xmm_t a;
|
||||
while (n > 32) {
|
||||
a = *(const xmm_t *)p ^ *(const xmm_t *)q;
|
||||
if (a[0] | a[1]) return 1;
|
||||
p += 16;
|
||||
q += 16;
|
||||
n -= 16;
|
||||
}
|
||||
a = (*(const xmm_t *)p ^ *(const xmm_t *)q) |
|
||||
(*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16));
|
||||
return !!(a[0] | a[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
_Microarchitecture("avx") static int bcmp_avx(const char *p, const char *q,
|
||||
size_t n) {
|
||||
xmm_t a, b, c, d;
|
||||
if (n > 32) {
|
||||
if (n >= 16 + 64) {
|
||||
do {
|
||||
a = ((const xmm_t *)p)[0] ^ ((const xmm_t *)q)[0];
|
||||
b = ((const xmm_t *)p)[1] ^ ((const xmm_t *)q)[1];
|
||||
c = ((const xmm_t *)p)[2] ^ ((const xmm_t *)q)[2];
|
||||
d = ((const xmm_t *)p)[3] ^ ((const xmm_t *)q)[3];
|
||||
a = a | b | c | d;
|
||||
if (a[0] | a[1]) return 1;
|
||||
p += 64;
|
||||
q += 64;
|
||||
n -= 64;
|
||||
} while (n >= 16 + 64);
|
||||
}
|
||||
while (n > 16 + 16) {
|
||||
a = *(const xmm_t *)p ^ *(const xmm_t *)q;
|
||||
if (a[0] | a[1]) return 1;
|
||||
p += 16;
|
||||
q += 16;
|
||||
n -= 16;
|
||||
}
|
||||
}
|
||||
a = (*(const xmm_t *)p ^ *(const xmm_t *)q) |
|
||||
(*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16));
|
||||
return !!(a[0] | a[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Tests inequality of first 𝑛 bytes of 𝑝 and 𝑞.
|
||||
*
|
||||
* bcmp n=0 992 picoseconds
|
||||
* bcmp n=1 992 ps/byte 984 mb/s
|
||||
* bcmp n=2 661 ps/byte 1,476 mb/s
|
||||
* bcmp n=3 441 ps/byte 2,214 mb/s
|
||||
* bcmp n=4 330 ps/byte 2,952 mb/s
|
||||
* bcmp n=5 264 ps/byte 3,690 mb/s
|
||||
* bcmp n=6 165 ps/byte 5,905 mb/s
|
||||
* bcmp n=7 189 ps/byte 5,166 mb/s
|
||||
* bcmp n=8 124 ps/byte 7,873 mb/s
|
||||
* bcmp n=9 183 ps/byte 5,314 mb/s
|
||||
* bcmp n=15 110 ps/byte 8,857 mb/s
|
||||
* bcmp n=16 62 ps/byte 15,746 mb/s
|
||||
* bcmp n=17 175 ps/byte 5,577 mb/s
|
||||
* bcmp n=31 96 ps/byte 10,169 mb/s
|
||||
* bcmp n=32 93 ps/byte 10,497 mb/s
|
||||
* bcmp n=33 80 ps/byte 12,179 mb/s
|
||||
* bcmp n=80 37 ps/byte 26,244 mb/s
|
||||
* bcmp n=128 36 ps/byte 26,994 mb/s
|
||||
* bcmp n=256 27 ps/byte 35,992 mb/s
|
||||
* bcmp n=16384 19 ps/byte 49,411 mb/s
|
||||
* bcmp n=32768 27 ps/byte 34,914 mb/s
|
||||
* bcmp n=131072 30 ps/byte 32,303 mb/s
|
||||
*
|
||||
* @return 0 if a and b have equal contents, otherwise nonzero
|
||||
* @see timingsafe_bcmp()
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int bcmp(const void *a, const void *b, size_t n) {
|
||||
int c;
|
||||
uint32_t i, j;
|
||||
uint64_t x, y;
|
||||
const char *p, *q;
|
||||
if ((p = a) == (q = b)) return 0;
|
||||
if (!IsTiny()) {
|
||||
if (n <= 16) {
|
||||
if (n >= 8) {
|
||||
__builtin_memcpy(&x, p, 8);
|
||||
__builtin_memcpy(&y, q, 8);
|
||||
if (x ^ y) return 1;
|
||||
__builtin_memcpy(&x, p + n - 8, 8);
|
||||
__builtin_memcpy(&y, q + n - 8, 8);
|
||||
return !!(x ^ y);
|
||||
} else if (n >= 4) {
|
||||
__builtin_memcpy(&i, p, 4);
|
||||
__builtin_memcpy(&j, q, 4);
|
||||
if (i ^ j) return 1;
|
||||
__builtin_memcpy(&i, p + n - 4, 4);
|
||||
__builtin_memcpy(&j, q + n - 4, 4);
|
||||
return !!(i ^ j);
|
||||
}
|
||||
#ifndef __chibicc__
|
||||
#ifdef __x86_64__
|
||||
} else if (LIKELY(X86_HAVE(AVX))) {
|
||||
return bcmp_avx(p, q, n);
|
||||
#endif
|
||||
} else {
|
||||
return bcmp_sse(p, q, n);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
while (n--) {
|
||||
if ((c = p[n] ^ q[n])) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return memcmp(a, b, n);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue