Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    what function is lshr32 ?
    Troels Henriksen
    @athas
    Logical shift right for 32-bit integers.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    oh, okay
    Troels Henriksen
    @athas
    Oh, are you invoking undefined behaviour maybe?
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    not impossible, but I don't think I do anything to sketchy
    Troels Henriksen
    @athas
    According to the language reference, "Shifting is undefined if the right operand is negative, or greater than or equal to the length in bits of the left operand", but I don't think that happens here.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    There should not be anything new with this code at least
    The right operand is negative, but I don't think it should be
    the right operand has all kinds of odd values
    Troels Henriksen
    @athas
    Is rotl right?
    You are always shifting 32 bits right, which I think in practice will be identity on x86.
    (That's why Futhark says "undefined behaviour", because it would be expensive to make x86 do something else.)
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    ah, rotl?
    Troels Henriksen
    @athas
    Yes.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    I read it as rot1
    and got confused
    Troels Henriksen
    @athas
    It would be nice if the interpreter yelled loudly about undefined behaviour. It's slow enough that it's not a problem to check for it.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    I am still confused as to why it fails now I've used this code for a very long time now
    The only used of rotluse constants below 32 though...
    -- xoshiro128**
    type PRNG = {state: (u32,u32,u32,u32)}
    let rotl (x:u32) (k:u32) = x << k | x >> 32-k
    
    let next (g:PRNG) : (u32, PRNG) =
        let (a,b,c,d) = g.state
        let res = (rotl (b * 5) 7) * 9
        let t = b << 9
        let c = c ^ a
        let d = d ^ b
        let b = b ^ c
        let a = a ^ d
        let c = c ^ t
        let d = rotl d 11
        in (res, {state = (a,b,c,d)})
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    it's supposed to be an implementaion of https://prng.di.unimi.it/xoshiro128starstar.c
    Troels Henriksen
    @athas
    x << k | x >> 32-k means (x << k | x >> 32)-k
    Uh, wait, no.
    I wonder if this is some other undefined behaviour thing in C that the code generator doesn't guard properly against.
    Sometimes C compilers are like evil genies that try to interpret your wishes in the most destructive way.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
        while (loop_while_150581) {
            int32_t defunc_0_f_res_150583 = add32(1, x_150582);
            int32_t x_150584 = lshr32(defunc_2_reduce_res_150569,
                                      defunc_0_f_res_150583);
            bool defunc_0_p_res_150585 = x_150584 == 0;
            bool loop_cond_150586 = !defunc_0_p_res_150585;
            bool loop_while_tmp_162843 = loop_cond_150586;
            int32_t x_tmp_162844 = defunc_0_f_res_150583;
    
            loop_while_150581 = loop_while_tmp_162843;
            x_150582 = x_tmp_162844;
        }
    This is the loop that fails
    Troels Henriksen
    @athas
    What are the values of defunc_0_f_res_150583?
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    it appears to just be incremented
    starting at 0
    Troels Henriksen
    @athas
    What is defunc_2_reduce_res_150569?
    This loop will never terminate if its most significant bit is set.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    it looks like a random number of varying magnitude
    Troels Henriksen
    @athas
    So the odds that the highest bit will be set is pretty good.
    At that point you will be saying x>>32, which is undefined behaviour (and just returns x on x86).
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    oh wait, I printed the wrong number
    Troels Henriksen
    @athas
    So while I am pretty sure this is an instance of undefined behaviour, you should consider using this function to replace the loop anyway: https://futhark-lang.org/docs/prelude/doc/prelude/math.html#1217
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    it's -1076945987
    Troels Henriksen
    @athas
    Which means the most significant bit is set.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    Now I wonder which piece of code this is part of, but th I can look for myself
    Thanks
    Troels Henriksen
    @athas
    It's in associations.fut; your radix sort.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    ah
    That explains why it's a new problem. I used a radix sort for some data generation
    Troels Henriksen
    @athas
    This is definitely one of the sharper edges in the language.
    Gusten Isfeldt
    @gusten:matrix.org
    [m]
    Now that I look at my code the edge case is obvious though
    assuming you think of undefined behavoiur that is
    and now it works
    adding a warning in the interpreter would indeed be nice though
    Troels Henriksen
    @athas
    It should be an error in the interpreter.
    Just like division by zero.