FAQ
Would it be possible to implement C-style arithmetic IF, e.g.

c = (a < b) ? a : b;

?

It cleans-up arithmetic-intensive code and could be directly compiled to
SEL or Conditional MOV instruction on some architectures.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Andrey mirtchovski at Aug 19, 2013 at 2:49 pm
    the FAQ is a bit dry on the subject (
    http://golang.org/doc/faq#Does_Go_have_a_ternary_form) but you can search
    the list for previous discussions using the term "ternary". for example:

    https://groups.google.com/d/msg/golang-nuts/CllsRgTaL7Q/CqEQX7UoVRkJ

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at Aug 19, 2013 at 2:50 pm

    On Mon, Aug 19, 2013 at 6:01 AM, wrote:
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?
    It's been discussed several times on the list.

    It cleans-up arithmetic-intensive code and could be directly compiled to SEL
    or Conditional MOV instruction on some architectures.
    Don't confuse the way that code is written with the generated code.
    An if statement can be compiled to a conditional move just as easily.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Mykola Dvornik at Aug 19, 2013 at 9:50 pm

    Don't confuse the way that code is written with the generated code.
    An if statement can be compiled to a conditional move just as easily.
    Just to give a small hint to go developers, please consider the
    following code:

    package main

    import (
         "math/rand"
         "fmt"
         "time"
    )

    const size = 1024 * 1024 * 128

    func main() {

      in := make([]{{.T}}, size)

      for i := 0; i < size; i++ {
       in[i] = rand.{{.T}}()
      }

         val := {{.T}}(0)

         t0 := time.Now()
      for i := 0; i < size; i++ {
       val = in[i]
       if val < {{.CUTOFF}} {
        val = {{.T}}({{.BLACK}})
       } else {
        val = {{.T}}({{.WHITE}})
       }
       in[i] = val
         }

         t1 := time.Now()
         fmt.Printf("%v\n", t1.Sub(t0))
    }

    . 6g (ms) gccgo -O1 (ms) %
    int 351.5920684545 292.4339090909 -16.8257946271
         ±0.9898938326 ±4.4000099194

    float32(x86_64) 1201.2026646 462.5641818182 %
         ±2.8027491 ±0.518607138 -61.9157877765

    float64(x86_64) 1221.0975032 478.8619090909 %
         ±9.5220245 ±4.4022106595 -60.7843020040


    As Arne has already pointed out 6g produces straightforward branching,
    while gccgo does branching-less cmov and SSE magic for int and float
    types, respectively.

    The SSE magic is SSE2 only as required by x86_64. No SSE2+, AVX or
    vectorization.

    On Mon, 19 Aug, 2013 at 4:49 PM, Ian Lance Taylor wrote:
    On Mon, Aug 19, 2013 at 6:01 AM, wrote:
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?
    It's been discussed several times on the list.

    It cleans-up arithmetic-intensive code and could be directly
    compiled to SEL
    or Conditional MOV instruction on some architectures.
    Don't confuse the way that code is written with the generated code.
    An if statement can be compiled to a conditional move just as easily.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Andreas Jellinghaus at Aug 19, 2013 at 3:21 pm
    2013/8/19 <mykola.dvornik@gmail.com>
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?

    It cleans-up arithmetic-intensive code and could be directly compiled to
    SEL or Conditional MOV instruction on some architectures.
       c = map[bool]int{true: a, false: b}[a < b]

    Andreas

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Aug 19, 2013 at 3:28 pm
    On 19 August 2013 16:21, Andreas Jellinghaus wrote:

    2013/8/19 <mykola.dvornik@gmail.com>
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?

    It cleans-up arithmetic-intensive code and could be directly compiled to
    SEL or Conditional MOV instruction on some architectures.
    c = map[bool]int{true: a, false: b}[a < b]

    This DOES NOT WORK in general. `a` and `b` are not variable names,
    they stand for entire expressions, expressions which may fail to evaluate
    if the boolean expression `c` has the inappropriate value. One of the points
    of an if-expression is that the unselected expression isn't evaluated.

    Chris

    --
    Chris "seeing a thing" Dollin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gustavo Niemeyer at Aug 19, 2013 at 7:02 pm

    On Mon, Aug 19, 2013 at 12:21 PM, Andreas Jellinghaus wrote:
    c = map[bool]int{true: a, false: b}[a < b]
          if a < b { c = a } else { c = b }

    :-)


    gustavo @ http://niemeyer.net

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Aug 19, 2013 at 7:31 pm

    On 19 August 2013 20:01, Gustavo Niemeyer wrote:

    On Mon, Aug 19, 2013 at 12:21 PM, Andreas Jellinghaus
    wrote:
    c = map[bool]int{true: a, false: b}[a < b]
    if a < b { c = a } else { c = b }

    :-)
    [Hoping of course that a and b have no side-effects, and that c is
      sufficiently simple that the eye can see that it really is the same
      expression both times,...]

    Chris

    --
    Chris "Caveats are poor substitutes for if-expressions." Dollin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • A Vansteenkiste at Aug 19, 2013 at 5:53 pm

    On Monday, August 19, 2013 3:01:36 PM UTC+2, mykola....@gmail.com wrote:

    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?

    It cleans-up arithmetic-intensive code and could be directly compiled to
    SEL or Conditional MOV instruction on some architectures.
    Out of curiosity whether or not the current compilers can already do this
    kind of optimization I tried this program:

    package main
    import "math/rand"

    func main(){
    c := (rand.Int() < -1)
    var a int
    if c { a=12345 } else { a=54321 }
    println(a)
    }

    I found this snippet in the output of 6g -S:

    --- prog list "main" ---
    0000 (tern.go:4) TEXT main+0(SB),$8-0
    0001 (tern.go:4) LOCALS ,$0
    0002 (tern.go:5) CALL ,rand.Int+0(SB)
    0003 (tern.go:5) MOVQ (SP),BX
    0004 (tern.go:5) JMP ,7
    0005 (tern.go:5) MOVQ $1,AX
    0006 (tern.go:5) JMP ,10
    0007 (tern.go:5) CMPQ BX,$-1
    0008 (tern.go:5) JLT ,5
    0009 (tern.go:5) MOVQ $0,AX
    *0010 (tern.go:7) CMPB AX,$0*
    *0011 (tern.go:7) JEQ ,14*
    *0012 (tern.go:7) MOVQ $12345,AX*
    *0013 (tern.go:7) JMP ,15*
    *0014 (tern.go:7) MOVQ $54321,AX*
    0015 (tern.go:8) MOVQ AX,(SP)
    0016 (tern.go:8) CALL ,runtime.printint+0(SB)
    0017 (tern.go:8) CALL ,runtime.printnl+0(SB)
    0018 (tern.go:9) RET ,
    ...

    So the current 6g produced straightforward code with branching.

    Contrary, gccgo -S -O3 produced:

    ...
    main.main:
    .LFB0:
    .cfi_startproc
    cmpq %fs:112, %rsp
    jb .L7
    .L4:
    subq $8, %rsp
    .cfi_def_cfa_offset 16
    call math_rand.Int
    *movl $12345, %edi*
    * cmpl $-2, %eax*
    * movl $54321, %eax*
    * cmovg %rax, %rdi*
    call __go_print_int64
    addq $8, %rsp
    .cfi_def_cfa_offset 8
    jmp __go_print_nl
    ...

    So gccgo nicely optimized it (apparently only with optimization flags). By
    the way, when I replaced c := (rand.Int() < -1) by c := true, gccgo -O3
    optimized away everything.

    -Arne.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Liigo Zhuang at Aug 20, 2013 at 3:42 am
    2013/8/19 <mykola.dvornik@gmail.com>
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?

    It cleans-up arithmetic-intensive code and could be directly compiled to
    SEL or Conditional MOV instruction on some architectures.
    +1
    great.


    --
    by *Liigo*, http://blog.csdn.net/liigo/
    Google+ https://plus.google.com/105597640837742873343/

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 20, 2013 at 8:02 am
    Just out of curiousity, do you really think that the Go developers
    implement this feature because of your +1 ?
    It has been discussed over and over again.
    On Tuesday, August 20, 2013 5:42:44 AM UTC+2, Liigo Zhuang wrote:

    2013/8/19 <mykola....@gmail.com <javascript:>>
    Would it be possible to implement C-style arithmetic IF, e.g.

    c = (a < b) ? a : b;

    ?

    It cleans-up arithmetic-intensive code and could be directly compiled to
    SEL or Conditional MOV instruction on some architectures.
    +1
    great.


    --
    by *Liigo*, http://blog.csdn.net/liigo/
    Google+ https://plus.google.com/105597640837742873343/
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 19, '13 at 2:41p
activeAug 20, '13 at 8:02a
posts11
users9
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase