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: 708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1385810wro; Wed, 24 Aug 2022 05:08:51 -0700 (PDT) X-Google-Smtp-Source: AA6agR6nNln7Sw6rx6Kyndy/a6iDWk3EBtrCDWEKTrYmWGPcCtEO1j55xWzyYwqnlOpa7W8amr8G X-Received: by 2002:a17:907:60c7:b0:731:2be4:f72d with SMTP id hv7-20020a17090760c700b007312be4f72dmr2772534ejc.639.1661342931247; Wed, 24 Aug 2022 05:08:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661342931; cv=none; d=google.com; s=arc-20160816; b=DD4XpZTHBNS5TibW8I9W2LwmCUWgxVVAPiLIyfGe/L0LgR0daqm++6V0c44X5+KH8f /d7IgicI7egIF80+6GMMdImxDI9VNKDu6Q6HZOsA824Oce3RNf6Rd977VES9iaWeWl5k snNusXncQWUInjZdvTryZZsBKigNWMKcRUooxzdiVomVE28O0mtS8rSdL8fDXY/gTwFS OSCh6z5oVAHGeHRWUNcyFSaaqQM55OcAgIfw/9Efj1K0fYl7olJtMMpvI6Aam+FzwfNj ruO7Aa0L8UMHVgbzf5Rpmc73UGR83SaQBjXZt2tS1XqTmmnhiW/oxJbc9NFJzr2uV9oQ 8p7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature:dmarc-filter :delivered-to; bh=t6aFODSRTT8upYExcW7j4bXWFKP99Dlm2IDdQsvVzPM=; b=kXMt+TS5cPns/9d+zAHR0Zx964JpIRNWz5kUuT+5yQBJVkGOpXvfEv29obz1UJ0g61 PISwijRh7GL6lMPvk0Xao6BMEWIs20jYxDjVw0c5+buGaQZRXz+G4W4rWC66BaIa2OEC WkFlm5yyKF8tHt/9ayyzucZSu3Ht1ZpLbn3Sgcfv5yRdmQ6CaKNinGNdAfcaKeOC9Kn0 U2+aY3rsnBtH48rPS08bSfCij6/l1L9Wcb3dArko/gE+t86KLc2BjzCaqGxspsXjj9Cn AXmNmx7t2vduX9D0RWrH1pVP22W9jy/v5A+x4reTEE5CyZcwXhDDq4JTSecNUSJEj+ES Wb0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=KMlYxvTj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gs32-20020a1709072d2000b00730a3b5af1fsi2480663ejc.338.2022.08.24.05.08.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:08:51 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=KMlYxvTj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 44A343959CA1 for ; Wed, 24 Aug 2022 12:03:20 +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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742044325633210144?= X-GMAIL-MSGID: =?utf-8?q?1742044325633210144?= 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