Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

eficient float sign routine

149 views
Skip to first unread message

fir

unread,
May 25, 2022, 6:09:29 AM5/25/22
to
is there some ?
people seem tend to use

inline float sign (float x) { return (x > 0) - (x < 0); }

but im not sure if it is efficient
afaik float standard says that sign bit is the highest bit ot 32bit
representation so maybe something based on this?

but what exactly?

Ben

unread,
May 25, 2022, 6:45:50 AM5/25/22
to
fir <profes...@gmail.com> writes:

> is there some ?
> people seem tend to use
>
> inline float sign (float x) { return (x > 0) - (x < 0); }

Standard C has copysign. A call to your sign(f) function is equivalent
to copysign(1, f).

There is also a standard signbit macro that returns 0/1 depending on the
sign of the argument. These two treat NaNs differently.

--
Ben.

fir

unread,
May 25, 2022, 7:47:13 AM5/25/22
to
ok i will see, though it would be better tho have normal functions to not add/multiply
not quite necessary references

fir

unread,
May 25, 2022, 8:07:22 AM5/25/22
to
return -2*signbit(x)+1 ;
seem to wrok, though i not tested it except one case

fir

unread,
May 25, 2022, 8:25:30 AM5/25/22
to
środa, 25 maja 2022 o 14:07:22 UTC+2 fir napisał(a):
inline float sign2(float x)
{
return -2*signbit(x)+1 ;
}

inline float sign1(float x)
{
return (x>0)-(x<0);
}

for(float f= -333333.33; f< 333333.3; f+=0.33)
sum += sign2(f);

i tested the efficiency and there is no visible difference here though (about 10 ms each, but i run on quite old machine)

fir

unread,
May 25, 2022, 8:31:25 AM5/25/22
to
środa, 25 maja 2022 o 14:25:30 UTC+2 fir napisał(a):
not to say its -2*signbit(x)+1 is not all i woulkd beed as it returns +1 for zero...but if i see no difference i may left he < > version though it looks weird imo

Ben

unread,
May 25, 2022, 9:07:22 AM5/25/22
to
gcc, with -O2, generates shorter code for the one you didn't try:

copysignf(1, x)

That does not mean it's faster, but gcc probably has a reason to do what
it does.

Also, in many cases, it's copysign[fl]? that you want, but with
something other than 1 as the first argument.

--
Ben.

Tim Rentsch

unread,
May 25, 2022, 9:16:23 AM5/25/22
to
Ben <ben.u...@bsb.me.uk> writes:

> fir <profes...@gmail.com> writes:
>
>> is there some ?
>> people seem tend to use
>>
>> inline float sign (float x) { return (x > 0) - (x < 0); }
>
> Standard C has copysign. A call to your sign(f) function is equivalent
> to copysign(1, f).

The output doesn't match when the input is 0.

fir

unread,
May 25, 2022, 9:19:20 AM5/25/22
to
thet copysigfn also roughly the same, maybe 5-10% faster but im not sure and have a terible headache today and cant test it too much
initially i tgought this < > version would be much slower (as ifs are slow) but if there is not such big difference the questiob is less important



fir

unread,
May 25, 2022, 10:25:48 AM5/25/22
to
thats right and this extended could be needed
hovever in my case it seem not so much

i need this sign when 'resolving' (adding) some force in a link (join?) agains 2 balls (dots) in 3d
space, say joint assumed distance is 100 when real distance is 110 or 95 then i need to applay force
like -10*10 or +5*5 (square probably best) thus i need a sign

void ResolveLinkForce_(int n, int i, float link_dist)
{
float Mi = dot3d[i].mass;
float dx = dot3d[i].x - dot3d[n].x;
float dy = dot3d[i].y - dot3d[n].y;
float dz = dot3d[i].z - dot3d[n].z;

float d = sqrt(dx*dx + dy*dy + dz*dz);
float dd = (d - link_dist);
float p = -sign(dd)*dd*dd* joint_hardness;

float px = (dx/d)*p;
float py = (dy/d)*p;
float pz = (dz/d)*p;
dot3d[i].vx += px/Mi;
dot3d[i].vy += py/Mi;
dot3d[i].vz += pz/Mi;

}
in case of dd being 0 its all zero so sign dont matter

or maybe i may simplify it at all ?

fir

unread,
May 25, 2022, 10:57:07 AM5/25/22
to
note btw it works curiously well i mean when i get together some things with this dots keeped togehthet by this joints i may fire a dot in it and it will behave realistically (or semi realistically i mean have angular momentum and various complex moves - and this is all done by like 10-20 lines of code (when skipping the non core ones) there are hovever 2 or 3 problems (one is it is square, i eman joints itself are linear but the colisions are square and it slows down, well known problem) (second problem is it disalows fast moves i mean when ball moves the force of collision is square proportional to depth of penetration of ball in ball (at least i use it square, not sure if other are eually good or definietely worse) in fact the colision routine is liek the above except i dont need the sign .. hovever simply if a ball moves to fast it may just teleport thru the obstacles and to have reasonebly fast nice to looking balls but not very fast ones just reasoneble/medicore i need a speed like +100 per frame (at 50 fps) when bal sizes are like typically of 50 or 100 - this makes i need to put like at least 5 or better 10 physical frames instead of one just to make taht speed of movement of objevts down to 10 form 100 just not to tunel through

thisrd problem is sometimes when hit to much all that link build small figure of dots 'explode' - probably
the link distance between dots began to much, the force generated is in turn too much and it explodes, and i dont know what happens but maybe positions goes to nans or infs (the rest of this ball sets continue to work ok, but eventually everything may explode)

doeas maybe someone know is there some simple addition to this link routine above stoping this explosions?





fir

unread,
May 25, 2022, 11:32:14 AM5/25/22
to
środa, 25 maja 2022 o 16:57:07 UTC+2 fir napisał(a):
soem may seen a demo of this problem

http://filesharing24.com/d/Oen

this demo dont seem to much good but may ilustrate the problem (in dempo press 5,6,7,8,9, 0
to fire dots into linked dots and some eventually will explode, may also pres f9 tu turn some gravity on and then most of them explode

Ben

unread,
May 25, 2022, 11:42:21 AM5/25/22
to
Ah, true.

--
Ben.

fir

unread,
May 26, 2022, 4:02:44 AM5/26/22
to
środa, 25 maja 2022 o 17:32:14 UTC+2 fir napisał(a):
>
> this demo dont seem to much good but may ilustrate the problem (in dempo press 5,6,7,8,9, 0
> to fire dots into linked dots and some eventually will explode, may also pres f9 tu turn some gravity on and then most of them explode

this is very simple simulation (seems most simplest that can be) but im usually interested in some
kind of fundaments in such things..still a bit pity processors are so slow and things began to choke
quite early at lieas when using native methods...such simulation is hovever totally adequate to paralisation in turn as you say you got 1 milion dots with pos and v and only need to produce same 1 milion dots with pos and v when order atc totally dont matter

hovever it seems i got no time to toy with that

fir

unread,
May 26, 2022, 11:00:05 AM5/26/22
to
środa, 25 maja 2022 o 15:19:20 UTC+2 fir napisał(a):
ovaralli i would say 5ns/15 cycles for this it is +=0.33, sum+=sign2(f), check if f<333333.3 is not so bad
but also not a aterribly quick (imo such sole sign could be someting like 3 cycles or so)
but hell knows which it is this if in loop or what

lat time when i checked it it was quite long time ago the ifs was quite slow and it seem to me that
processors would definitively need soem asm opcodes that would do kinda of local ifs without ifs
but being small set of oparatiobs able to be combined to make more complex tasks (like various
arithmetic without real ifs)

meybe in a word where memory bandwith is main limit it doesnt matter so much but still 1-core
strightforward peograming is so nice that any able optimisation would be wery much welcome by me

what could be such ifless operations? i dont know maybe something like this old asm opcodes liek adc using falgs set to do somethink or do not or do soemething other, there those flags works liek ifs

it also eventually could be probably some opcodes that may combine some test and operation in one opcode

Andrey Tarasevich

unread,
May 27, 2022, 9:17:34 PM5/27/22
to
I presume that your "float standard" is IEEE754, which is not a part of
C standard, but anyway...

The important part of the rationale behind IEEE754 floating-point
representation was the fact that for ordering comparison purposes
floating-point numbers could be type-punned as equally-sized signed
integers and compared as signed integers. This allowed "weak" hardware
(i.e. slow/emulated FP support) to perform plain FP comparisons without
having to invoke anything FP-related at all. (Of course, one had to make
sure one's happy with what happened to such special values as negative
zeroes.)

So, in your case this means that you can type-pun-hack the original FP
problem into an integer problem. Now you can start a topic titled
"eficient int sign routine".

--
Best regards,
Andrey

Richard Damon

unread,
May 27, 2022, 10:03:17 PM5/27/22
to
I thought floating point was stored as signed-magnitude, so you can't
just compare as signed integers, but the comparison was still pretty
quick, you first compare signs, and only if same did you compare
magnatude, but the fields were sorted to make the magnitude comparison
just work.

Andrey Tarasevich

unread,
May 27, 2022, 10:57:41 PM5/27/22
to
You are right. They have to be type-punned as signed magnitude
specifically in order to be compared as integers.

--
Best regards,
Andrey


fir

unread,
May 28, 2022, 1:06:49 AM5/28/22
to
people shpul;d trop they thinking that c is only what is defined in standard,
c is what people use not of what only is defined by 'standard'

i think much can be done by using float binary representation which in turn
is not hard, kinda weird they dont used 24 bits on lower 'integer' part (it would make round 3 bytes
then) im not shure also if that shift of 'dot' part is done right

1 is for example 00111111100000000000000000000000
2 is 01000000000000000000000000000000
4 is 01000000100000000000000000000000
(according to https://www.h-schmidt.net/FloatConverter/IEEE754.html )

as powers of 2 are always in fo rm 100.......0 and the first 1 is not stored shouldnt 4
be [2][0] (when first is exponent part and second are integer bits) ?
i think also this sign moved to highest bite is not necessary best idea it
could be better just have 3 bytes for integer (including its sign) and 1 byte for 'exponent'

hovever in fact, this sign not necesarrely need to be in this integer part as the highest bit is already skipped to simple copying will not recreate the oryginal integer

hovever i think probbaly this float should be done like this that 3 bytes would be identical as
normal 3 bytes of integer so zero should be 00000000 00000000 0000000 and 1 shopuld be 00000000 00000000 00000001 minus one should be 11111111 11111111 11111111 as to exponent part i dont know but probably should denote zeros/ones added lefot or right or something

this problem needs to be rethinked though,m though my brain like lost interestmen in that things which used to vitally interest me back days, its curious

fir

unread,
May 28, 2022, 1:49:15 AM5/28/22
to
maybe it should be like that 3 bytes are exact integer, teh exponened should say how many xxxx
add on right ond on left like integerxxxxxxxx. and .xxxxxxinteger though im not sure/probably in case
on negative integer xxxx should be not zeros but ones, im not sure though (and not sure if i like U2 at all)

Bonita Montero

unread,
May 28, 2022, 1:42:13 PM5/28/22
to
In a superior language:

#include <cstdint>
#include <cstring>
#include <limits>
#include <concepts>
#include <type_traits>

template<typename FpType>
requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
&& std::numeric_limits<FpType>::is_iec559
std::conditional_t<std::same_as<FpType, float>, std::int32_t,
std::int64_t> giveSign( FpType value )
{
using namespace std;
conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
memcpy( &iValue, &value, sizeof value );
return iValue;
}

template
std::int32_t giveSign( float );


template
std::int64_t giveSign( double );

Gives:

"??$giveSign@M@@YAHM@Z":
movd eax, xmm0
ret

"??$giveSign@N@@YA_JN@Z":
movq rax, xmm0
ret

Bonita Montero

unread,
May 28, 2022, 1:49:38 PM5/28/22
to
Sorry, litle mistake:

#include <cstdint>
#include <cstring>
#include <limits>
#include <concepts>
#include <type_traits>

template<typename FpType>
requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
&& std::numeric_limits<FpType>::is_iec559
int giveSign( FpType value )
{
using namespace std;
conditional_t<same_as<FpType, float>, int32_t, int64_t> iValue;
memcpy( &iValue, &value, sizeof value );
return (iValue > 0) - (iValue < 0);
}

template
int giveSign( float );

template
int giveSign( double );

Gives:

"??$giveSign@M@@YAHM@Z":
movd eax, xmm0
xor ecx, ecx
test eax, eax
setg cl
sar eax, 31
add eax, ecx
ret

"??$giveSign@N@@YAHN@Z":
movq rax, xmm0
xor ecx, ecx
test rax, rax
setg cl
sar rax, 63
add eax, ecx

Compiler: clang-cl 13.

Scott Lurndal

unread,
May 28, 2022, 3:43:39 PM5/28/22
to
Bonita Montero <Bonita....@gmail.com> writes:
>In a superior language:
>
>#include <cstdint>
>#include <cstring>
>#include <limits>
>#include <concepts>
>#include <type_traits>
>
>template<typename FpType>
> requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
>&& std::numeric_limits<FpType>::is_iec559
>std::conditional_t<std::same_as<FpType, float>, std::int32_t,
>std::int64_t> giveSign( FpType value )
>{
> using namespace std;
> conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
> memcpy( &iValue, &value, sizeof value );
> return iValue;
>}
>
>template
>std::int32_t giveSign( float );
>
>
>template
>std::int64_t giveSign( double );

In an even more superior language.

#include <math.h>

int main()
{
float_t number = -1.235;

sign = signbit(number);
return sign;
}

Manfred

unread,
May 28, 2022, 4:52:25 PM5/28/22
to
On 5/28/2022 9:43 PM, Scott Lurndal wrote:
> Bonita Montero <Bonita....@gmail.com> writes:
>> In a superior language:
>>
>> #include <cstdint>
>> #include <cstring>
>> #include <limits>
>> #include <concepts>
>> #include <type_traits>
>>
>> template<typename FpType>
>> requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
>> && std::numeric_limits<FpType>::is_iec559
>> std::conditional_t<std::same_as<FpType, float>, std::int32_t,
>> std::int64_t> giveSign( FpType value )
>> {
>> using namespace std;
>> conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
>> memcpy( &iValue, &value, sizeof value );
>> return iValue;
>> }
>>
>> template
>> std::int32_t giveSign( float );
>>
>>
>> template
>> std::int64_t giveSign( double );
>
> In an even more superior language.

Agreed with the "even superior" part, at least in this case.

>
> #include <math.h>
>
> int main()
> {
> float_t number = -1.235;
>
> // sign = signbit(number);

int sign = signbit(number);

> return sign;
> }

Ben

unread,
May 28, 2022, 8:01:44 PM5/28/22
to
Manfred <non...@add.invalid> writes:

> On 5/28/2022 9:43 PM, Scott Lurndal wrote:
>> Bonita Montero <Bonita....@gmail.com> writes:
>>> In a superior language:
>>>
>>> #include <cstdint>
>>> #include <cstring>
>>> #include <limits>
>>> #include <concepts>
>>> #include <type_traits>
>>>
>>> template<typename FpType>
>>> requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
>>> && std::numeric_limits<FpType>::is_iec559
>>> std::conditional_t<std::same_as<FpType, float>, std::int32_t,
>>> std::int64_t> giveSign( FpType value )
>>> {
>>> using namespace std;
>>> conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
>>> memcpy( &iValue, &value, sizeof value );
>>> return iValue;
>>> }
>>>
>>> template
>>> std::int32_t giveSign( float );
>>>
>>>
>>> template
>>> std::int64_t giveSign( double );
>> In an even more superior language.
>
> Agreed with the "even superior" part, at least in this case.

Although C++ also has the signbit functions.

>> #include <math.h>
>> int main()
>> {
>> float_t number = -1.235;
>> // sign = signbit(number);
>
> int sign = signbit(number);

Yes, missing type but also

int sign = signbitf(number);

if likely to be what's wanted, even though float_t is not a standard
type. Anyway, the point stands that simpler solutions are often
preferable.

>> return sign;
>> }

--
Ben.

Bonita Montero

unread,
May 28, 2022, 8:25:13 PM5/28/22
to
My second implementation does more than that
but gives a -1, 0 or 1 result.

Keith Thompson

unread,
May 28, 2022, 9:42:34 PM5/28/22
to
Ben <ben.u...@bsb.me.uk> writes:
[...]
> Yes, missing type but also
>
> int sign = signbitf(number);
>
> if likely to be what's wanted, even though float_t is not a standard
> type. Anyway, the point stands that simpler solutions are often
> preferable.

Yes, float_t is standard. It's the same as one of the predefined
floating-point types, depending on the value of FLT_EVAL_METHOD.

And there is no signbitf in the standard. signbit() is a macro that
takes an argument of any real floating type.

float_t and signbit() are defined in <math.h>, introduced in C99.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Bonita Montero

unread,
May 29, 2022, 1:17:02 AM5/29/22
to
Even simpler:

#include <cstdint>
#include <cstring>
#include <limits>
#include <concepts>
#include <type_traits>

template<typename FpType>
requires (sizeof(FpType) == 4 || sizeof(FpType) == 8) &&
std::numeric_limits<FpType>::is_iec559
int giveSign( FpType value )
{
using namespace std;
conditional_t<sizeof(FpType) == 4, int32_t, int64_t> iValue;
memcpy( &iValue, &value, sizeof value );
return (int)(iValue > 0) - (int)(iValue < 0);
}

template
int giveSign( float );

template
int giveSign( double );

Now two floating point types are supported that must be four
or eight bytes. They don't have to be float and double.

Kaz Kylheku

unread,
May 29, 2022, 1:52:26 AM5/29/22
to
Problem is, you have to get parliament to vote on this lengthy
and controversial bill, and then get it past the senate.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Bonita Montero

unread,
May 29, 2022, 2:36:15 AM5/29/22
to
Am 29.05.2022 um 07:52 schrieb Kaz Kylheku:
> On 2022-05-29, Bonita Montero <Bonita....@gmail.com> wrote:
>> Even simpler:
>>
>> #include <cstdint>
>> #include <cstring>
>> #include <limits>
>> #include <concepts>
>> #include <type_traits>
>>
>> template<typename FpType>
>> requires (sizeof(FpType) == 4 || sizeof(FpType) == 8) &&
>> std::numeric_limits<FpType>::is_iec559
>> int giveSign( FpType value )
>> {
>> using namespace std;
>> conditional_t<sizeof(FpType) == 4, int32_t, int64_t> iValue;
>> memcpy( &iValue, &value, sizeof value );
>> return (int)(iValue > 0) - (int)(iValue < 0);
>> }
>>
>> template
>> int giveSign( float );
>>
>> template
>> int giveSign( double );
>>
>> Now two floating point types are supported that must be four
>> or eight bytes. They don't have to be float and double.
>
> Problem is, you have to get parliament to vote on this lengthy
> and controversial bill, and then get it past the senate.

People with such trippy phantasies like you are usually
mentally disordered.

Öö Tiib

unread,
May 29, 2022, 4:37:27 AM5/29/22
to
On Sunday, 29 May 2022 at 09:36:15 UTC+3, Bonita Montero wrote:
> People with such trippy phantasies like you are usually
> mentally disordered.

Stop mirroring your health issues to others, it looks sad.

Bonita Montero

unread,
May 29, 2022, 4:38:33 AM5/29/22
to
I'm not the one with such phantasies.

fir

unread,
May 29, 2022, 5:36:34 AM5/29/22
to
youre religious fanatic bonita and this is not lookin healthy

for example i know that c is unparralably better than c++ but i dont feel a religious need to
fight on it in hundreds of posts... you do fight for c++ like hodgin for christ and that
is kinda sick.. such war fanatism is not healthly

fir

unread,
May 29, 2022, 5:40:40 AM5/29/22
to
only this assembly is interesting for me ..still for a such thing as sign it seem a
long assembly

as i said teh oryginal problem is to count

float d = sqrt(dx*dx + dy*dy + dz*dz);
float dd = (d - link_dist);
float p = -sign(dd)*dd*dd* joint_hardness;

as fast as it can, and puttiung this couple of instructions to sole sign seems
a bit of overhead

Bonita Montero

unread,
May 29, 2022, 5:55:14 AM5/29/22
to
Am 29.05.2022 um 11:36 schrieb fir:
> niedziela, 29 maja 2022 o 10:38:33 UTC+2 Bonita Montero napisał(a):
>> Am 29.05.2022 um 10:37 schrieb Öö Tiib:
>>> On Sunday, 29 May 2022 at 09:36:15 UTC+3, Bonita Montero wrote:
>>>> People with such trippy phantasies like you are usually
>>>> mentally disordered.
>>>
>>> Stop mirroring your health issues to others, it looks sad.
>> I'm not the one with such phantasies.
>
> youre religious fanatic bonita and this is not lookin healthy

You're the ulta-nerd, not me.

> for example i know that c is unparralably better than c++ ...

Hrhr.
Rest unread.


Bonita Montero

unread,
May 29, 2022, 5:56:05 AM5/29/22
to
Show me the according C-functions with equal performance
and their clang-assembly.

fir

unread,
May 29, 2022, 5:58:55 AM5/29/22
to
why you think i need to show that - its ypour imagination you need to fight a war
proving c++ is better than c...for me its psychiatric

fir

unread,
May 29, 2022, 6:08:21 AM5/29/22
to
on assembly level ( x86 ) there is sign flag that is set by some operation
so in fact to know sing one need like a zero instructions (and thats a good design imo)

though i dont know x86 assembly to much (even though i wrote x86 assembler)
and dont exactly remember which assembly obcode allows some to use this flag
to do the counting

like:
x = sign(d)*d*d

as i said ehen i last measured it conditional jumps are costly in code (even to 30 cycles which
is a lot, but maybe on newer cpu its less) and it would be better to have conditional instructions
not doing conditional jumps, something like

movd xmm0 (d)
mulss xmm0 xmm0
mullss_bysignflag xmm0
movd (x) xmm0

but im not sure as there is adequate instruction (and above the flag probably woudl better
be converted to register value as mulss xmm0 xmm0 probably changes/overwrites flag)

fir

unread,
May 29, 2022, 6:16:22 AM5/29/22
to
niedziela, 29 maja 2022 o 12:08:21 UTC+2 fir napisał(a):
in the assembly you got there is an instruction setg (which seem to set byte to 0 or 1 based on falgs) but i dont know what cl means

https://c9x.me/x86/html/file_module_x86_id_288.html

is there them an instruction converting byte to xmm1 as float?

Bonita Montero

unread,
May 29, 2022, 6:51:45 AM5/29/22
to
It's not a war C++ against C. C++ has obivously advantages over
C, but not here. It's just to say that with C you don't have any
less work.


Bonita Montero

unread,
May 29, 2022, 6:52:46 AM5/29/22
to
> as i said ehen i last measured it conditional jumps are costly in code (even to 30 cycles which
> is a lot, but maybe on newer cpu its less) and it would be better to have conditional instructions
> not doing conditional jumps, something like

Where were the jumps in the clang 13 output I've shown ?

Bonita Montero

unread,
May 29, 2022, 6:53:44 AM5/29/22
to
> in the assembly you got there is an instruction setg (which seem to set byte to 0 or 1 based on falgs) but i dont know what cl means

Sorry, if you don't understand the compiler output
please say nothing.

fir

unread,
May 29, 2022, 6:57:48 AM5/29/22
to
niedziela, 29 maja 2022 o 12:08:21 UTC+2 fir napisał(a):
>
> like:
> x = sign(d)*d*d
>

in fact fabs could go here and i do some tests

void RunFrame(int advance)
{
ClearFrameData(0x505077);
sum = 0;
for(float f= -333333.33; f< 333333.3; f+=0.111)
// sum = -copysignf(1,f)*f*f; //26 ms //29 ms
sum = -fabs(f)*f; //26 ms //34 ms

DrawSomeText2(0xff0000, 10 , 30, "aaaa %f", sum);
}

i test it pitting some dosed in frame ofthe game as my little library has a built in plot
showing frame times so i may see olways live what my game changes effect on frame times

i tested it on gcc 5 and gcc 10 - i more use gcc5 for some reason i dont remember which one
maybe it compiles faster or maybe becouse another reasons (various version of compilers sometimes
compiel some things better and some other worse so its the best to have couple and test them all
from time to time)

some may also test it of have windows, only need to have mingw/tdm installed and put path to in tn compile.vat first line

http://filesharing24.com/d/Ofo

fir

unread,
May 29, 2022, 6:59:34 AM5/29/22
to
youre crazy so you give extreme failure questions i for example did not said nothing about that and it was simply general note - but your state of mind disalows you to read it

fir

unread,
May 29, 2022, 7:00:01 AM5/29/22
to
i must reject, i say what i want

fir

unread,
May 29, 2022, 7:01:08 AM5/29/22
to
niedziela, 29 maja 2022 o 12:57:48 UTC+2 fir napisał(a):
> niedziela, 29 maja 2022 o 12:08:21 UTC+2 fir napisał(a):
> >
> > like:
> > x = sign(d)*d*d
> >
>
> in fact fabs could go here and i do some tests
>
> void RunFrame(int advance)
> {
> ClearFrameData(0x505077);
> sum = 0;
> for(float f= -333333.33; f< 333333.3; f+=0.111)
> // sum = -copysignf(1,f)*f*f; //26 ms //29 ms
> sum = -fabs(f)*f; //26 ms //34 ms
>
> DrawSomeText2(0xff0000, 10 , 30, "aaaa %f", sum);
> }
>
> i test it pitting some dosed in frame ofthe game as my little library has a built in plot
> showing frame times so i may see olways live what my game changes effect on frame times
>
> i tested it on gcc 5 and gcc 10 - i more use gcc5 for some reason i dont remember which one
> maybe it compiles faster or maybe becouse another reasons (various version of compilers sometimes
> compiel some things better and some other worse so its the best to have couple and test them all
> from time to time)
>

i mean those two numbers 29 ms and 34 ms are on gcc5 and 26 26 are on gcc10
(it showed gcc10 is faster here but i wouldnt say its generel rule)

fir

unread,
May 29, 2022, 7:05:30 AM5/29/22
to
niedziela, 29 maja 2022 o 12:57:48 UTC+2 fir napisał(a):
btw if some would like test it when runing it press f4 and hold, it sets sleep in the loop to zero so only there the value is real (sleep is added to not make cpu work on full load, and teh counter counts difference in time including all round (including sleep) but in case of testing one want to sleep zero to see minimal frame time)

Bonita Montero

unread,
May 29, 2022, 7:13:19 AM5/29/22
to
You seem to be manic.
No need to talk to you.

fir

unread,
May 29, 2022, 7:20:11 AM5/29/22
to
niedziela, 29 maja 2022 o 13:13:19 UTC+2 Bonita Montero napisał(a):
> You seem to be manic.
> No need to talk to you.

lol

fir

unread,
May 29, 2022, 7:25:06 AM5/29/22
to
honestly i dont feel manic but im feeling old thats why i got problems with concentration and finding info often recent times... used to be better back then

fir

unread,
May 29, 2022, 7:50:44 AM5/29/22
to
niedziela, 29 maja 2022 o 13:05:30 UTC+2 fir napisał(a):
forgot to mention to show a plot one ned to toggle f2

overally its quite weird how times switch randomly form version to wersion,
there is kinda joke that may be used when talking with ponys who generally thinks hard that
newer version of soft is always better - the joke would be to say that never version has 50% chance
to be better and 50% chance to be worse - i dont use that joke but it seems that reall life resembles this joke a lot

for eample when compiling my library (with some initial scene) with gcc 4.7 30 ms... gcc5.1 25 ms ... gcc10.3 28 ms

fir

unread,
May 29, 2022, 7:55:18 AM5/29/22
to
niedziela, 29 maja 2022 o 13:50:44 UTC+2 fir napisał(a):
> for eample when compiling my library (with some initial scene) with gcc 4.9 30 ms... gcc5.1 25 ms ... gcc10.3 28 ms

with this sign example its in fact oposite 4.7 was 26 ms same as 10.3 when 5.1 was 29ms and 34 ms

Manfred

unread,
May 29, 2022, 11:26:02 AM5/29/22
to
On 5/29/2022 2:01 AM, Ben wrote:
> Manfred <non...@add.invalid> writes:
>
>> On 5/28/2022 9:43 PM, Scott Lurndal wrote:
>>> Bonita Montero <Bonita....@gmail.com> writes:
>>>> In a superior language:
>>>>
>>>> #include <cstdint>
>>>> #include <cstring>
>>>> #include <limits>
>>>> #include <concepts>
>>>> #include <type_traits>
>>>>
>>>> template<typename FpType>
>>>> requires (std::same_as<FpType, float> || std::same_as<FpType, double>)
>>>> && std::numeric_limits<FpType>::is_iec559
>>>> std::conditional_t<std::same_as<FpType, float>, std::int32_t,
>>>> std::int64_t> giveSign( FpType value )
>>>> {
>>>> using namespace std;
>>>> conditional_t<std::same_as<FpType, float>, int32_t, int64_t> iValue;
>>>> memcpy( &iValue, &value, sizeof value );
>>>> return iValue;
>>>> }
>>>>
>>>> template
>>>> std::int32_t giveSign( float );
>>>>
>>>>
>>>> template
>>>> std::int64_t giveSign( double );
>>> In an even more superior language.
>>
>> Agreed with the "even superior" part, at least in this case.
>
> Although C++ also has the signbit functions.

Yes, but that comes from <math.h> or <cmath>, which is substantially
equivalent - in other words, this falls into the inclusion of C into C++.
On the other hand, the proposed alternative, C++ template based, is
considerably worse, IMO.

>
>>> #include <math.h>
>>> int main()
>>> {
>>> float_t number = -1.235;
>>> // sign = signbit(number);
>>
>> int sign = signbit(number);
>
> Yes, missing type but also
>
> int sign = signbitf(number);

I can't find a signbitf function or macro in the C or C++ standard.

>
> if likely to be what's wanted, even though float_t is not a standard
> type.

As Keith said, float_t is declared in <math.h> as part of the standard.
I was surprised too.

Anyway, the point stands that simpler solutions are often
> preferable.

Agreed, that is the gist of the "even superior" part.

>
>>> return sign;
>>> }
>

fir

unread,
May 29, 2022, 12:31:08 PM5/29/22
to
ye thinking a bot more on that i think thet simplicity is the most valueable here (probably)

so this IEEE 754 is not good

what is should be:
highest byte exponent (in U2 from +127 to -128 or maybe -127 and -128 would meen inf)
lowest 3 bytes 24bit integer in U2 ...exponent would meen *2^exp

so zero would be 00000000 00000000 00000000 00000000
1 would be 00000000 00000000 00000000 00000001
-1 would be 00000000 11111111 11111111 11111111
1/2 would be 11111111 00000000 00000000 00000001
-1/2 im not sure 11111111 11111111 1111111 1111111
???

fir

unread,
May 29, 2022, 12:36:39 PM5/29/22
to
though im not sure if the dot should be not movd inituially
so 1 should be 00000000 00000000 0001 0000 00000000
1/2 would be 00000000 00000000 0000 1000 00000000

do teh 1/x would be then liek copying bits in reverse? like 13 = 00001101 so 1/13 = 10110000 ??

fir

unread,
May 29, 2022, 12:41:37 PM5/29/22
to
nah it would be only 1/1 +1/4+1/8 = 13/8

Ben

unread,
May 29, 2022, 2:36:47 PM5/29/22
to
Keith Thompson <Keith.S.T...@gmail.com> writes:

> Ben <ben.u...@bsb.me.uk> writes:
> [...]
>> Yes, missing type but also
>>
>> int sign = signbitf(number);
>>
>> if likely to be what's wanted, even though float_t is not a standard
>> type. Anyway, the point stands that simpler solutions are often
>> preferable.
>
> Yes, float_t is standard. It's the same as one of the predefined
> floating-point types, depending on the value of FLT_EVAL_METHOD.

Never new that. Thanks.

--
Ben.

Tim Rentsch

unread,
Jun 5, 2022, 10:35:53 AM6/5/22
to
Right. C defines a signbit() macro; C++ defines a signbit()
function, overloaded for each of the usual floating-point types.

>> if likely to be what's wanted, even though float_t is not a standard
>> type.
>
> As Keith said, float_t is declared in <math.h> as part of the
> standard. I was surprised too.
>
>> Anyway, the point stands that simpler solutions are often
>> preferable.
>
> Agreed, that is the gist of the "even superior" part.

I'm all in favor of simple solutions, but to count as a solution
the code needs to supply the behavior desired. The code from
Bonita gives a several-valued result (actually a many-valued
result, but the key thing is that it is more than binary).
Apparently what is being looked for is a "signum" function for
floating point, giving -1, 0, or 1, according to whether its
argument is, respectively, less than, equal to, or greater than
zero. This behavior can be supplied with a single (and simple)
function definition (and incidentally one that works regardless
of how floating-point numbers are represented):

#include <math.h>

int
fp_signum( long double x ){
return x ? signbit(x) ? -1 : 1 : 0;
}

If writing in C++, we might need a different #include (I have not
investigated that), and we might want to define two more overloaded
versions, for double and float, if that matters for some reason
(such as performance considerations).

jak

unread,
Jun 5, 2022, 3:12:48 PM6/5/22
to
mmmh ... so in C, using a macro like this, you could make the floating
point type generic:

#define my_sign(x) ((x == 0.) ? 0 : (((unsigned char *)&x)[sizeof(x) -
1] & 0x80) ? -1 : 1)

Tim Rentsch

unread,
Jun 5, 2022, 4:09:59 PM6/5/22
to
jak <nos...@please.ty> writes:

> Il 05/06/2022 16:35, Tim Rentsch ha scritto:

[...]

>> Apparently what is being looked for is a "signum" function for
>> floating point, giving -1, 0, or 1, according to whether its
>> argument is, respectively, less than, equal to, or greater than
>> zero. This behavior can be supplied with a single (and simple)
>> function definition (and incidentally one that works regardless
>> of how floating-point numbers are represented):
>>
>> #include <math.h>
>>
>> int
>> fp_signum( long double x ){
>> return x ? signbit(x) ? -1 : 1 : 0;
>> }
>>
>> [...]
>
> mmmh ... so in C, using a macro like this, you could make the floating
> point type generic: [white space added to avoid long lines]
>
> #define my_sign(x) ( \
> (x == 0.) ? 0 : \
> (((unsigned char *)&x)[sizeof(x) - 1] & 0x80) ? -1 : 1 \
> )

Yes you could.. if you aren't worried about portability, and
you aren't too fussy about getting compilation errors for some
floating-point arguments, and you don't mind getting wrong
answers in some cases.

(Also, macro parameters should be enclosed in parentheses to
avoid unintended parsings. But the problems mentioned remain
even if the macro definition is changed to follow that rule.)

jak

unread,
Jun 6, 2022, 4:00:56 AM6/6/22
to
then changing the macro like this:

#define my_sign(v) \
({ \
long double x = (long double)(v); \
int r = 0; \
if(x != 0.) \
r = (x > 0.) ? 1 : -1; \
r; \
})

and then by casting to the largest of the float types and using only
boolean logic with a macro you can test all the float types that have a
size smaller than or equal to the largest of them and the representation
of the types is not important. If there are further problems I have not
noticed them English is not my language.

Tim Rentsch

unread,
Jun 6, 2022, 7:46:00 PM6/6/22
to
This macro is better than the last one, but it's more complicated
than it has to be, and it needs a non-standard extension to work.

Using a function like the one shown above allows the code to be
standard ISO C, which is an important advantage. If performance
matters the function can be declared 'static inline', which is
likely to give the same performance as a macro expansion.

jak

unread,
Jun 7, 2022, 2:14:15 AM6/7/22
to
Il 07/06/2022 01:45, Tim Rentsch ha scritto:
> This macro is better than the last one, but it's more complicated
> than it has to be, and it needs a non-standard extension to work.
>
> Using a function like the one shown above allows the code to be
> standard ISO C, which is an important advantage. If performance
> matters the function can be declared 'static inline', which is
> likely to give the same performance as a macro expansion.

you are right, but this macro has the advantage that it can be used with
all numeric types and not just floats. sometimes accepting compromises
can be beneficial.

cheers

Ben

unread,
Jun 7, 2022, 6:56:54 AM6/7/22
to
So can Tim's function.

> sometimes accepting compromises can be beneficial.

Sure, but there's no trade-off here.

--
Ben.

jak

unread,
Jun 7, 2022, 1:01:01 PM6/7/22
to
oh i know but i also know that among so many languages, this is the
least suitable for following standards and therefore i shut myself up.

0 new messages