From patchwork Fri Aug 8 23:24:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulrich Drepper X-Patchwork-Id: 378700 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D6EB4140114 for ; Sat, 9 Aug 2014 09:25:04 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; q=dns; s=default; b=uyQFraw/7nx01tDf lveHIoN/mx/paGXa4RuXeyjKF2reCz7NGCXulgiIuwWOW0M7GqJCn+3njt4eM++J 9rA3MChnjGvUnTkol9VkK/mMVlQQ4LXnL9HBPC2WSegMGxNbjYnI/MSCaiJecOg/ /pvsvBx2t+u1lD92AFNuauAFKgg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; s=default; bh=tuof6hS9wsibJQDhhjRXWw AsULs=; b=FUInTb2WJbzXXNUUnmF3x63OZfLnhcqaDLPLejresf/ccs2EJIMXT4 WatuIIgFgjuL/DyY3WbkufDCeII3+H6VhbvelFOM1oB3lTjq/U6d3T/qnwCPxTrp 7+o5D41h/p2Cd6/COuonUGQVpR1L/w8fzPPAFMOqq+umaQgnre5Cg= Received: (qmail 31736 invoked by alias); 8 Aug 2014 23:24:58 -0000 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 Received: (qmail 31716 invoked by uid 89); 8 Aug 2014 23:24:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-qa0-f43.google.com Received: from mail-qa0-f43.google.com (HELO mail-qa0-f43.google.com) (209.85.216.43) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 08 Aug 2014 23:24:56 +0000 Received: by mail-qa0-f43.google.com with SMTP id w8so6287198qac.30 for ; Fri, 08 Aug 2014 16:24:54 -0700 (PDT) X-Received: by 10.224.223.199 with SMTP id il7mr5736388qab.10.1407540294543; Fri, 08 Aug 2014 16:24:54 -0700 (PDT) Received: from x240.local (cpe-68-173-15-30.nyc.res.rr.com. [68.173.15.30]) by mx.google.com with ESMTPSA id k3sm1013611qge.1.2014.08.08.16.24.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Aug 2014 16:24:54 -0700 (PDT) From: Ulrich Drepper X-Google-Original-From: Ulrich Drepper Received: from x240.local (localhost.localdomain [127.0.0.1]) by x240.local (8.14.8/8.14.8) with ESMTP id s78NOqeD029164; Fri, 8 Aug 2014 19:24:53 -0400 Received: (from drepper@localhost) by x240.local (8.14.8/8.14.8/Submit) id s78NOqPs029163; Fri, 8 Aug 2014 19:24:52 -0400 To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCH] libstdc++: add uniform on sphere distribution References: <87a98eow4m.fsf@x240.local.i-did-not-set--mail-host-address--so-tickle-me> <20140723102908.GM2361@redhat.com> Date: Fri, 08 Aug 2014 19:24:52 -0400 In-Reply-To: <20140723102908.GM2361@redhat.com> (Jonathan Wakely's message of "Wed, 23 Jul 2014 11:29:08 +0100") Message-ID: <87d2catvi3.fsf@x240.local.i-did-not-set--mail-host-address--so-tickle-me> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Jonathan Wakely writes: > On 23/07/14 11:58 +0200, Marc Glisse wrote: > As an aside, we already have divide-by-zero bugs in , it > would be nice if someone could look at that. > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60037 Sorry, it took a while to get back to tihs and now the referenced bug is already fixed. Good. Now also for a fix of the sphere distribution. Unless someone objects I'll check in the patch below. 2014-08-08 Ulrich Drepper * include/ext/random.tcc (uniform_on_sphere_distribution::__generate_impl): Reject vectors with norm zero. diff --git a/libstdc++-v3/include/ext/random.tcc b/libstdc++-v3/include/ext/random.tcc index 05361d8..d1f0b9c 100644 --- a/libstdc++-v3/include/ext/random.tcc +++ b/libstdc++-v3/include/ext/random.tcc @@ -1548,13 +1548,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const param_type& __p) { result_type __ret; - _RealType __sum = _RealType(0); + _RealType __norm; + + do + { + _RealType __sum = _RealType(0); + + std::generate(__ret.begin(), __ret.end(), + [&__urng, &__sum, this](){ + _RealType __t = _M_nd(__urng); + __sum += __t * __t; + return __t; }); + __norm = std::sqrt(__sum); + } + while (__norm == _RealType(0)); - std::generate(__ret.begin(), __ret.end(), - [&__urng, &__sum, this](){ _RealType __t = _M_nd(__urng); - __sum += __t * __t; - return __t; }); - auto __norm = std::sqrt(__sum); std::transform(__ret.begin(), __ret.end(), __ret.begin(), [__norm](_RealType __val){ return __val / __norm; });