2020-12-25 00:01:48 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-12-25 00:01:48 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2017 Google LLC │
|
|
|
|
│ │
|
|
|
|
│ Licensed under the Apache License, Version 2.0 (the "License"); │
|
|
|
|
│ you may not use this file except in compliance with the License. │
|
|
|
|
│ You may obtain a copy of the License at │
|
|
|
|
│ │
|
|
|
|
│ http://www.apache.org/licenses/LICENSE-2.0 │
|
|
|
|
│ │
|
|
|
|
│ Unless required by applicable law or agreed to in writing, software │
|
|
|
|
│ distributed under the License is distributed on an "AS IS" BASIS, │
|
|
|
|
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
|
|
|
│ See the License for the specific language governing permissions and │
|
|
|
|
│ limitations under the License. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/str/highwayhash64.h"
|
2023-11-28 22:24:28 +00:00
|
|
|
#include "libc/serialize.h"
|
2020-12-25 00:01:48 +00:00
|
|
|
|
Release Cosmopolitan v3.3
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.
This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.
Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.
OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().
This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.
We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 19:12:09 +00:00
|
|
|
__notice(highwayhash_notice, "\
|
|
|
|
HighwayHash (Apache 2.0)\n\
|
|
|
|
Copyright 2017 Google LLC");
|
2020-12-25 00:01:48 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint64_t v0[4];
|
|
|
|
uint64_t v1[4];
|
|
|
|
uint64_t mul0[4];
|
|
|
|
uint64_t mul1[4];
|
|
|
|
} HighwayHashState;
|
|
|
|
|
|
|
|
static void HighwayHashReset(const uint64_t key[4], HighwayHashState *state) {
|
2021-08-22 22:02:18 +00:00
|
|
|
state->mul0[0] = 0xdbe6d5d5fe4cce2f;
|
|
|
|
state->mul0[1] = 0xa4093822299f31d0;
|
|
|
|
state->mul0[2] = 0x13198a2e03707344;
|
|
|
|
state->mul0[3] = 0x243f6a8885a308d3;
|
|
|
|
state->mul1[0] = 0x3bd39e10cb0ef593;
|
|
|
|
state->mul1[1] = 0xc0acf169b5f18a8c;
|
|
|
|
state->mul1[2] = 0xbe5466cf34e90c6c;
|
|
|
|
state->mul1[3] = 0x452821e638d01377;
|
2020-12-25 00:01:48 +00:00
|
|
|
state->v0[0] = state->mul0[0] ^ key[0];
|
|
|
|
state->v0[1] = state->mul0[1] ^ key[1];
|
|
|
|
state->v0[2] = state->mul0[2] ^ key[2];
|
|
|
|
state->v0[3] = state->mul0[3] ^ key[3];
|
|
|
|
state->v1[0] = state->mul1[0] ^ ((key[0] >> 32) | (key[0] << 32));
|
|
|
|
state->v1[1] = state->mul1[1] ^ ((key[1] >> 32) | (key[1] << 32));
|
|
|
|
state->v1[2] = state->mul1[2] ^ ((key[2] >> 32) | (key[2] << 32));
|
|
|
|
state->v1[3] = state->mul1[3] ^ ((key[3] >> 32) | (key[3] << 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ZipperMergeAndAdd(const uint64_t v1, const uint64_t v0,
|
|
|
|
uint64_t *add1, uint64_t *add0) {
|
2021-08-22 22:02:18 +00:00
|
|
|
*add0 += (((v0 & 0xff000000) | (v1 & 0xff00000000)) >> 24) |
|
|
|
|
(((v0 & 0xff0000000000) | (v1 & 0xff000000000000)) >> 16) |
|
|
|
|
(v0 & 0xff0000) | ((v0 & 0xff00) << 32) |
|
|
|
|
((v1 & 0xff00000000000000) >> 8) | (v0 << 56);
|
|
|
|
*add1 += (((v1 & 0xff000000) | (v0 & 0xff00000000)) >> 24) | (v1 & 0xff0000) |
|
|
|
|
((v1 & 0xff0000000000) >> 16) | ((v1 & 0xff00) << 24) |
|
|
|
|
((v0 & 0xff000000000000) >> 8) | ((v1 & 0xff) << 48) |
|
|
|
|
(v0 & 0xff00000000000000);
|
2020-12-25 00:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Update(const uint64_t lanes[4], HighwayHashState *state) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
state->v1[i] += state->mul0[i] + lanes[i];
|
|
|
|
state->mul0[i] ^= (state->v1[i] & 0xffffffff) * (state->v0[i] >> 32);
|
|
|
|
state->v0[i] += state->mul1[i];
|
|
|
|
state->mul1[i] ^= (state->v0[i] & 0xffffffff) * (state->v1[i] >> 32);
|
|
|
|
}
|
|
|
|
ZipperMergeAndAdd(state->v1[1], state->v1[0], &state->v0[1], &state->v0[0]);
|
|
|
|
ZipperMergeAndAdd(state->v1[3], state->v1[2], &state->v0[3], &state->v0[2]);
|
|
|
|
ZipperMergeAndAdd(state->v0[1], state->v0[0], &state->v1[1], &state->v1[0]);
|
|
|
|
ZipperMergeAndAdd(state->v0[3], state->v0[2], &state->v1[3], &state->v1[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HighwayHashUpdatePacket(const uint8_t *packet,
|
|
|
|
HighwayHashState *state) {
|
|
|
|
uint64_t lanes[4];
|
2021-08-22 22:02:18 +00:00
|
|
|
lanes[0] = READ64LE(packet + 000);
|
|
|
|
lanes[1] = READ64LE(packet + 010);
|
|
|
|
lanes[2] = READ64LE(packet + 020);
|
|
|
|
lanes[3] = READ64LE(packet + 030);
|
2020-12-25 00:01:48 +00:00
|
|
|
Update(lanes, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Rotate32By(uint64_t count, uint64_t lanes[4]) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
uint32_t half0 = lanes[i] & 0xffffffff;
|
|
|
|
uint32_t half1 = (lanes[i] >> 32);
|
|
|
|
lanes[i] = (half0 << count) | (half0 >> (32 - count));
|
|
|
|
lanes[i] |= (uint64_t)((half1 << count) | (half1 >> (32 - count))) << 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HighwayHashUpdateRemainder(const uint8_t *bytes,
|
|
|
|
const size_t size_mod32,
|
|
|
|
HighwayHashState *state) {
|
|
|
|
int i;
|
|
|
|
const size_t size_mod4 = size_mod32 & 3;
|
|
|
|
const uint8_t *remainder = bytes + (size_mod32 & ~3);
|
|
|
|
uint8_t packet[32] = {0};
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
state->v0[i] += ((uint64_t)size_mod32 << 32) + size_mod32;
|
|
|
|
}
|
|
|
|
Rotate32By(size_mod32, state->v1);
|
|
|
|
for (i = 0; i < remainder - bytes; i++) {
|
|
|
|
packet[i] = bytes[i];
|
|
|
|
}
|
|
|
|
if (size_mod32 & 16) {
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
packet[28 + i] = remainder[i + size_mod4 - 4];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (size_mod4) {
|
|
|
|
packet[16 + 0] = remainder[0];
|
|
|
|
packet[16 + 1] = remainder[size_mod4 >> 1];
|
|
|
|
packet[16 + 2] = remainder[size_mod4 - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HighwayHashUpdatePacket(packet, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Permute(const uint64_t v[4], uint64_t *permuted) {
|
2021-08-22 22:02:18 +00:00
|
|
|
permuted[0] = v[2] >> 32 | v[2] << 32;
|
|
|
|
permuted[1] = v[3] >> 32 | v[3] << 32;
|
|
|
|
permuted[2] = v[0] >> 32 | v[0] << 32;
|
|
|
|
permuted[3] = v[1] >> 32 | v[1] << 32;
|
2020-12-25 00:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PermuteAndUpdate(HighwayHashState *state) {
|
|
|
|
uint64_t permuted[4];
|
|
|
|
Permute(state->v0, permuted);
|
|
|
|
Update(permuted, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t HighwayHashFinalize64(HighwayHashState *state) {
|
|
|
|
int i;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
PermuteAndUpdate(state);
|
2020-12-25 00:01:48 +00:00
|
|
|
return state->v0[0] + state->v1[0] + state->mul0[0] + state->mul1[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessAll(const uint8_t *data, size_t size, const uint64_t key[4],
|
|
|
|
HighwayHashState *state) {
|
|
|
|
size_t i;
|
|
|
|
HighwayHashReset(key, state);
|
|
|
|
for (i = 0; i + 32 <= size; i += 32) {
|
|
|
|
HighwayHashUpdatePacket(data + i, state);
|
|
|
|
}
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if ((size & 31) != 0)
|
|
|
|
HighwayHashUpdateRemainder(data + i, size & 31, state);
|
2020-12-25 00:01:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
/**
|
|
|
|
* Computes Highway Hash.
|
|
|
|
*
|
|
|
|
* highwayhash64 n=0 121 nanoseconds
|
|
|
|
* highwayhash64 n=8 16 ns/byte 59,865 kb/s
|
|
|
|
* highwayhash64 n=31 4 ns/byte 222 mb/s
|
|
|
|
* highwayhash64 n=32 3 ns/byte 248 mb/s
|
|
|
|
* highwayhash64 n=63 2 ns/byte 387 mb/s
|
|
|
|
* highwayhash64 n=64 2 ns/byte 422 mb/s
|
|
|
|
* highwayhash64 n=128 1 ns/byte 644 mb/s
|
|
|
|
* highwayhash64 n=256 1 ns/byte 875 mb/s
|
|
|
|
* highwayhash64 n=22851 721 ps/byte 1,354 mb/s
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint64_t HighwayHash64(const void *data, size_t size, const uint64_t key[4]) {
|
2020-12-25 00:01:48 +00:00
|
|
|
HighwayHashState state;
|
|
|
|
ProcessAll(data, size, key, &state);
|
|
|
|
return HighwayHashFinalize64(&state);
|
|
|
|
}
|