From patchwork Sat Sep 22 06:06:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 186096 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 44B162C0098 for ; Sat, 22 Sep 2012 16:07:16 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1348898836; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:From:To:Subject:Date:Message-ID:User-Agent: MIME-Version:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=I3Dt6KFMw1gQQk0klD/M64JL8og=; b=E5IPVeRwdxTe4LK /UAem3/KCqcRCplRvHXe3HigZ4+J3DLZwEYLwwoufyci6UzMuXZZGBs/cx3yvdiX MbqW6OS7Qve8VQ81FBy/aYNbX5jZyPQCpli52qeCF2nve5pLgZ3uAtT6FJfdMPAk Ids0cVqisoG6HrMlOuzldD+jjuG0= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=ZoXepER4sCuY3oGVRnFQz8fO95RlUw4KPeAk8pipB/zrUjgmfIYd7cSx3YKzio 8klNYXU/U/UF4J82b8UoiAf0wODY2rxcEw+KUlE1KgIWhZ7Qy6kSrFHk8w/Y4gvT MFTG6QJo5P5CLsd+LBOr6D9CFVobemB07YJ57Cw6ME2S0=; Received: (qmail 3594 invoked by alias); 22 Sep 2012 06:07:11 -0000 Received: (qmail 3581 invoked by uid 22791); 22 Sep 2012 06:07:10 -0000 X-SWARE-Spam-Status: No, hits=-4.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-pb0-f47.google.com (HELO mail-pb0-f47.google.com) (209.85.160.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 22 Sep 2012 06:06:57 +0000 Received: by pbcwy7 with SMTP id wy7so9636483pbc.20 for ; Fri, 21 Sep 2012 23:06:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=io9o0X0jpIplhaInRpceDpSreCZA0Wx47KEWFwgcHZQ=; b=n4T67HLFytf6jgK3RbVW4rLV6I2Kx/MiFGhxon0EYIesVgbcrzhtBnPbpn/rE1c97o IH9UT+gVgA+7XSLbd0/e5VB58l1l5dYKp2/8pvlsG6hG43cvrCFuwvHfb8jqfX6wZEWR jNAYUNd8j1unAoDOiEgMppTJSeAtRfdbNVmP8mbNpzppkefkJdoWZ1IdxhZj/GTEO6NW Z01iFp5wZJp/TyyMflKKrQ2QaVdZZ9B53V4XFpj7KskvkRyaz6GCESj6BH3lhLIBSMy9 DK0Ai8wvzWKTCxlDd4zTUeC2pUFTxcPag+EDSS1NmXtOwZUm8wK3Cw+z0En79MincRiy 4nNw== Received: by 10.66.78.199 with SMTP id d7mr17271806pax.77.1348294016902; Fri, 21 Sep 2012 23:06:56 -0700 (PDT) Received: by 10.66.78.199 with SMTP id d7mr17271798pax.77.1348294016807; Fri, 21 Sep 2012 23:06:56 -0700 (PDT) Received: from coign.google.com (adsl-71-133-8-30.dsl.pltn13.pacbell.net. [71.133.8.30]) by mx.google.com with ESMTPS id hc10sm6229556pbc.21.2012.09.21.23.06.55 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 21 Sep 2012 23:06:56 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: libgo patch committed: Return random number of hash of NaN Date: Fri, 21 Sep 2012 23:06:55 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQnwkAOn3ddGuLWMDCMmzUUcXqgSESKkU84vLS2iS0MKF/DYsOPWoWeir7n31evucTMG53YWX9A2INrstJ2oLcAinWgMhCw/aEYqOr2yFCgs9NT1euHiBsRaxmZX+jaqzf0eCFt4qAninV7UspQprrZELB/JGQNquEbY5qMRQzr/8Rtg05FiIA9tQj13jlvoEq+E8fg+ X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org It is valid to store a NaN in a Go hash table. However, the result can not be looked up, because NaN never equals NaN. The only way to see the result is to range over the map. This means that the hash code used for a NaN is irrelevant. In general it's better to not hash all NaN values to the same bucket, so this patch simply uses a random number. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline and 4.7 branch. Ian diff -r 895a5e834a7b libgo/runtime/go-type-complex.c --- a/libgo/runtime/go-type-complex.c Fri Sep 21 23:01:55 2012 -0700 +++ b/libgo/runtime/go-type-complex.c Fri Sep 21 23:03:04 2012 -0700 @@ -32,10 +32,14 @@ cf = ucf.cf; cfr = __builtin_crealf (cf); cfi = __builtin_cimagf (cf); - if (__builtin_isinff (cfr) || __builtin_isinff (cfi) - || __builtin_isnanf (cfr) || __builtin_isnanf (cfi)) + if (__builtin_isinff (cfr) || __builtin_isinff (cfi)) return 0; + /* NaN != NaN, so the hash code of a NaN is irrelevant. Make it + random so that not all NaNs wind up in the same place. */ + if (__builtin_isnanf (cfr) || __builtin_isnanf (cfi)) + return runtime_fastrand1 (); + /* Avoid negative zero. */ if (cfr == 0 && cfi == 0) return 0; @@ -62,10 +66,12 @@ cd = ucd.cd; cdr = __builtin_crealf (cd); cdi = __builtin_cimagf (cd); - if (__builtin_isinf (cdr) || __builtin_isinf (cdi) - || __builtin_isnan (cdr) || __builtin_isnan (cdi)) + if (__builtin_isinf (cdr) || __builtin_isinf (cdi)) return 0; + if (__builtin_isnan (cdr) || __builtin_isnan (cdi)) + return runtime_fastrand1 (); + /* Avoid negative zero. */ if (cdr == 0 && cdi == 0) return 0; diff -r 895a5e834a7b libgo/runtime/go-type-float.c --- a/libgo/runtime/go-type-float.c Fri Sep 21 23:01:55 2012 -0700 +++ b/libgo/runtime/go-type-float.c Fri Sep 21 23:03:04 2012 -0700 @@ -29,8 +29,14 @@ __builtin_memcpy (uf.a, vkey, 4); f = uf.f; - if (__builtin_isinff (f) || __builtin_isnanf (f) || f == 0) + if (__builtin_isinff (f) || f == 0) return 0; + + /* NaN != NaN, so the hash code of a NaN is irrelevant. Make it + random so that not all NaNs wind up in the same place. */ + if (__builtin_isnanf (f)) + return runtime_fastrand1 (); + return (uintptr_t) uf.si; } else if (key_size == 8) @@ -45,8 +51,12 @@ __builtin_memcpy (ud.a, vkey, 8); d = ud.d; - if (__builtin_isinf (d) || __builtin_isnan (d) || d == 0) + if (__builtin_isinf (d) || d == 0) return 0; + + if (__builtin_isnan (d)) + return runtime_fastrand1 (); + return (uintptr_t) ud.di; } else