Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70572 > unrolled thread

Probably UB but I am finding it hard to give a fuck (C++ swizzle)

Started byMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
First post2019-12-18 21:34 +0000
Last post2020-01-31 21:42 -0800
Articles 20 — 8 participants

Back to article view | Back to comp.lang.c++


Contents

  Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-18 21:34 +0000
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Öö Tiib <ootiib@hot.ee> - 2019-12-18 14:24 -0800
      Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-18 23:14 +0000
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Öö Tiib <ootiib@hot.ee> - 2019-12-18 15:36 -0800
          Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-19 06:18 +0000
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-19 10:49 +0100
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Manfred <noname@add.invalid> - 2019-12-19 14:29 +0100
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-12-18 15:38 -0800
      Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-19 06:19 +0000
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-12-23 10:34 -0800
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-12-28 00:30 -0800
          Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibble@i42.removethisbit.co.uk> - 2019-12-28 15:53 +0000
            Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-31 23:00 -0800
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Manfred <noname@add.invalid> - 2019-12-19 14:25 +0100
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) woodbrian77@gmail.com - 2019-12-20 08:22 -0800
      Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-21 13:23 +0000
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-11 15:08 -0800
      Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-10 18:35 +0100
        Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-02-10 14:07 -0800
    Re: Probably UB but I am finding it hard to give a fuck (C++ swizzle) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-01-31 21:42 -0800

#70572 — Probably UB but I am finding it hard to give a fuck (C++ swizzle)

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-18 21:34 +0000
SubjectProbably UB but I am finding it hard to give a fuck (C++ swizzle)
Message-ID<cYwKF.3051$vj3.487@fx10.dcg>
Hi!

The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:

vec3 v1 = { 1.0, 2.0, 3.0 };
vec2 v2 = v1.xy;
double z = v1.z;
vec4 v3 = v1.xyzz;

So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.

Collective thoughts/wisdom?

// swizzle_array.hpp
/*
   neogfx C++ GUI Library
   Copyright (c) 2019 Leigh Johnston.  All Rights Reserved.

   This program is free software: you can redistribute it and / or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <neogfx/neogfx.hpp>
#include <neogfx/core/swizzle.hpp>

namespace neogfx
{
     namespace math
     {
         template <typename V, typename T, uint32_t Size>
         struct swizzle_array
         {
             typedef T value_type;
             typedef std::array<value_type, Size> array_type;
             typedef V vector_type;

             union
             {
                 array_type v;
                 struct // todo: alignment, padding?
                 {
                     value_type x;
                     value_type y;
                     value_type z;
                 };
                 swizzle<vector_type, array_type, 2, 0, 0> xx;
                 swizzle<vector_type, array_type, 2, 0, 1> xy;
                 swizzle<vector_type, array_type, 2, 0, 2> xz;
                 swizzle<vector_type, array_type, 2, 1, 0> yx;
                 swizzle<vector_type, array_type, 2, 1, 1> yy;
                 swizzle<vector_type, array_type, 2, 1, 2> yz;
                 swizzle<vector_type, array_type, 2, 2, 0> zx;
                 swizzle<vector_type, array_type, 2, 2, 1> zy;
                 swizzle<vector_type, array_type, 2, 2, 2> zz;
                 swizzle<vector_type, array_type, 3, 0, 0, 0> xxx;
                 swizzle<vector_type, array_type, 3, 0, 0, 1> xxy;
                 swizzle<vector_type, array_type, 3, 0, 0, 2> xxz;
                 swizzle<vector_type, array_type, 3, 0, 1, 0> xyx;
                 swizzle<vector_type, array_type, 3, 0, 1, 1> xyy;
                 swizzle<vector_type, array_type, 3, 0, 1, 2> xyz;
                 swizzle<vector_type, array_type, 3, 1, 0, 0> yxx;
                 swizzle<vector_type, array_type, 3, 1, 0, 1> yxy;
                 swizzle<vector_type, array_type, 3, 1, 0, 2> yxz;
                 swizzle<vector_type, array_type, 3, 1, 1, 0> yyx;
                 swizzle<vector_type, array_type, 3, 1, 1, 1> yyy;
                 swizzle<vector_type, array_type, 3, 1, 1, 2> yyz;
                 swizzle<vector_type, array_type, 3, 1, 2, 0> yzx;
                 swizzle<vector_type, array_type, 3, 1, 2, 1> yzy;
                 swizzle<vector_type, array_type, 3, 1, 2, 2> yzz;
                 swizzle<vector_type, array_type, 3, 2, 0, 0> zxx;
                 swizzle<vector_type, array_type, 3, 2, 0, 1> zxy;
                 swizzle<vector_type, array_type, 3, 2, 0, 2> zxz;
                 swizzle<vector_type, array_type, 3, 2, 1, 0> zyx;
                 swizzle<vector_type, array_type, 3, 2, 1, 1> zyy;
                 swizzle<vector_type, array_type, 3, 2, 1, 2> zyz;
                 swizzle<vector_type, array_type, 3, 2, 2, 0> zzx;
                 swizzle<vector_type, array_type, 3, 2, 2, 1> zzy;
                 swizzle<vector_type, array_type, 3, 2, 2, 2> zzz;
             };
         };

         template <typename V, typename T>
         struct swizzle_array<V, T, 1>
         {
             typedef T value_type;
             typedef std::array<value_type, 1> array_type;
             typedef V vector_type;

             union
             {
                 array_type v;
                 struct // todo: alignment, padding?
                 {
                     value_type x;
                 };
                 swizzle<vector_type, array_type, 2, 0, 0> xx;
                 swizzle<vector_type, array_type, 3, 0, 0, 0> xxx;
             };
         };

         template <typename V, typename T>
         struct swizzle_array<V, T, 2>
         {
             typedef T value_type;
             typedef std::array<value_type, 2> array_type;
             typedef V vector_type;

             union
             {
                 array_type v;
                 struct // todo: alignment, padding?
                 {
                     value_type x;
                     value_type y;
                 };
                 swizzle<vector_type, array_type, 2, 0, 0> xx;
                 swizzle<vector_type, array_type, 2, 0, 1> xy;
                 swizzle<vector_type, array_type, 2, 1, 0> yx;
                 swizzle<vector_type, array_type, 2, 1, 1> yy;
                 swizzle<vector_type, array_type, 3, 0, 0, 0> xxx;
                 swizzle<vector_type, array_type, 3, 0, 0, 1> xxy;
                 swizzle<vector_type, array_type, 3, 0, 1, 0> xyx;
                 swizzle<vector_type, array_type, 3, 0, 1, 1> xyy;
                 swizzle<vector_type, array_type, 3, 1, 0, 0> yxx;
                 swizzle<vector_type, array_type, 3, 1, 0, 1> yxy;
                 swizzle<vector_type, array_type, 3, 1, 1, 0> yyx;
                 swizzle<vector_type, array_type, 3, 1, 1, 1> yyy;
             };
         };

         template <typename V, typename T>
         struct swizzle_array<V, T, 3>
         {
             typedef T value_type;
             typedef std::array<value_type, 3> array_type;
             typedef V vector_type;

             union
             {
                 array_type v;
                 struct // todo: alignment, padding?
                 {
                     value_type x;
                     value_type y;
                     value_type z;
                 };
                 swizzle<vector_type, array_type, 2, 0, 0> xx;
                 swizzle<vector_type, array_type, 2, 0, 1> xy;
                 swizzle<vector_type, array_type, 2, 0, 2> xz;
                 swizzle<vector_type, array_type, 2, 1, 0> yx;
                 swizzle<vector_type, array_type, 2, 1, 1> yy;
                 swizzle<vector_type, array_type, 2, 1, 2> yz;
                 swizzle<vector_type, array_type, 2, 2, 0> zx;
                 swizzle<vector_type, array_type, 2, 2, 1> zy;
                 swizzle<vector_type, array_type, 2, 2, 2> zz;
                 swizzle<vector_type, array_type, 3, 0, 0, 0> xxx;
                 swizzle<vector_type, array_type, 3, 0, 0, 1> xxy;
                 swizzle<vector_type, array_type, 3, 0, 0, 2> xxz;
                 swizzle<vector_type, array_type, 3, 0, 1, 0> xyx;
                 swizzle<vector_type, array_type, 3, 0, 1, 1> xyy;
                 swizzle<vector_type, array_type, 3, 0, 1, 2> xyz;
                 swizzle<vector_type, array_type, 3, 1, 0, 0> yxx;
                 swizzle<vector_type, array_type, 3, 1, 0, 1> yxy;
                 swizzle<vector_type, array_type, 3, 1, 0, 2> yxz;
                 swizzle<vector_type, array_type, 3, 1, 1, 0> yyx;
                 swizzle<vector_type, array_type, 3, 1, 1, 1> yyy;
                 swizzle<vector_type, array_type, 3, 1, 1, 2> yyz;
                 swizzle<vector_type, array_type, 3, 1, 2, 0> yzx;
                 swizzle<vector_type, array_type, 3, 1, 2, 1> yzy;
                 swizzle<vector_type, array_type, 3, 1, 2, 2> yzz;
                 swizzle<vector_type, array_type, 3, 2, 0, 0> zxx;
                 swizzle<vector_type, array_type, 3, 2, 0, 1> zxy;
                 swizzle<vector_type, array_type, 3, 2, 0, 2> zxz;
                 swizzle<vector_type, array_type, 3, 2, 1, 0> zyx;
                 swizzle<vector_type, array_type, 3, 2, 1, 1> zyy;
                 swizzle<vector_type, array_type, 3, 2, 1, 2> zyz;
                 swizzle<vector_type, array_type, 3, 2, 2, 0> zzx;
                 swizzle<vector_type, array_type, 3, 2, 2, 1> zzy;
                 swizzle<vector_type, array_type, 3, 2, 2, 2> zzz;
             };
         };
     }
}

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [next] | [standalone]


#70573

FromÖö Tiib <ootiib@hot.ee>
Date2019-12-18 14:24 -0800
Message-ID<c7c7929d-b01c-4a19-a557-60da44ed6c9f@googlegroups.com>
In reply to#70572
On Wednesday, 18 December 2019 23:35:23 UTC+2, Mr Flibble  wrote:
> Hi!
> 
> The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:
> 
> vec3 v1 = { 1.0, 2.0, 3.0 };
> vec2 v2 = v1.xy;
> double z = v1.z;
> vec4 v3 = v1.xyzz;
> 
> So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.
> 
> Collective thoughts/wisdom?

It likely violates strict aliasing, casting between unrelated types
and also how unions are allowed to be used in C++. 

So even if it appears to work with your compilers on cases
you did test it might mess it up differently in other use-cases 
that you did not test yet. Also it may change with next patch to
any of your compilers.

Can you say what is the issue with typical interface
implementable to work same on all C++11 or better compilers:

 vec3 v1 = { 1.0, 2.0, 3.0 };
 vec2 v2 = v1.xy();
 double z = v1.z();
 vec4 v3 = v1.xyzz();

Couple of extra parentheses can't be problem, otherwise we all
would switch to Kotlin (that is *way* less verbose than C++).

[toc] | [prev] | [next] | [standalone]


#70574

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-18 23:14 +0000
Message-ID<FpyKF.281$Rl7.139@fx06.dcg>
In reply to#70573
On 18/12/2019 22:24, Öö Tiib wrote:
> On Wednesday, 18 December 2019 23:35:23 UTC+2, Mr Flibble  wrote:
>> Hi!
>>
>> The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:
>>
>> vec3 v1 = { 1.0, 2.0, 3.0 };
>> vec2 v2 = v1.xy;
>> double z = v1.z;
>> vec4 v3 = v1.xyzz;
>>
>> So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.
>>
>> Collective thoughts/wisdom?
> 
> It likely violates strict aliasing, casting between unrelated types
> and also how unions are allowed to be used in C++.
> 
> So even if it appears to work with your compilers on cases
> you did test it might mess it up differently in other use-cases
> that you did not test yet. Also it may change with next patch to
> any of your compilers.
> 
> Can you say what is the issue with typical interface
> implementable to work same on all C++11 or better compilers:
> 
>   vec3 v1 = { 1.0, 2.0, 3.0 };
>   vec2 v2 = v1.xy();
>   double z = v1.z();
>   vec4 v3 = v1.xyzz();
> 
> Couple of extra parentheses can't be problem, otherwise we all
> would switch to Kotlin (that is *way* less verbose than C++).

Because

v1.set_z(42.0);

is ew and

v1.z() = 42.0

is even more ew.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70575

FromÖö Tiib <ootiib@hot.ee>
Date2019-12-18 15:36 -0800
Message-ID<bb1f4444-bfe5-4ac8-8cc2-68d92a0dfd33@googlegroups.com>
In reply to#70574
On Thursday, 19 December 2019 01:15:06 UTC+2, Mr Flibble  wrote:
> On 18/12/2019 22:24, Öö Tiib wrote:
> > On Wednesday, 18 December 2019 23:35:23 UTC+2, Mr Flibble  wrote:
> >> Hi!
> >>
> >> The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:
> >>
> >> vec3 v1 = { 1.0, 2.0, 3.0 };
> >> vec2 v2 = v1.xy;
> >> double z = v1.z;
> >> vec4 v3 = v1.xyzz;
> >>
> >> So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.
> >>
> >> Collective thoughts/wisdom?
> > 
> > It likely violates strict aliasing, casting between unrelated types
> > and also how unions are allowed to be used in C++.
> > 
> > So even if it appears to work with your compilers on cases
> > you did test it might mess it up differently in other use-cases
> > that you did not test yet. Also it may change with next patch to
> > any of your compilers.
> > 
> > Can you say what is the issue with typical interface
> > implementable to work same on all C++11 or better compilers:
> > 
> >   vec3 v1 = { 1.0, 2.0, 3.0 };
> >   vec2 v2 = v1.xy();
> >   double z = v1.z();
> >   vec4 v3 = v1.xyzz();
> > 
> > Couple of extra parentheses can't be problem, otherwise we all
> > would switch to Kotlin (that is *way* less verbose than C++).
> 
> Because
> 
> v1.set_z(42.0);
> 
> is ew and
> 
> v1.z() = 42.0
> 
> is even more ew.

I trust  v1.moveBy(0.0, 0.0, 39.0) or 
v1.moveTo(1.0, 2.0, 42.0) depending what was the point
is simpler to understand than v1.z = 42.0 anyway.

[toc] | [prev] | [next] | [standalone]


#70578

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-19 06:18 +0000
Message-ID<SCEKF.6569$RQ.5965@fx09.dcg>
In reply to#70575
On 18/12/2019 23:36, Öö Tiib wrote:
> On Thursday, 19 December 2019 01:15:06 UTC+2, Mr Flibble  wrote:
>> On 18/12/2019 22:24, Öö Tiib wrote:
>>> On Wednesday, 18 December 2019 23:35:23 UTC+2, Mr Flibble  wrote:
>>>> Hi!
>>>>
>>>> The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:
>>>>
>>>> vec3 v1 = { 1.0, 2.0, 3.0 };
>>>> vec2 v2 = v1.xy;
>>>> double z = v1.z;
>>>> vec4 v3 = v1.xyzz;
>>>>
>>>> So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.
>>>>
>>>> Collective thoughts/wisdom?
>>>
>>> It likely violates strict aliasing, casting between unrelated types
>>> and also how unions are allowed to be used in C++.
>>>
>>> So even if it appears to work with your compilers on cases
>>> you did test it might mess it up differently in other use-cases
>>> that you did not test yet. Also it may change with next patch to
>>> any of your compilers.
>>>
>>> Can you say what is the issue with typical interface
>>> implementable to work same on all C++11 or better compilers:
>>>
>>>    vec3 v1 = { 1.0, 2.0, 3.0 };
>>>    vec2 v2 = v1.xy();
>>>    double z = v1.z();
>>>    vec4 v3 = v1.xyzz();
>>>
>>> Couple of extra parentheses can't be problem, otherwise we all
>>> would switch to Kotlin (that is *way* less verbose than C++).
>>
>> Because
>>
>> v1.set_z(42.0);
>>
>> is ew and
>>
>> v1.z() = 42.0
>>
>> is even more ew.
> 
> I trust  v1.moveBy(0.0, 0.0, 39.0) or
> v1.moveTo(1.0, 2.0, 42.0) depending what was the point
> is simpler to understand than v1.z = 42.0 anyway.

No. v1 += vec3{ 0.0, 0.0, 39.0 } is.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70582

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-19 10:49 +0100
Message-ID<qtfh2l$95i$1@dont-email.me>
In reply to#70574
On 19.12.2019 00:14, Mr Flibble wrote:
> v1.set_z(42.0);
> 
> is ew and
> 
> v1.z() = 42.0
> 
> is even more ew.

#define Z z()

There.


- Alf

[toc] | [prev] | [next] | [standalone]


#70585

FromManfred <noname@add.invalid>
Date2019-12-19 14:29 +0100
Message-ID<qtfu04$tek$1@gioia.aioe.org>
In reply to#70574
On 12/19/2019 12:14 AM, Mr Flibble wrote:
>> Can you say what is the issue with typical interface
>> implementable to work same on all C++11 or better compilers:
>>
>>   vec3 v1 = { 1.0, 2.0, 3.0 };
>>   vec2 v2 = v1.xy();
>>   double z = v1.z();
>>   vec4 v3 = v1.xyzz();
>>
>> Couple of extra parentheses can't be problem, otherwise we all
>> would switch to Kotlin (that is *way* less verbose than C++).
> 
> Because
> 
> v1.set_z(42.0);
> 
> is ew and
> 
> v1.z() = 42.0
> 
> is even more ew.
> 
> /Flibble

A slightly better (IMO) interface is:
void z(double val);
double z() const;

That doesn't change much, though.

[toc] | [prev] | [next] | [standalone]


#70576

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2019-12-18 15:38 -0800
Message-ID<qteda4$1vi2$1@gioia.aioe.org>
In reply to#70572
On 12/18/2019 1:34 PM, Mr Flibble wrote:
> Hi!
> 
> The following is the base class of my vector class (the mathematical 
> vector not the container) so I can do things like:
> 
> vec3 v1 = { 1.0, 2.0, 3.0 };
> vec2 v2 = v1.xy;
> double z = v1.z;
> vec4 v3 = v1.xyzz;
> 
> So we have an anonymous union of an anonymous struct and some swizzles 
> and I suspect it is full of UB but I am finding it hard to care as I 
> cannot see any other way of doing this.
> 
> Collective thoughts/wisdom?
[...]

Are you trying to get close to GLSL? The glm lib for C++ is very close, 
but its does not have the .xyz type of functionality. Interesting.

vec4 v4 = vec4(v3.xyz, 1.0);

v4 now equals { v3.x, v3.y, v3.z, 1.0 }


[toc] | [prev] | [next] | [standalone]


#70579

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-19 06:19 +0000
Message-ID<9EEKF.6570$RQ.608@fx09.dcg>
In reply to#70576
On 18/12/2019 23:38, Chris M. Thomasson wrote:
> On 12/18/2019 1:34 PM, Mr Flibble wrote:
>> Hi!
>>
>> The following is the base class of my vector class (the mathematical vector not the container) so I can do things like:
>>
>> vec3 v1 = { 1.0, 2.0, 3.0 };
>> vec2 v2 = v1.xy;
>> double z = v1.z;
>> vec4 v3 = v1.xyzz;
>>
>> So we have an anonymous union of an anonymous struct and some swizzles and I suspect it is full of UB but I am finding it hard to care as I cannot see any other way of doing this.
>>
>> Collective thoughts/wisdom?
> [...]
> 
> Are you trying to get close to GLSL? The glm lib for C++ is very close, but its does not have the .xyz type of functionality. Interesting.
> 
> vec4 v4 = vec4(v3.xyz, 1.0);
> 
> v4 now equals { v3.x, v3.y, v3.z, 1.0 }

That is the goal, yes.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70626

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2019-12-23 10:34 -0800
Message-ID<qtr1as$17rq$1@gioia.aioe.org>
In reply to#70579
On 12/18/2019 10:19 PM, Mr Flibble wrote:
> On 18/12/2019 23:38, Chris M. Thomasson wrote:
>> On 12/18/2019 1:34 PM, Mr Flibble wrote:
[...]
>> Are you trying to get close to GLSL? The glm lib for C++ is very 
>> close, but its does not have the .xyz type of functionality. Interesting.
>>
>> vec4 v4 = vec4(v3.xyz, 1.0);
>>
>> v4 now equals { v3.x, v3.y, v3.z, 1.0 }
> 
> That is the goal, yes.

Perfect. Thanks for the work. I would like to use it from time to time.

Fwiw, sometimes get a little pissed when I see a swizzle, and have to 
port the damn shader over to my C++ code. Now, going from C++ to GLSL is 
"easier", in certain respects...

Sometimes when I see shi% like, just typing this in:
________________
vec2 foo = vec2(1., 2.);
vec3 bar = vec3(foo.xy, 3.);
vec4 foobar = vec4(foo.xy, bar.yz);

foobar = { 1., 2., 2., 3. }
________________

;^)

[toc] | [prev] | [next] | [standalone]


#70725

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2019-12-28 00:30 -0800
Message-ID<qu73rr$13d7$1@gioia.aioe.org>
In reply to#70579
On 12/18/2019 10:19 PM, Mr Flibble wrote:
> On 18/12/2019 23:38, Chris M. Thomasson wrote:
>> On 12/18/2019 1:34 PM, Mr Flibble wrote:
>>> Hi!
>>>
>>> The following is the base class of my vector class (the mathematical 
>>> vector not the container) so I can do things like:
>>>
>>> vec3 v1 = { 1.0, 2.0, 3.0 };
>>> vec2 v2 = v1.xy;
>>> double z = v1.z;
>>> vec4 v3 = v1.xyzz;
>>>
>>> So we have an anonymous union of an anonymous struct and some 
>>> swizzles and I suspect it is full of UB but I am finding it hard to 
>>> care as I cannot see any other way of doing this.
>>>
>>> Collective thoughts/wisdom?
>> [...]
>>
>> Are you trying to get close to GLSL? The glm lib for C++ is very 
>> close, but its does not have the .xyz type of functionality. Interesting.
>>
>> vec4 v4 = vec4(v3.xyz, 1.0);
>>
>> v4 now equals { v3.x, v3.y, v3.z, 1.0 }
> 
> That is the goal, yes.

Looking forward to using C++ with swizzles, using your work, to render 
something like the following experiment of mine:

https://youtu.be/TLd64a4gdZQ

[toc] | [prev] | [next] | [standalone]


#70736

FromMr Flibble <flibble@i42.removethisbit.co.uk>
Date2019-12-28 15:53 +0000
Message-ID<8UKNF.20704$vj3.9587@fx10.dcg>
In reply to#70725
On 28/12/2019 08:30, Chris M. Thomasson wrote:
> On 12/18/2019 10:19 PM, Mr Flibble wrote:
>> On 18/12/2019 23:38, Chris M. Thomasson wrote:
>>> On 12/18/2019 1:34 PM, Mr Flibble wrote:
>>>> Hi!
>>>>
>>>> The following is the base class of my vector class (the mathematical 
>>>> vector not the container) so I can do things like:
>>>>
>>>> vec3 v1 = { 1.0, 2.0, 3.0 };
>>>> vec2 v2 = v1.xy;
>>>> double z = v1.z;
>>>> vec4 v3 = v1.xyzz;
>>>>
>>>> So we have an anonymous union of an anonymous struct and some 
>>>> swizzles and I suspect it is full of UB but I am finding it hard to 
>>>> care as I cannot see any other way of doing this.
>>>>
>>>> Collective thoughts/wisdom?
>>> [...]
>>>
>>> Are you trying to get close to GLSL? The glm lib for C++ is very 
>>> close, but its does not have the .xyz type of functionality. 
>>> Interesting.
>>>
>>> vec4 v4 = vec4(v3.xyz, 1.0);
>>>
>>> v4 now equals { v3.x, v3.y, v3.z, 1.0 }
>>
>> That is the goal, yes.
> 
> Looking forward to using C++ with swizzles, using your work, to render 
> something like the following experiment of mine:
> 
> https://youtu.be/TLd64a4gdZQ

Probably best waiting until I get SIMD specializations working.

/Flibble

-- 
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais
“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” 
– Ricky Gervais
"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates 
a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#71670

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-31 23:00 -0800
Message-ID<r137m0$k63$1@gioia.aioe.org>
In reply to#70736
On 12/28/2019 7:53 AM, Mr Flibble wrote:
> On 28/12/2019 08:30, Chris M. Thomasson wrote:
>> On 12/18/2019 10:19 PM, Mr Flibble wrote:
>>> On 18/12/2019 23:38, Chris M. Thomasson wrote:
>>>> On 12/18/2019 1:34 PM, Mr Flibble wrote:
>>>>> Hi!
>>>>>
>>>>> The following is the base class of my vector class (the 
>>>>> mathematical vector not the container) so I can do things like:
>>>>>
>>>>> vec3 v1 = { 1.0, 2.0, 3.0 };
>>>>> vec2 v2 = v1.xy;
>>>>> double z = v1.z;
>>>>> vec4 v3 = v1.xyzz;
>>>>>
>>>>> So we have an anonymous union of an anonymous struct and some 
>>>>> swizzles and I suspect it is full of UB but I am finding it hard to 
>>>>> care as I cannot see any other way of doing this.
>>>>>
>>>>> Collective thoughts/wisdom?
>>>> [...]
>>>>
>>>> Are you trying to get close to GLSL? The glm lib for C++ is very 
>>>> close, but its does not have the .xyz type of functionality. 
>>>> Interesting.
>>>>
>>>> vec4 v4 = vec4(v3.xyz, 1.0);
>>>>
>>>> v4 now equals { v3.x, v3.y, v3.z, 1.0 }
>>>
>>> That is the goal, yes.
>>
>> Looking forward to using C++ with swizzles, using your work, to render 
>> something like the following experiment of mine:
>>
>> https://youtu.be/TLd64a4gdZQ
> 
> Probably best waiting until I get SIMD specializations working.

No problem. Keep up the great work.

[toc] | [prev] | [next] | [standalone]


#70584

FromManfred <noname@add.invalid>
Date2019-12-19 14:25 +0100
Message-ID<qtftob$s87$1@gioia.aioe.org>
In reply to#70572
On 12/18/2019 10:34 PM, Mr Flibble wrote:
> Hi!
> 
> The following is the base class of my vector class (the mathematical 
> vector not the container) so I can do things like:
> 
> vec3 v1 = { 1.0, 2.0, 3.0 };
> vec2 v2 = v1.xy;
> double z = v1.z;
> vec4 v3 = v1.xyzz;
> 
> So we have an anonymous union of an anonymous struct and some swizzles 
> and I suspect it is full of UB but I am finding it hard to care as I 
> cannot see any other way of doing this.
> 
> Collective thoughts/wisdom?

Sounds like a union is not the right tool, unless I am misinterpreting 
the purpose.
Possibly xy, z, xyz etc. should be object members that overload the 
assignment and type cast (aka user-defined conversion) operators.
Inside the body of the overloads you perform the actions you need on the 
vector (so you need a reference to the vector inside the members).

[toc] | [prev] | [next] | [standalone]


#70590

Fromwoodbrian77@gmail.com
Date2019-12-20 08:22 -0800
Message-ID<7064a235-54ca-4270-96c6-730f5a3a72ee@googlegroups.com>
In reply to#70572
Please don't swear here. 


Brian
Ebenezer Enterprises - Enjoying programming again.
https://github.com/Ebenezer-group/onward

[toc] | [prev] | [next] | [standalone]


#70597

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-21 13:23 +0000
Message-ID<M1pLF.29603$3sa.16213@fx02.dcg>
In reply to#70590
On 20/12/2019 16:22, woodbrian77@gmail.com wrote:
> 
> Please don't swear here.

Fuck. Off.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#71297

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-11 15:08 -0800
Message-ID<qvdkhc$1v2n$1@gioia.aioe.org>
In reply to#70572
On 12/18/2019 1:34 PM, Mr Flibble wrote:
> Hi!
> 
> The following is the base class of my vector class (the mathematical 
> vector not the container) so I can do things like:
> 
> vec3 v1 = { 1.0, 2.0, 3.0 };
> vec2 v2 = v1.xy;
> double z = v1.z;
> vec4 v3 = v1.xyzz;
[...]

Almost getting enough time to try it out. Its an interesting go at the 
problem. Fwiw, here is a new animation that can benefit from this in C++:

https://youtu.be/Q6lJqIl-Nvs

[toc] | [prev] | [next] | [standalone]


#71782

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-02-10 18:35 +0100
Message-ID<r1s49p$e87$1@news.albasani.net>
In reply to#71297
> Almost getting enough time to try it out. Its an interesting go at the 
> problem. Fwiw, here is a new animation that can benefit from this in C++:
> https://youtu.be/Q6lJqIl-Nvs

You have diffuse associations.
Not that you'll end up where Aminer is today.

[toc] | [prev] | [next] | [standalone]


#71784

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-02-10 14:07 -0800
Message-ID<r1sk7r$1ot$2@gioia.aioe.org>
In reply to#71782
On 2/10/2020 9:35 AM, Bonita Montero wrote:
>> Almost getting enough time to try it out. Its an interesting go at the 
>> problem. Fwiw, here is a new animation that can benefit from this in C++:
>> https://youtu.be/Q6lJqIl-Nvs
> 
> You have diffuse associations.
> Not that you'll end up where Aminer is today.

Creating a swizzle in C++ that can behave like GLSL is not a trivial 
task. However, it should help anybody who is porting shader code over to 
a pure C++ representation.

[toc] | [prev] | [next] | [standalone]


#71668

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2020-01-31 21:42 -0800
Message-ID<r1333g$1lb$2@gioia.aioe.org>
In reply to#70572
On 12/18/2019 1:34 PM, Mr Flibble wrote:
> Hi!
> 
> The following is the base class of my vector class (the mathematical 
> vector not the container) so I can do things like:
[...]

I think I saw a swizzle in here:

https://youtu.be/O-1zEo7DD8w

live shader code competition.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web