www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Adding a float with all four elements of a float4

reply Straivers <straivers98 gmail.com> writes:
Hi,

I want to make a utility wrapper around a core.simd.float4, and 
have been trying to make the following code work, but have been 
met with no success.

auto add(float rhs)
{
     return __simd(XMM.ADDPS, lhs, rhs);
}

Then I tried

auto add(float4 lhs, float rhs)
{
     float4 tmp = [rhs, rhs, rhs, rhs];
     return __simd(XMM.ADDPS, lhs, rhs);
}

When that didn't work, I turned to IASM and threw together this:

float4 add(float4 lhs, float rhs)
{
     float4 res;
     float4 rhs_tmp = [rhs, rhs, rhs, rhs];
     auto lhs_addr = &lhs;
     auto rhs_addr = &rhs_tmp;
     asm
     {
         mov RAX, lhs_addr;
         mov RBX, rhs_addr;
         movups XMM0, [RAX];
         movups XMM1, [RBX];

         addps XMM0, XMM1;
         movups res, XMM0;
     }
     return res;
}

and it still didn't work. So, what am I doing wrong?
Apr 20 2016
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 21 April 2016 at 00:14:53 UTC, Straivers wrote:
 Hi,

 I want to make a utility wrapper around a core.simd.float4, and 
 have been trying to make the following code work, but have been 
 met with no success.

 [...]
you want to broadcast the rhs to a float4 and then adds them. Can you post the errors (if any) you get during compilation.
Apr 20 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 21 April 2016 at 01:48:15 UTC, Nicholas Wilson wrote:
 On Thursday, 21 April 2016 at 00:14:53 UTC, Straivers wrote:
 Hi,

 I want to make a utility wrapper around a core.simd.float4, 
 and have been trying to make the following code work, but have 
 been met with no success.

 [...]
you want to broadcast the rhs to a float4 and then adds them. Can you post the errors (if any) you get during compilation.
Urgh, autocorrect. That should be addps them.
Apr 20 2016
parent Straivers <straivers98 gmail.com> writes:
On Thursday, 21 April 2016 at 01:49:19 UTC, Nicholas Wilson wrote:
 [...]
you want to broadcast the rhs to a float4 and then adds them. Can you post the errors (if any) you get during compilation.
Urgh, autocorrect. That should be addps them.
I get a "Error: floating point constant expression expected instead of rhs"
Apr 20 2016
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Thu, 21 Apr 2016 00:14:53 +0000
schrieb Straivers <straivers98 gmail.com>:

 Hi,
 
 I want to make a utility wrapper around a core.simd.float4, and 
 have been trying to make the following code work, but have been 
 met with no success.
 
 auto add(float rhs)
 {
      return __simd(XMM.ADDPS, lhs, rhs);
 }
It seems like your are duplicating std.simd: https://github.com/TurkeyMan/simd -- Marco
Apr 21 2016