Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 6 Jan 2019 08:54:08 -0800
From:      Brian Neal <brian@aceshardware.com>
To:        Alan Somers <asomers@freebsd.org>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>, Garance A Drosehn <drosih@rpi.edu>
Subject:   Re: Speculative: Rust for base system components
Message-ID:  <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com>
In-Reply-To: <CAOtMX2gPrpO2O70pnwpU80bXMXo6LB9PCkU3bYw2ToMMnhf%2Bww@mail.gmail.com>
References:  <ca76e5f7-6e59-bd67-144a-90ad66f0252e@metricspace.net> <7d7bc47d-04cf-2f9b-00a3-e3d9d92b3623@aceshardware.com> <72922F2C-9D27-47AA-BB1C-2DA8589CF008@rpi.edu> <92bd5362-d898-aa12-8f3d-9fbe23f38e0c@aceshardware.com> <CAOtMX2gPrpO2O70pnwpU80bXMXo6LB9PCkU3bYw2ToMMnhf%2Bww@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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:

<I as core::iter::traits::IntoIterator>::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"
> 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?26325c0b-4960-7739-72aa-c31c4e0638d3>