From owner-freebsd-hackers@freebsd.org Sun Jan 6 16:54:23 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5B6A51494228 for ; Sun, 6 Jan 2019 16:54:23 +0000 (UTC) (envelope-from brian@aceshardware.com) Received: from mail.aceshardware.com (mail.aceshardware.com [52.52.37.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1180685AA9; Sun, 6 Jan 2019 16:54:21 +0000 (UTC) (envelope-from brian@aceshardware.com) Received: from [10.0.0.102] (unknown [71.145.208.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.aceshardware.com (Postfix) with ESMTPSA id D1B6920130F; Sun, 6 Jan 2019 16:54:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aceshardware.com; s=dkim; t=1546793652; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1q+mYsNbuhKhSVVhmDoG/69UFOui0O77ymbzJwimntc=; b=HwC15STyN3t0xg+DZfUgvTe2+2UNQl899HNUtmkVlBxnuYOxFCHy40JCEPd5g41xfq/mzT sPeQF6chf/TutgWH0SuSJb6MWwlPL/iGvfzlEjmvoxPlrVGdS433y9e97oCiSWGotRi3Gp +SnYzaJFVgnDAkihSpe/DrEtxwaP7NVxOlf5N/366jzmTd2fcUjJqDhK7BKelMdjyHOTtj Igeck//ulBRThS1jRlnIkN+hXnZiQHArxGLPviml9madNTDMdiHDjOydz0J9OvRacPyd8a EcSyTUr47UDs8ipneg+O7U0sqp2BkfVnHk0a/WUbao13UzUdWTBKGEnLQ7bqnQ== Subject: Re: Speculative: Rust for base system components To: Alan Somers Cc: "freebsd-hackers@freebsd.org" , Garance A Drosehn References: <7d7bc47d-04cf-2f9b-00a3-e3d9d92b3623@aceshardware.com> <72922F2C-9D27-47AA-BB1C-2DA8589CF008@rpi.edu> <92bd5362-d898-aa12-8f3d-9fbe23f38e0c@aceshardware.com> From: Brian Neal Message-ID: <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com> Date: Sun, 6 Jan 2019 08:54:08 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:65.0) Gecko/20100101 Thunderbird/65.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 1180685AA9 X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=aceshardware.com header.s=dkim header.b=HwC15STy; dmarc=pass (policy=none) header.from=aceshardware.com X-Spamd-Result: default: False [-3.86 / 15.00]; ARC_NA(0.00)[]; TO_DN_EQ_ADDR_SOME(0.00)[]; R_DKIM_ALLOW(-0.20)[aceshardware.com:s=dkim]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_MEDIUM(-1.00)[-0.998,0]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DKIM_TRACE(0.00)[aceshardware.com:+]; DMARC_POLICY_ALLOW(-0.50)[aceshardware.com,none]; MX_GOOD(-0.01)[mail.aceshardware.com]; R_SPF_NA(0.00)[]; NEURAL_HAM_SHORT(-0.57)[-0.572,0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; IP_SCORE(-0.48)[ipnet: 52.52.0.0/15(-1.08), asn: 16509(-1.25), country: US(-0.08)]; ASN(0.00)[asn:16509, ipnet:52.52.0.0/15, country:US]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 Jan 2019 16:54:23 -0000 I don't have it anymore, but yours is close enough. When compiling your first example, I get 156 instructions, including map iterators and rust result assert/unwrap logic. Your second example produces 21 lines, including a loop iterator: rustc 1.31.0 with -C opt-level=s: ::into_iter: mov eax, edi mov edx, esi ret example::oddcount2: xor eax, eax test edi, edi jle .LBB1_3 xor ecx, ecx .LBB1_2: mov edx, ecx and edx, 1 neg edx and edx, ecx lea esi, [rcx + 1] add eax, edx mov ecx, esi cmp edi, esi jne .LBB1_2 .LBB1_3: ret Converting to C: int oddcount2(int num) { int sum = 0; for (int i = 0; i <= num; ++i) { if (i % 2 > 0) { sum += i; } } return sum; } I get the following with gcc 8.2 using -Os: oddcount2(int): xor edx, edx xor eax, eax .L4: cmp edx, edi jg .L1 test dl, 1 je .L3 add eax, edx .L3: inc edx jmp .L4 .L1: ret Cheers, -Brian On 1/6/2019 8:01 AM, Alan Somers wrote: > I get different results on Godbolt. I don't know exactly what your > program was, but I tried to recreate it from your description. I > wrote it in two ways. With opt-level=s, I got 13 instructions for one > version, and 16 for the other. With opt-level=3, I got vectorized > code for both. Here's my code: > > pub fn oddcount(num: i32) -> i32 { > (0..num).map(|i| { > if i % 2 > 0 { > i > } else { > 0 > } > }).sum() > } > > pub fn oddcount2(num: i32) -> i32 { > let mut sum = 0; > for i in 0..num { > if i % 2 > 0 { > sum += i; > } > } > sum > } > > -Alan > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" >