FAQ
I came across this blog
entry: http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
remembered Go supports multiple assignment,
ie: http://play.golang.org/p/0ARjyFZjGI

I wonder how does it work underneath, does it creates some sort of temp
variable?

Does anyone knows?

p.s. Yey, my first post in this group :)

--
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

  • Patrick Mylund Nielsen at Feb 26, 2013 at 4:45 pm
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and remembered
    Go supports multiple assignment, ie: http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of temp
    variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.
  • Kyle Lemons at Feb 26, 2013 at 5:38 pm
    Technically I think it evaluates and stores in registers before assigning.
    It's not temporary "variables" as such.

    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen wrote:

    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and remembered
    Go supports multiple assignment, ie: http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of temp
    variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.
  • Patrick Mylund Nielsen at Feb 26, 2013 at 5:47 pm
    Temporary value, then.

    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons wrote:

    Technically I think it evaluates and stores in registers before assigning.
    It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of temp
    variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.
  • Michael Jones at Feb 26, 2013 at 10:30 pm
    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.


    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen wrote:

    Temporary value, then.

    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons wrote:

    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of temp
    variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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.
  • Patrick Mylund Nielsen at Feb 26, 2013 at 10:36 pm
    I don't think this is the case, but I honestly don't know the compiler well
    enough to say. I'm getting my info from here:
    https://code.google.com/p/go/issues/detail?id=3126#c5

    On Tue, Feb 26, 2013 at 11:29 PM, Michael Jones wrote:

    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.



    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Temporary value, then.

    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons wrote:

    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of
    temp variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765
    --
    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.
  • Michael Jones at Feb 26, 2013 at 10:48 pm
    No, sorry --- the compiler's don't do that. Just saying that it's always a
    possibility.
    On Tue, Feb 26, 2013 at 5:35 PM, Patrick Mylund Nielsen wrote:

    I don't think this is the case, but I honestly don't know the compiler
    well enough to say. I'm getting my info from here:
    https://code.google.com/p/go/issues/detail?id=3126#c5

    On Tue, Feb 26, 2013 at 11:29 PM, Michael Jones wrote:

    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.



    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Temporary value, then.

    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons wrote:

    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of
    temp variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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.
  • Patrick Mylund Nielsen at Feb 26, 2013 at 10:56 pm
    Oh, right, certainly. Missed "might choose", sorry.

    On Tue, Feb 26, 2013 at 11:48 PM, Michael Jones wrote:

    No, sorry --- the compiler's don't do that. Just saying that it's always a
    possibility.


    On Tue, Feb 26, 2013 at 5:35 PM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    I don't think this is the case, but I honestly don't know the compiler
    well enough to say. I'm getting my info from here:
    https://code.google.com/p/go/issues/detail?id=3126#c5

    On Tue, Feb 26, 2013 at 11:29 PM, Michael Jones wrote:

    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.



    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Temporary value, then.

    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons wrote:

    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    patrick@patrickmylund.com> wrote:
    Yes, a, b = b, a uses a temp variable

    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz wrote:

    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of
    temp variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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.

    --
    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.

    --
    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.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765
    --
    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.
  • Pete Wilson at Feb 27, 2013 at 4:01 pm
    While not relevant to current go compilers, most/many VLIW machines
    naturally support multiple assignment in the hardware. The actual
    instruction for x,y = y, x is generally one which simply executes x = y and
    y = x concurrently, but with the semantic rule that all the right hands are
    evaluated first and in parallel, and then all the right hands are executed
    in parallel. In the processor pipeline, all the source registers are read
    in parallel, an operation (a null op in this case) is performed in the next
    stage, and then in the next stage the values are all written in parallel.

    On Tue, Feb 26, 2013 at 11:29 PM, Michael Jones <m...@google.com<javascript:>
    wrote:
    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.



    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen <
    pat...@patrickmylund.com <javascript:>> wrote:
    Temporary value, then.


    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons <kev...@google.com<javascript:>
    wrote:
    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    pat...@patrickmylund.com <javascript:>> wrote:
    Yes, a, b = b, a uses a temp variable


    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz <osca...@gmail.com<javascript:>
    wrote:
    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of
    temp variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | m...@google.com<javascript:>
    +1 650-335-5765
    --
    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.
  • Johann Höchtl at Feb 28, 2013 at 1:51 pm

    Am Dienstag, 26. Februar 2013 23:29:25 UTC+1 schrieb Michael Jones:
    The exchange operation

    x, y = y, x

    certainly suggests

    t := x
    x = y
    y = t

    This is really an edge case. If the compiler is required to spill out to
    stack or even heap because of free register exhaustion, using the xor-trick
    can be the last resort to avoid allocation and book-keeping. Besides that,
    I would assume a plain assignment to a register instead of three XORs to be
    more effective in terms of OPS.
    ...but a compiler, with no spare registers, might choose to code it as

    x ^= y
    y ^= x
    x ^= y

    where the values x and y are treated as if they are 8, 16, 32, or 64 bit
    ints no matter what their actual types might be.



    On Tue, Feb 26, 2013 at 12:47 PM, Patrick Mylund Nielsen <
    pat...@patrickmylund.com <javascript:>> wrote:
    Temporary value, then.


    On Tue, Feb 26, 2013 at 6:38 PM, Kyle Lemons <kev...@google.com<javascript:>
    wrote:
    Technically I think it evaluates and stores in registers before
    assigning. It's not temporary "variables" as such.


    On Tue, Feb 26, 2013 at 8:45 AM, Patrick Mylund Nielsen <
    pat...@patrickmylund.com <javascript:>> wrote:
    Yes, a, b = b, a uses a temp variable


    On Tue, Feb 26, 2013 at 5:40 PM, OscarRyz <osca...@gmail.com<javascript:>
    wrote:
    I came across this blog entry:
    http://chris-taylor.github.com/blog/2013/02/25/xor-trick/ and
    remembered Go supports multiple assignment, ie:
    http://play.golang.org/p/0ARjyFZjGI

    I wonder how does it work underneath, does it creates some sort of
    temp variable?

    Does anyone knows?

    p.s. Yey, my first post in this group :)

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | m...@google.com<javascript:>
    +1 650-335-5765
    --
    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
postedFeb 26, '13 at 4:45p
activeFeb 28, '13 at 1:51p
posts10
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase