www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23746] New: ICE with bit-wise binops with vector masks

https://issues.dlang.org/show_bug.cgi?id=23746

          Issue ID: 23746
           Summary: ICE with bit-wise binops with vector masks
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kinke gmx.net

This crashes the backend of v2.102.1:
```
T clamp(T)(T x)
{
    enum min = T(-1);
    enum max = T(1);
    const ltMask = x < min;
    const gtMask = x > max;
    version (none) // nice-to-have version
    {
        return (min & ltMask)
            | (max & gtMask)
            | (x & ~(ltMask | gtMask));
    }
    else // currently required ugly version
    {
        alias I = typeof(ltMask);
        return cast(T) (
              ((cast(I) min) & ltMask)
            | ((cast(I) max) & gtMask)
            | ((cast(I) x) & ~(ltMask | gtMask)));
    }
}

void main()
{
    alias int4 = __vector(int[4]);
    alias float4 = __vector(float[4]);
    assert(clamp!int4([0, 1, -2, 3]).array == [0, 1, -1, 1]);
    assert(clamp!float4([-0.25f, 0.5f, 1.5f, -2]).array == [-0.25f, 0.5f, 1,
-1]);
}
```

Output on Linux x64:
```
el:0x556764d27e70 cnt=0 cs=0 > mTYconst|TYulong[4] 0x556764d275a0
0x556764d27480
 el:0x556764d275a0 cnt=1 cs=255 var TYfloat4  x
 el:0x556764d27480 cnt=0 cs=255 var mTYconst|TYfloat4  _TMP14
Illegal instruction
```

---

As shown in the example, working with vector masks is very cumbersome as of
v2.102, requiring reinterpret-casts all over the place.

--
Feb 26 2023