From patchwork Wed Aug 24 11:59:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: herron.philip@googlemail.com X-Patchwork-Id: 56986 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F9763952498 for ; Wed, 24 Aug 2022 12:03:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 8DA083882159; Wed, 24 Aug 2022 12:00:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8DA083882159 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-wr1-x42a.google.com with SMTP id az27so465272wrb.6; Wed, 24 Aug 2022 05:00:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc; bh=t6aFODSRTT8upYExcW7j4bXWFKP99Dlm2IDdQsvVzPM=; b=KMlYxvTjMELGSE5BnzWfI3EhzvTk8vTrEjkbPAuCvpAFMV4Pe540bTzAmMLEwmHoI6 BEe8syYJqCMTPIhVHltBk6dcKXMRv2s7z7E8tVYZkRTKNaTXv0FIsg9EEqegtdr+vNvi HNyjioLrAHx8ZzSzBsDszZax13BdHhjZy0lx/A44yoXP0i3YS+YDPqCyv/q9QBIFocfq VF3vIlBeBLqkKJe9tzEYaBvEmOlWjyvRIWcl0N6xNUZ78qKeJS4XJkZe2DmsNsCgN5Ww RF6LX6Ar6XI0h9xom+GG6YpywLAEQXryCNmn2+vP7ACsdL8KABFFCmaV5LILcwuCG4P0 aPew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc; bh=t6aFODSRTT8upYExcW7j4bXWFKP99Dlm2IDdQsvVzPM=; b=GpxeRRt8D0PFhmya0hvOjnGO4ppDsKPWEv88zjYP80L6yECVsvXRTovvnOO8nglo5J 0pV+PeyNx+iS7ysTkfuy5S3/sZgtKYwOruWusSnr1zVrYG5glQRtw1ioHT59kvsRsfMi 7v5n+5YVVHTpc+Q50cgQXqbzv1ZFuuq8wXAWaFhtpNr7jpCULxLXa75xblDaLPCa1czu 01jXBVe2JkD8n/+vNGIPxuff9FzeGKxj2VeX/6Zt2M+BGa3aNIxWfHSQIb+114hTApL2 2wwqZ+vrGXXi6WAutROUs/+RZBJM8jvNkbCVAsNGgvTP+IddZ75VS3Y8DX5h1mxOBL+l 9ifw== X-Gm-Message-State: ACgBeo3ef6/UecU/78C3I8ZDmdXlmSBu+VJLClOTlfer4rvc1+T5nQtj w2m2OPHZwpIIDI7r4bto5CCa5RExcE8= X-Google-Smtp-Source: AA6agR5ZcUoPENdL4We5qL7HiySQvUz265NZCBSi7MnhhCSdORVsv2J8essCBUlxxMAFxFMq2EISNQ== X-Received: by 2002:adf:eb10:0:b0:225:70d5:e994 with SMTP id s16-20020adfeb10000000b0022570d5e994mr1640735wrn.425.1661342453092; Wed, 24 Aug 2022 05:00:53 -0700 (PDT) Received: from localhost.localdomain ([86.14.124.218]) by smtp.gmail.com with ESMTPSA id cc19-20020a5d5c13000000b0022571d43d32sm1697676wrb.21.2022.08.24.05.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:00:52 -0700 (PDT) From: herron.philip@googlemail.com X-Google-Original-From: philip.herron@embecosm.com To: gcc-patches@gcc.gnu.org Subject: [PATCH Rust front-end v2 16/37] gccrs: Add port of FNV hash used during legacy symbol mangling Date: Wed, 24 Aug 2022 12:59:35 +0100 Message-Id: <20220824115956.737931-17-philip.herron@embecosm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220824115956.737931-1-philip.herron@embecosm.com> References: <20220824115956.737931-1-philip.herron@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: philip.herron@embecosm.com Cc: gcc-rust@gcc.gnu.org, Philip Herron Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Philip Herron This hash was ported from the go runime as we needed a hash for the legacy symbol mangling system. Which means all symbols in Rust contain a hash of some metadata for uniqueness on generic functions. --- gcc/rust/util/fnv-hash.h | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 gcc/rust/util/fnv-hash.h diff --git a/gcc/rust/util/fnv-hash.h b/gcc/rust/util/fnv-hash.h new file mode 100644 index 00000000000..78e54c99411 --- /dev/null +++ b/gcc/rust/util/fnv-hash.h @@ -0,0 +1,95 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC 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; either version 3, or (at your option) any later +// version. + +// GCC 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 GCC; see the file COPYING3. If not see +// . + +#ifndef RUST_FNV_HASH_H +#define RUST_FNV_HASH_H + +namespace Rust { +namespace Hash { + +const uint64_t offset128Lower = 0x62b821756295c58d; +const uint64_t offset128Higher = 0x6c62272e07bb0142; +const uint64_t prime128Lower = 0x13b; +const uint64_t prime128Shift = 24; + +// ported from https://github.com/golang/go/blob/master/src/hash/fnv/fnv.go +class FNV128 +{ +public: + FNV128 () { reset (); } + + void reset () + { + buf[0] = offset128Higher; + buf[1] = offset128Lower; + } + + void write (const unsigned char *in, size_t len) + { + for (size_t i = 0; i < len; i++) + { + unsigned char c = in[i]; + + // https://stackoverflow.com/questions/28868367/getting-the-high-part-of-64-bit-integer-multiplication + uint64_t a = prime128Lower; + uint64_t b = buf[1]; + + uint64_t a_lo = (uint32_t) a; + uint64_t a_hi = a >> 32; + uint64_t b_lo = (uint32_t) b; + uint64_t b_hi = b >> 32; + + uint64_t a_x_b_hi = a_hi * b_hi; + uint64_t a_x_b_mid = a_hi * b_lo; + uint64_t b_x_a_mid = b_hi * a_lo; + uint64_t a_x_b_lo = a_lo * b_lo; + + uint64_t carry_bit + = ((uint64_t) (uint32_t) a_x_b_mid + (uint64_t) (uint32_t) b_x_a_mid + + (a_x_b_lo >> 32)) + >> 32; + + uint64_t multhi + = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit; + + uint64_t s0 = multhi; // high + uint64_t s1 = prime128Lower * buf[1]; // low + + s0 += buf[1] << (prime128Shift + prime128Lower * buf[0]); + + // Update the values + buf[1] = s1; + buf[0] = s0; + buf[1] ^= (uint64_t) c; + } + } + + void sum (uint64_t *hi, uint64_t *lo) const + { + *hi = buf[0]; + *lo = buf[1]; + } + +private: + uint64_t buf[2]; +}; + +} // namespace Hash +} // namespace Rust + +#endif // RUST_FNV_HASH_H