Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All your files have been destroyed (sorry). Paul.


devel / comp.lang.c / Fast trunc() algorithm

SubjectAuthor
* Fast trunc() algorithmBonita Montero
+* Re: Fast trunc() algorithmMuttley
|+- Re: Fast trunc() algorithmBonita Montero
|`- Re: Fast trunc() algorithmJohn McCue
+- Re: Fast trunc() algorithmfir
+* Re: Fast trunc() algorithmChristian Gollwitzer
|+* Re: Fast trunc() algorithmJuha Nieminen
||+- Re: Fast trunc() algorithmDavid Brown
||+- Re: Fast trunc() algorithmmuttley
||`- Re: Fast trunc() algorithmPaavo Helde
|`- Re: Fast trunc() algorithmBonita Montero
+* Re: Fast trunc() algorithmKenny McCormack
|`- Re: Fast trunc() algorithmBonita Montero
+- Re: Fast trunc() algorithmBonita Montero
`* Re: Fast trunc() algorithmBonita Montero
 `* Re: Fast trunc() algorithmBonita Montero
  +- Re: Fast trunc() algorithmBonita Montero
  `* Re: Fast trunc() algorithmBonita Montero
   +* Re: Fast trunc() algorithmjak
   |`* Re: Fast trunc() algorithmBonita Montero
   | `* Re: Fast trunc() algorithmjak
   |  `- Re: Fast trunc() algorithmBonita Montero
   `* Re: Fast trunc() algorithmtTh
    `- Re: Fast trunc() algorithmBonita Montero

1
Fast trunc() algorithm

<t6va0v$eno$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21767&group=comp.lang.c#21767

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Fast trunc() algorithm
Date: Sun, 29 May 2022 10:14:33 +0200
Organization: A noiseless patient Spider
Lines: 215
Message-ID: <t6va0v$eno$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 29 May 2022 08:14:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="617106b4ae0eb2c50f972941a2ba0003";
logging-data="15096"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19owqy80FvUilXG5PuKyZ5rEbqylNwgizU="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:eVGM84y51roVN44iGnmHF1fXGsA=
Content-Language: de-DE
 by: Bonita Montero - Sun, 29 May 2022 08:14 UTC

The trunc() implementation of VC++'s runtime was too slow for me,
so I've written my own:

#include <cstdint>
#include <limits>
#include <cfenv>
#include <cstring>

using namespace std;

double ftrunc( double d )
{ static_assert(sizeof(double) == 8 && numeric_limits<double>::is_iec559,
"double must be IEEE-754");
// assume size_t is our CPU's native register-width
static_assert(sizeof(size_t) == 8 || sizeof(size_t) == 4,
"register-width must be 32 or 64 bit");
if constexpr( sizeof(size_t) == 8 )
// we have 64 bit registers
{
unsigned const
MANTISSA_BITS = 52,
EXP_BIAS = 0x3FF,
INF_NAN_BASE = 0x7FF;
uint64_t const
EXP_MASK = (uint64_t)0x7FF << MANTISSA_BITS,
SIGN_MASK = (uint64_t)0x800 << MANTISSA_BITS ,
MANTISSA_MASK = 0x000FFFFFFFFFFFFFu,
NAN_MASK = 0x0008000000000000u,
MIN_INTEGRAL_DIGITS_EXP = (uint64_t)EXP_BIAS << MANTISSA_BITS,
MIN_INTEGRAL_ONLY_EXP = (uint64_t)(EXP_BIAS + MANTISSA_BITS) <<
MANTISSA_BITS,
INF_NAN_EXP = (uint64_t)INF_NAN_BASE << MANTISSA_BITS;
int64_t const MANTISSA_SHIFT_MASK = 0xFFF0000000000000u;
uint64_t dx;
memcpy( &dx, &d, 8 );
auto retBack = [&]() -> double
{
memcpy( &d, &dx, 8 );
return d;
};
uint64_t exp = dx & EXP_MASK;
if( exp >= MIN_INTEGRAL_DIGITS_EXP )
// value has integral digits
if( exp < MIN_INTEGRAL_ONLY_EXP )
{
// there are fraction-digits to mask out, mask them
unsigned shift = (unsigned)(exp >> MANTISSA_BITS) - EXP_BIAS;
dx &= MANTISSA_SHIFT_MASK >> shift;
return retBack();
}
else
if( exp < INF_NAN_EXP )
// value is integral
return d;
else
{
uint64_t mantissa = dx & MANTISSA_MASK;
// infinite, NaN: return value
if( !mantissa || mantissa & NAN_MASK )
return retBack();
// SNaN: raise exception on SNaN if necessary
feraiseexcept( FE_INVALID );
return retBack();
}
else
{
// below +/-1.0
// return +/-0.0
dx &= SIGN_MASK;
return retBack();
}
}
else
// we have 32 bit registers
{
unsigned const
MANTISSA_BITS = 52,
HI_MANTISSA_BITS = 20,
EXP_BIAS = 0x3FF,
INF_NAN_BASE = 0x7FF;
uint32_t const
EXP_MASK = (uint32_t)0x7FFu << HI_MANTISSA_BITS,
SIGN_MASK = (uint32_t)0x800u << HI_MANTISSA_BITS,
HI_MANTISSA_MASK = 0x000FFFFFu,
NAN_MASK = 0x00080000u,
MIN_INTEGRAL_DIGITS_EXP = (uint32_t) EXP_BIAS << HI_MANTISSA_BITS,
MAX_INTEGRAL32_EXP = (uint32_t)(EXP_BIAS + HI_MANTISSA_BITS) <<
HI_MANTISSA_BITS,
MIN_INTEGRAL_ONLY_EXP = (uint32_t)(EXP_BIAS + MANTISSA_BITS) <<
HI_MANTISSA_BITS,
INF_NAN_EXP = (uint32_t)INF_NAN_BASE << HI_MANTISSA_BITS,
NEG_LO_MANTISSA_SHIFT_MASK = 0xFFFFFFFFu;
int32_t const HI_MANTISSA_SHIFT_MASK = 0xFFF00000;
uint64_t dx;
memcpy( &dx, &d, 8 );
uint32_t
lo = (uint32_t)dx,
hi = (uint32_t)(dx >> 32);
auto retBack = [&]() -> double
{
dx = lo | (uint64_t)hi << 32;
memcpy( &d, &dx, 8 );
return d;
};
uint32_t exp = hi & EXP_MASK;
if( exp >= MIN_INTEGRAL_DIGITS_EXP )
// value has integral digits
if( exp < MIN_INTEGRAL_ONLY_EXP )
// there are fraction-digits to mask out
if( exp <= MAX_INTEGRAL32_EXP )
{
// the fraction digits are in the upper dword, mask them and zero
the lower dword
unsigned shift = (unsigned)(exp >> HI_MANTISSA_BITS) - EXP_BIAS;
hi &= HI_MANTISSA_SHIFT_MASK >> shift;
lo = 0;
return retBack();
}
else
{
// the fraction digits are in the lower dword, mask them
unsigned shift = (unsigned)(exp >> HI_MANTISSA_BITS) - EXP_BIAS -
HI_MANTISSA_BITS;
lo &= ~(NEG_LO_MANTISSA_SHIFT_MASK >> shift);
return retBack();
}
else
if( exp < INF_NAN_EXP )
// value is integral
return retBack();
else
{
uint32_t hiMantissa = hi & HI_MANTISSA_MASK;
// infinite, NaN: return value
if( !(hiMantissa | lo) || hiMantissa & NAN_MASK )
return retBack();
// SNaN: raise exception on SNaN if necessary
feraiseexcept( FE_INVALID );
return retBack();
}
else
{
// below +/-1.0
// return +/-0.0
hi &= SIGN_MASK;
lo = 0;
return retBack();
}
}
}

float ftrunc( float f )
{ static_assert(sizeof(float) == 4, "sizeof(float) not equal to
sizeof(uint32_t)");
static_assert(numeric_limits<float>::is_iec559, "float must be IEEE-754");
unsigned const
MANTISSA_BITS = 23,
EXP_BIAS = 0x7F,
INF_NAN_BASE = 0xFF;
uint32_t const
EXP_MASK = (uint32_t)0xFF << MANTISSA_BITS,
SIGN_MASK = (uint32_t)0x100 << MANTISSA_BITS,
MANTISSA_MASK = 0x007FFFFFu,
NAN_MASK = 0x00400000u,
MIN_INTEGRAL_DIGITS_EXP = (uint32_t) EXP_BIAS << MANTISSA_BITS,
MIN_INTEGRAL_ONLY_EXP = (uint32_t)(EXP_BIAS + MANTISSA_BITS) <<
MANTISSA_BITS,
INF_NAN_EXP = (uint32_t)INF_NAN_BASE << MANTISSA_BITS;
int32_t const MANTISSA_SHIFT_MASK = 0xFF800000u;
uint32_t fx;
memcpy( &fx, &f, 4 );
auto retBack = [&]() -> float
{
memcpy( &f, &fx, 4 );
return f;
};
uint32_t exp = fx & EXP_MASK;
if( exp >= MIN_INTEGRAL_DIGITS_EXP )
// value has integral digits
if( exp < MIN_INTEGRAL_ONLY_EXP )
{
// there are fraction-digits to mask out, mask them
unsigned shift = (unsigned)(exp >> MANTISSA_BITS) - EXP_BIAS;
fx &= MANTISSA_SHIFT_MASK >> shift;
return retBack();
}
else
if( exp < INF_NAN_EXP )
// value is integral
return retBack();
else
{
uint32_t mantissa = fx & MANTISSA_MASK;
// infinite, NaN: return value
if( !mantissa || mantissa & NAN_MASK )
return retBack();
// SNaN: raise exception on SNaN if necessary
feraiseexcept( FE_INVALID );
return retBack();
}
else
{
// below +/-1.0
// return +/-0.0
fx &= SIGN_MASK;
return retBack();
}
}

memcpy() is the only valid way to have writeable aliasing in C++.
memcpy() is normally an intrinsic function and in the above code
the memcpy()s between integral and floating point types are just
moves between general purpose and FPU-registers.

Re: Fast trunc() algorithm

<t6vade$19gk$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21768&group=comp.lang.c#21768

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!BKzeqmo2UYxb4eR2zKm0zw.user.46.165.242.91.POSTED!not-for-mail
From: Mutt...@dastardlyhq.com
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Sun, 29 May 2022 08:21:02 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t6vade$19gk$1@gioia.aioe.org>
References: <t6va0v$eno$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="42516"; posting-host="BKzeqmo2UYxb4eR2zKm0zw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mutt...@dastardlyhq.com - Sun, 29 May 2022 08:21 UTC

On Sun, 29 May 2022 10:14:33 +0200
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>The trunc() implementation of VC++'s runtime was too slow for me,
>so I've written my own:

What do you want, a medal? A scooby snack?

Re: Fast trunc() algorithm

<t6vat5$urr$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21769&group=comp.lang.c#21769

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Sun, 29 May 2022 10:29:35 +0200
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <t6vat5$urr$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t6vade$19gk$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 29 May 2022 08:29:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="617106b4ae0eb2c50f972941a2ba0003";
logging-data="31611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/F0HWvIfvNkwVETSZNB+lqPp3JYS6JRME="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:Wa1lyo+T9WujHyYXB+SHsW4ByKA=
In-Reply-To: <t6vade$19gk$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Sun, 29 May 2022 08:29 UTC

Am 29.05.2022 um 10:21 schrieb Muttley@dastardlyhq.com:
> On Sun, 29 May 2022 10:14:33 +0200
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> The trunc() implementation of VC++'s runtime was too slow for me,
>> so I've written my own:
>
> What do you want, a medal? A scooby snack?

No I don't have your self esteem problems.

Re: Fast trunc() algorithm

<5eadea16-8137-416c-ad84-5cb3077bff82n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21776&group=comp.lang.c#21776

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2947:b0:6a3:a317:fa08 with SMTP id n7-20020a05620a294700b006a3a317fa08mr21636312qkp.746.1653818165211;
Sun, 29 May 2022 02:56:05 -0700 (PDT)
X-Received: by 2002:a05:622a:346:b0:301:f2c5:851f with SMTP id
r6-20020a05622a034600b00301f2c5851fmr2890301qtw.50.1653818165008; Sun, 29 May
2022 02:56:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 29 May 2022 02:56:04 -0700 (PDT)
In-Reply-To: <t6va0v$eno$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.215; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.215
References: <t6va0v$eno$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5eadea16-8137-416c-ad84-5cb3077bff82n@googlegroups.com>
Subject: Re: Fast trunc() algorithm
From: profesor...@gmail.com (fir)
Injection-Date: Sun, 29 May 2022 09:56:05 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: fir - Sun, 29 May 2022 09:56 UTC

niedziela, 29 maja 2022 o 10:14:37 UTC+2 Bonita Montero napisał(a):
> The trunc() implementation of VC++'s runtime was too slow for me,
> so I've written my own:
>

you mean such thing int x = (int) xd; is too slow?

do yu measured it in nanoseconds/cycles

i remember on old borland5 such cast was killing slow (killed
my graphics routines as set pixels which are counted as floats needed that
for each pixel set) (it was based on old x86 instruction set as i dimly remember)
but later begining form 2003 or something they began to use dedicated
assembly instruction to do that and it become reasonable

Re: Fast trunc() algorithm

<t70oqb$pqg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21798&group=comp.lang.c#21798

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Followup: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jmc...@magnetar.hsd1.ma.comcast.net (John McCue)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Followup-To: comp.lang.c
Date: Sun, 29 May 2022 21:32:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <t70oqb$pqg$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t6vade$19gk$1@gioia.aioe.org>
Reply-To: jmclnx@SPAMisBADgmail.com
Injection-Date: Sun, 29 May 2022 21:32:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f2787370d24053a83e9ea48037bd751c";
logging-data="26448"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ewRuUk++TaJZn+Y0DUcP8"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.38 (x86_64))
Cancel-Lock: sha1:YYYWGYoXAErFYEVCkQTdTCi4uC4=
X-OS-Version: Slackware 15.0 x86_64
 by: John McCue - Sun, 29 May 2022 21:32 UTC

Trimmed followups to comp.lang.c

In comp.lang.c Muttley@dastardlyhq.com wrote:
> On Sun, 29 May 2022 10:14:33 +0200
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>The trunc() implementation of VC++'s runtime was too slow for me,
>>so I've written my own:
>
> What do you want, a medal? A scooby snack?

Well if I had a scobby snack I would send it to Bonita.

Thanks Bonita, gives me something to play with :)
John

--
[t]csh(1) - "An elegant shell, for a more... civilized age."
- Paraphrasing Star Wars

Re: Fast trunc() algorithm

<t70p0f$a8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21799&group=comp.lang.c#21799

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: aurio...@gmx.de (Christian Gollwitzer)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Sun, 29 May 2022 23:36:15 +0200
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <t70p0f$a8$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 29 May 2022 21:36:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="baf7ec43ccc8ba5552195ced1e24529b";
logging-data="328"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RR1r6zGiLS3Nzxxpk/936dGiSnhzM+s8="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.9.1
Cancel-Lock: sha1:D+BJEx4XFQl2/5RVIkzgHoVz4E0=
In-Reply-To: <t6va0v$eno$1@dont-email.me>
 by: Christian Gollwitzer - Sun, 29 May 2022 21:36 UTC

Am 29.05.22 um 10:14 schrieb Bonita Montero:
>
> memcpy() is the only valid way to have writeable aliasing in C++.
> memcpy() is normally an intrinsic function and in the above code
> the memcpy()s between integral and floating point types are just
> moves between general purpose and FPU-registers.

How about reinterpret_cast<> ?

Christian

Re: Fast trunc() algorithm

<t71fh9$sftu$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21800&group=comp.lang.c#21800

  copy link   Newsgroups: comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 04:00:41 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <t71fh9$sftu$1@news.xmission.com>
References: <t6va0v$eno$1@dont-email.me>
Injection-Date: Mon, 30 May 2022 04:00:41 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="933822"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 30 May 2022 04:00 UTC

In article <t6va0v$eno$1@dont-email.me>,
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>The trunc() implementation of VC++'s runtime was too slow for me,
>so I've written my own:
>
>#include <cstdint>
>#include <limits>
>#include <cfenv>
>#include <cstring>
>
>using namespace std;

Hmmm... What language is this? What newsgroup is this???

Hmmmm....

--
I love the poorly educated.

Re: Fast trunc() algorithm

<t71nqf$6p4$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21801&group=comp.lang.c#21801

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail
From: nos...@thanks.invalid (Juha Nieminen)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 06:22:09 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t71nqf$6p4$1@gioia.aioe.org>
References: <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="6948"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/5.10.103-grsec-kapsi (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Juha Nieminen - Mon, 30 May 2022 06:22 UTC

In comp.lang.c++ Christian Gollwitzer <auriocus@gmx.de> wrote:
> Am 29.05.22 um 10:14 schrieb Bonita Montero:
>>
>> memcpy() is the only valid way to have writeable aliasing in C++.
>> memcpy() is normally an intrinsic function and in the above code
>> the memcpy()s between integral and floating point types are just
>> moves between general purpose and FPU-registers.
>
> How about reinterpret_cast<> ?

Doing type punning, ie. interpreting the bit representation of some type
as a value of an incompatible type, by reinterpret-casting a pointer to
that value, or by using a union, is technically speaking UB in C++
(but allowed in C, I think since C11 or the like), even though in
practice it does work (for the same reason it does in C).
The only "official" way of doing it (prior to C++20) was to use
std::memcpy() (and hope that the compiler will optimize it into
equivalent code as using a union).

C++20 added an official way of doing type punning: std::bit_cast.

Re: Fast trunc() algorithm

<t71pvp$d5o$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21802&group=comp.lang.c#21802

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 08:59:05 +0200
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <t71pvp$d5o$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me>
<t71nqf$6p4$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 30 May 2022 06:59:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e160607c5ac3a5ecffa6b1401256bfa2";
logging-data="13496"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vTw5+lA210EfqFj7tZA50Mv/dll2UBNo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:iGzP8IwzU6ppAYw4jP+Kilt3uv8=
In-Reply-To: <t71nqf$6p4$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Mon, 30 May 2022 06:59 UTC

On 30/05/2022 08:22, Juha Nieminen wrote:
> In comp.lang.c++ Christian Gollwitzer <auriocus@gmx.de> wrote:
>> Am 29.05.22 um 10:14 schrieb Bonita Montero:
>>>
>>> memcpy() is the only valid way to have writeable aliasing in C++.
>>> memcpy() is normally an intrinsic function and in the above code
>>> the memcpy()s between integral and floating point types are just
>>> moves between general purpose and FPU-registers.
>>
>> How about reinterpret_cast<> ?
>
> Doing type punning, ie. interpreting the bit representation of some type
> as a value of an incompatible type, by reinterpret-casting a pointer to
> that value, or by using a union, is technically speaking UB in C++
> (but allowed in C, I think since C11 or the like), even though in
> practice it does work (for the same reason it does in C).

Using unions for type-punning has, AFAIUI, always been defined behaviour
in C. The wording was changed in C99 to make it clear that it is
allowed, but the way the change was done (as a footnote) indicates that
this was meant as a clarification, not a change to the language. In
C++, however, using unions for type-punning has always been UB. (This
can be viewed as an indication that C++ is more strongly typed than C.)

(You are of course correct that reinterpret_cast can't be used for
accessing one type as an incompatible type.)

> The only "official" way of doing it (prior to C++20) was to use
> std::memcpy() (and hope that the compiler will optimize it into
> equivalent code as using a union).

I believe you could also write your own memcpy version - using char or
unsigned char to access the data, or std::byte in C++17. (You might
want that if you have a poorly optimising compiler that uses a library
call for memcpy even in simple cases.)

>
> C++20 added an official way of doing type punning: std::bit_cast.

Yes.

Re: Fast trunc() algorithm

<t71t3f$jvc$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21803&group=comp.lang.c#21803

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!MWvCYnmDx7hPQlajQ11egA.user.46.165.242.91.POSTED!not-for-mail
From: mutt...@dastardlyhq.com
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 07:52:15 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t71t3f$jvc$1@gioia.aioe.org>
References: <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me> <t71nqf$6p4$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="20460"; posting-host="MWvCYnmDx7hPQlajQ11egA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: mutt...@dastardlyhq.com - Mon, 30 May 2022 07:52 UTC

On Mon, 30 May 2022 06:22:09 -0000 (UTC)
Juha Nieminen <nospam@thanks.invalid> wrote:
>C++20 added an official way of doing type punning: std::bit_cast.

Haven't heard of that. Useful to know. Thanks.

Re: Fast trunc() algorithm

<t720vt$pul$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21804&group=comp.lang.c#21804

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: eesn...@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 11:58:36 +0300
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <t720vt$pul$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me>
<t71nqf$6p4$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 30 May 2022 08:58:37 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5ff8b9c122c0a2a2753a089dbe2c2325";
logging-data="26581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9tFiYLH6DTLPFTu9DSeIJOirNKKk3zt8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:0zxE7YIfe/o9zVsVP0GyLUDsxdg=
In-Reply-To: <t71nqf$6p4$1@gioia.aioe.org>
Content-Language: en-US
 by: Paavo Helde - Mon, 30 May 2022 08:58 UTC

30.05.2022 09:22 Juha Nieminen kirjutas:

> Doing type punning, ie. interpreting the bit representation of some type
> as a value of an incompatible type, by reinterpret-casting a pointer to
> that value, or by using a union, is technically speaking UB in C++
> (but allowed in C, I think since C11 or the like), even though in
> practice it does work (for the same reason it does in C).

It might work sometimes, until it doesn't. Here is a counter-example. It
uses std::launder() (which ought to be kind of a null-op, not quite sure
why using it makes a difference).

$ cat test1.cpp
#include <iostream>
#include <new>
int main() {
float x = 3.14;
int y = *std::launder(reinterpret_cast<int*>(&x));
std::cout << y << "\n";
}

$ g++ -std=c++20 -O0 -Wall test1.cpp
$ ./a.exe
1078523331

$ g++ -std=c++20 -O3 -Wall test1.cpp
$ ./a.exe
0

You can see with -O3 it has "optimized" the result to be 0. It has all
the rights to do that because this code contains UB-s.

With memcpy it works as intended also with -O3:

$ cat test1.cpp
#include <iostream>
#include <cstring>
int main() {
float x = 3.14;
int y;
std::memcpy(&y, &x, sizeof(int));
std::cout << y << "\n";
}

$ g++ -std=c++20 -O0 -Wall test1.cpp
$ ./a.exe
1078523331
$ g++ -std=c++20 -O3 -Wall test1.cpp
$ ./a.exe
1078523331

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/10/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with:
/mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-10.2.0/configure
--srcdir=/mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-10.2.0 --prefix=/usr
--exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc
--docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C
--build=x86_64-pc-cygwin --host=x86_64-pc-cygwin
--target=x86_64-pc-cygwin --without-libiconv-prefix
--without-libintl-prefix --libexecdir=/usr/lib
--with-gcc-major-version-only --enable-shared --enable-shared-libgcc
--enable-static --enable-version-specific-runtime-libs
--enable-bootstrap --enable-__cxa_atexit --with-dwarf2
--with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++
--enable-graphite --enable-threads=posix --enable-libatomic
--enable-libgomp --enable-libquadmath --enable-libquadmath-support
--disable-libssp --enable-libada --disable-symvers --with-gnu-ld
--with-gnu-as --with-cloog-include=/usr/include/cloog-isl
--without-libiconv-prefix --without-libintl-prefix --with-system-zlib
--enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible
--enable-libstdcxx-filesystem-ts
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC)

Re: Fast trunc() algorithm

<t722ut$8a6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21805&group=comp.lang.c#21805

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.freedyn.de!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 11:32:27 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <t722ut$8a6$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t70p0f$a8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 30 May 2022 09:32:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="afd9f3ce627bc0b3f30e4c192f7cb6ed";
logging-data="8518"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/e7HZCKDYQNmTEUYBe8wrUSrRWq5LvXI8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:b1g3pyXP6cibmrQUHxuv84YBmV0=
In-Reply-To: <t70p0f$a8$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Mon, 30 May 2022 09:32 UTC

Am 29.05.2022 um 23:36 schrieb Christian Gollwitzer:
> Am 29.05.22 um 10:14 schrieb Bonita Montero:
>>
>> memcpy() is the only valid way to have writeable aliasing in C++.
>> memcpy() is normally an intrinsic function and in the above code
>> the memcpy()s between integral and floating point types are just
>> moves between general purpose and FPU-registers.
>
> How about reinterpret_cast<> ?

reinterpret_cast<int_type &>(floatTyped) didn't work wheh I tried
this the last time. But for me this isn't a problem since memcpy
works and is intrinsically optimized with MSVC and clang-cl.

Re: Fast trunc() algorithm

<t7232d$8a6$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21806&group=comp.lang.c#21806

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 11:34:19 +0200
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <t7232d$8a6$2@dont-email.me>
References: <t6va0v$eno$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 30 May 2022 09:34:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="afd9f3ce627bc0b3f30e4c192f7cb6ed";
logging-data="8518"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gRySizlYN2n3f+3MMCt6AUmltMzo3WRY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:MJ/EosBzh+oFLkR7/GcyYOzr5jA=
In-Reply-To: <t6va0v$eno$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Mon, 30 May 2022 09:34 UTC

I had a look at the g++ code. g++ is even faster because it simply
does an integer-store and load in series and checks for the overflow
-flag while storing. If it is sed to choses another, more complex,
path.
Maybe there are some MSVC-intrinsics that allow this conversion from
float to int and to have a char, signalling the overflow-flag.

Re: Fast trunc() algorithm

<t72342$8a6$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21807&group=comp.lang.c#21807

  copy link   Newsgroups: comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Mon, 30 May 2022 11:35:12 +0200
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <t72342$8a6$3@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t71fh9$sftu$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 30 May 2022 09:34:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="afd9f3ce627bc0b3f30e4c192f7cb6ed";
logging-data="8518"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AaAprxidCedh3Es08Qqegq5Hhh/0uPzI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:UtXJs5cYDw1abkt+wLlyP5G6s6k=
In-Reply-To: <t71fh9$sftu$1@news.xmission.com>
Content-Language: de-DE
 by: Bonita Montero - Mon, 30 May 2022 09:35 UTC

Am 30.05.2022 um 06:00 schrieb Kenny McCormack:
> In article <t6va0v$eno$1@dont-email.me>,
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> The trunc() implementation of VC++'s runtime was too slow for me,
>> so I've written my own:
>>
>> #include <cstdint>
>> #include <limits>
>> #include <cfenv>
>> #include <cstring>
>>
>> using namespace std;
>
> Hmmm... What language is this? What newsgroup is this???

It's about the basic algorithm, it woudln't be much different in C.

Re: Fast trunc() algorithm

<t742o0$pha$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21808&group=comp.lang.c#21808

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Tue, 31 May 2022 05:41:02 +0200
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <t742o0$pha$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 31 May 2022 03:40:48 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="57d8ccb6c6a04e5a2416654d3c05d24e";
logging-data="26154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dc+2WNUtVX8GHvtLoBIry8n6oGtRlzUQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:P4/jq5VchEyy1UYWvAfdDax9iwc=
In-Reply-To: <t6va0v$eno$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Tue, 31 May 2022 03:41 UTC

The code can be much simpler and faster:

#include <cstdint>
#include <limits>
#include <cfenv>
#include <cstring>
#include "fmath.h"

using namespace std;

double ftrunc( double d )
{ unsigned const
MANTISSA_BITS = 52,
EXP_BIAS = 0x3FF,
INF_NAN_BASE = 0x7FF;
constexpr uint64_t
EXP_MASK = (uint64_t)0x7FF << MANTISSA_BITS,
MANTISSA_MASK = 0x000FFFFFFFFFFFFFu,
NAN_MASK = 0x0008000000000000u,
MIN_PURE_INTEGRAL_EXP = (uint64_t)(EXP_BIAS + MANTISSA_BITS) <<
MANTISSA_BITS,
INF_NAN_EXP = (uint64_t)INF_NAN_BASE << MANTISSA_BITS;
uint64_t dx;
memcpy( &dx, &d, 8 );
uint64_t exp = dx & EXP_MASK;
if( exp < MIN_PURE_INTEGRAL_EXP ) [[likely]]
return (double)(int64_t)d;
if( exp < INF_NAN_EXP ) [[likely]]
// value has integral only mantissa bits
return d;
uint64_t mantissa = dx & MANTISSA_MASK;
// infinite, NaN: return value
if( !mantissa || mantissa & NAN_MASK ) [[likely]]
return d;
// SNaN: raise exception on SNaN if necessary
feraiseexcept( FE_INVALID );
return d;
}

float ftrunc( float f )
{ static_assert(sizeof(float) == 4, "sizeof(float) not equal to
sizeof(uint32_t)");
static_assert(numeric_limits<float>::is_iec559, "float must be IEEE-754");
constexpr unsigned
MANTISSA_BITS = 23,
EXP_BIAS = 0x7F,
INF_NAN_BASE = 0xFF;
constexpr uint32_t
EXP_MASK = (uint32_t)0xFF << MANTISSA_BITS,
SIGN_MASK = (uint32_t)0x100 << MANTISSA_BITS,
MANTISSA_MASK = 0x007FFFFFu,
NAN_MASK = 0x00400000u,
MIN_PURE_INTEGRAL_EXP = (uint64_t)(EXP_BIAS + MANTISSA_BITS) <<
MANTISSA_BITS,
INF_NAN_EXP = (uint32_t)INF_NAN_BASE << MANTISSA_BITS;
uint32_t fx;
memcpy( &fx, &f, 4 );
uint32_t exp = fx & EXP_MASK;
if( exp < MIN_PURE_INTEGRAL_EXP )
return (float)(int64_t)f;
if( exp < INF_NAN_EXP )
// value has integral only mantissa bits
return f;
uint32_t mantissa = fx & MANTISSA_MASK;
// infinite, NaN: return value
if( !mantissa || mantissa & NAN_MASK )
return f;
// SNaN: raise exception on SNaN if necessary
feraiseexcept( FE_INVALID );
return f;
}

Re: Fast trunc() algorithm

<t782sg$qa4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21810&group=comp.lang.c#21810

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Wed, 1 Jun 2022 18:07:56 +0200
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <t782sg$qa4$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 1 Jun 2022 16:07:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bb8f0ac5e94ee4d40cf7ef27c23d2aeb";
logging-data="26948"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wLljP08RtHqq6Eb+yn8UQN3w1dYvbRWw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:TOaMnNV63ustWBVBvNAGMluShbc=
In-Reply-To: <t742o0$pha$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 1 Jun 2022 16:07 UTC

Unfortunately VC++ calls its own conversion algorithm when converting
from a floating point value to an integer. That makes the before posted
not significantly faster than that of MSVC++ and even slower than that
before.
clang-cl, the mostly MSVC++-compatible version of clang, doesn't do
that but uses the SSE-instructions to convert from floating point to
integer directly and the code runs a lot faster with that. Because
this isn't reliable when you won't switch to clang-cl (the C++20
-featues are partitially a disaster and I've a lot of code that
does compile with current MSVC++ and g++ 11.x) I re-wrote the whole
thing in assembly. Here it is:

PUBLIC ?ftrunc@@YANN@Z

_TEXT SEGMENT
?ftrunc@@YANN@Z PROC
cvttsd2si rax, xmm0
mov rcx, 08000000000000000h
cmp rax, rcx
je complex
cvtsi2sd xmm0, rax
ret
complex:
shr rcx, 52
and rcx, 07FFh
cmp rcx, 07FFh
jne asIs
mov rcx, 0FFFFFFFFFFFFFh
test rax, rcx
jz asIs
mov rcx, 08000000000000h
test rax, rcx
jnz asIs
stmxcsr dword ptr [esp - 4]
or byte ptr [esp - 4], 1
ldmxcsr dword ptr [esp - 4]
ret
asIs:
ret
?ftrunc@@YANN@Z ENDP

?ftrunc@@YAMM@Z PROC
cvttsd2si eax, xmm0
cmp eax, 080000000h
je complex
cvtsi2sd xmm0, eax
ret
complex:
mov ecx, eax
shr ecx, 23
and ecx, 07Fh
cmp ecx, 07Fh
jne asIs
test eax, 07FFFFFh
jz asIs
test eax, 0800000h
jnz asIs
stmxcsr dword ptr [esp - 4]
or byte ptr [esp - 4], 1
ldmxcsr dword ptr [esp - 4]
ret
asIs:
ret
?ftrunc@@YAMM@Z ENDP
_TEXT ENDS

END

You might think why I post assembly code here.
But that's still valid for MSVC C++ programmers only.

Re: Fast trunc() algorithm

<t782vo$s1s$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21811&group=comp.lang.c#21811

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Wed, 1 Jun 2022 18:09:41 +0200
Organization: A noiseless patient Spider
Lines: 2
Message-ID: <t782vo$s1s$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 1 Jun 2022 16:09:28 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bb8f0ac5e94ee4d40cf7ef27c23d2aeb";
logging-data="28732"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fBOBCJHXi+nOlQFdnWkGJlZaRE3r4Jc4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:1vdsYsDS0IR2zgZ9G1cGUdzwSP4=
In-Reply-To: <t782sg$qa4$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 1 Jun 2022 16:09 UTC

>     test eax, 0800000h
test eax, 0400000h

Re: Fast trunc() algorithm

<t7983b$ufs$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21812&group=comp.lang.c#21812

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 04:43:06 +0200
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <t7983b$ufs$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 2 Jun 2022 02:42:51 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2b499170ffb1cca81eae445753d54ece";
logging-data="31228"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Jo89Qi2dFZmAitZmR27Bbu0jLf0K7yZg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:MRSc8J0y/85VvyknvHjZZPvlkFw=
In-Reply-To: <t782sg$qa4$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 2 Jun 2022 02:43 UTC

There's even a simpler way with SSE 4.1:

inline double ftrunc( double d )
{ __m128d mm;
mm.m128d_f64[0] = d;
_mm_round_sd( mm, mm, _MM_FROUND_FLOOR );
return mm.m128d_f64[0];
}

inline double ftrunc( float f )
{ __m128 mm;
mm.m128_f32[0] = f;
_mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
return mm.m128_f32[0];
}

With that a simple double trunc() 11.6 times faster on my CPU.

Re: Fast trunc() algorithm

<t79in2$mul$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21813&group=comp.lang.c#21813

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!hVSmjLLINUtQpjZYk09xWA.user.46.165.242.91.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 07:44:02 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t79in2$mul$1@gioia.aioe.org>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="23509"; posting-host="hVSmjLLINUtQpjZYk09xWA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: jak - Thu, 2 Jun 2022 05:44 UTC

Il 02/06/2022 04:43, Bonita Montero ha scritto:
> There's even a simpler way with SSE 4.1:
>
> inline double ftrunc( double d )
> {
>     __m128d mm;
>     mm.m128d_f64[0] = d;
>     _mm_round_sd( mm, mm, _MM_FROUND_FLOOR );
>     return mm.m128d_f64[0];
> }
>
> inline double ftrunc( float f )
> {
>     __m128 mm;
>     mm.m128_f32[0] = f;
>     _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>     return mm.m128_f32[0];
> }
>
> With that a simple double trunc() 11.6 times faster on my CPU.

#include <math.h>

double myTrunc(double value)
{ return value - fmod(value, 1.);
}

Re: Fast trunc() algorithm

<t79keq$k19$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21814&group=comp.lang.c#21814

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 08:14:00 +0200
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <t79keq$k19$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
<t79in2$mul$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 2 Jun 2022 06:13:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2b499170ffb1cca81eae445753d54ece";
logging-data="20521"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18X23RhBcOh5m+Kds8PqUvjkLRnxoTNHNQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:MbrPL7oTjk/ST/hWNfA+wn8mqp4=
In-Reply-To: <t79in2$mul$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Thu, 2 Jun 2022 06:14 UTC

Am 02.06.2022 um 07:44 schrieb jak:
> Il 02/06/2022 04:43, Bonita Montero ha scritto:
>> There's even a simpler way with SSE 4.1:
>>
>> inline double ftrunc( double d )
>> {
>>      __m128d mm;
>>      mm.m128d_f64[0] = d;
>>      _mm_round_sd( mm, mm, _MM_FROUND_FLOOR );
>>      return mm.m128d_f64[0];
>> }
>>
>> inline double ftrunc( float f )
>> {
>>      __m128 mm;
>>      mm.m128_f32[0] = f;
>>      _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>>      return mm.m128_f32[0];
>> }
>>
>> With that a simple double trunc() 11.6 times faster on my CPU.
>
> #include <math.h>
>
> double myTrunc(double value)
> {
>         return value - fmod(value, 1.);
> }
>

That solution is 194 times slower with g++ 11.1 and 188 times
slower with clang 13. Seems the compilers don't do any special
optimization if the divisor 1.0.

Re: Fast trunc() algorithm

<t79v1g$177a$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21815&group=comp.lang.c#21815

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!hVSmjLLINUtQpjZYk09xWA.user.46.165.242.91.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 11:14:25 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t79v1g$177a$1@gioia.aioe.org>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
<t79in2$mul$1@gioia.aioe.org> <t79keq$k19$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="40170"; posting-host="hVSmjLLINUtQpjZYk09xWA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: jak - Thu, 2 Jun 2022 09:14 UTC

Il 02/06/2022 08:14, Bonita Montero ha scritto:
> Am 02.06.2022 um 07:44 schrieb jak:
>> Il 02/06/2022 04:43, Bonita Montero ha scritto:
>>> There's even a simpler way with SSE 4.1:
>>>
>>> inline double ftrunc( double d )
>>> {
>>>      __m128d mm;
>>>      mm.m128d_f64[0] = d;
>>>      _mm_round_sd( mm, mm, _MM_FROUND_FLOOR );
>>>      return mm.m128d_f64[0];
>>> }
>>>
>>> inline double ftrunc( float f )
>>> {
>>>      __m128 mm;
>>>      mm.m128_f32[0] = f;
>>>      _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>>>      return mm.m128_f32[0];
>>> }
>>>
>>> With that a simple double trunc() 11.6 times faster on my CPU.
>>
>> #include <math.h>
>>
>> double myTrunc(double value)
>> {
>>          return value - fmod(value, 1.);
>> }
>>
>
> That solution is 194 times slower with g++ 11.1 and 188 times
> slower with clang 13. Seems the compilers don't do any special
> optimization if the divisor 1.0.

compared with your solution or with the trunc function of msvc?
also, I think you will need to verify that the SEE feature exists
and is enabled on the processor before using it.

Re: Fast trunc() algorithm

<t7a4hi$tpf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21816&group=comp.lang.c#21816

  copy link   Newsgroups: comp.lang.c++ comp.lang.c de.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c,de.comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 12:48:34 +0200
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <t7a4hi$tpf$1@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
<t79in2$mul$1@gioia.aioe.org> <t79keq$k19$1@dont-email.me>
<t79v1g$177a$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 2 Jun 2022 10:48:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2b499170ffb1cca81eae445753d54ece";
logging-data="30511"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185oXwQCge7AHmZs2JhqmQW4kPfuVhsEyc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:llUznu96Q+J8aN6UifkUFK6+kRs=
In-Reply-To: <t79v1g$177a$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Thu, 2 Jun 2022 10:48 UTC

Am 02.06.2022 um 11:14 schrieb jak:
> Il 02/06/2022 08:14, Bonita Montero ha scritto:
>> Am 02.06.2022 um 07:44 schrieb jak:
>>> Il 02/06/2022 04:43, Bonita Montero ha scritto:
>>>> There's even a simpler way with SSE 4.1:
>>>>
>>>> inline double ftrunc( double d )
>>>> {
>>>>      __m128d mm;
>>>>      mm.m128d_f64[0] = d;
>>>>      _mm_round_sd( mm, mm, _MM_FROUND_FLOOR );
>>>>      return mm.m128d_f64[0];
>>>> }
>>>>
>>>> inline double ftrunc( float f )
>>>> {
>>>>      __m128 mm;
>>>>      mm.m128_f32[0] = f;
>>>>      _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>>>>      return mm.m128_f32[0];
>>>> }
>>>>
>>>> With that a simple double trunc() 11.6 times faster on my CPU.
>>>
>>> #include <math.h>
>>>
>>> double myTrunc(double value)
>>> {
>>>          return value - fmod(value, 1.);
>>> }
>>>
>>
>> That solution is 194 times slower with g++ 11.1 and 188 times
>> slower with clang 13. Seems the compilers don't do any special
>> optimization if the divisor 1.0.
>
> compared with your solution or with the trunc function of msvc?
> also, I think you will need to verify that the SEE feature exists
> and is enabled on the processor before using it.

This featrure came with the first Intel i-series processors.
The generation before were the Core II processors, these are
so ancient and homeopathically used today that I can silently
assume that the code runs.

Re: Fast trunc() algorithm

<t7a6fm$22gp$2@news.gegeweb.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21817&group=comp.lang.c#21817

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!.POSTED.2001:861:5e50:f690:44f:eed2:8031:5244!not-for-mail
From: tth...@none.invalid (tTh)
Newsgroups: comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 13:21:26 +0200
Organization: Gegeweb News Server
Message-ID: <t7a6fm$22gp$2@news.gegeweb.eu>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 2 Jun 2022 11:21:26 -0000 (UTC)
Injection-Info: news.gegeweb.eu; posting-account="tontonth@usenet.local"; posting-host="2001:861:5e50:f690:44f:eed2:8031:5244";
logging-data="68121"; mail-complaints-to="abuse@gegeweb.eu"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.0
Content-Language: en-US
Cancel-Lock: sha256:sgep/tTqaL6hyGjuKZYLX0XDdgNpxxTlz6M1ogHVGYM=
In-Reply-To: <t7983b$ufs$1@dont-email.me>
 by: tTh - Thu, 2 Jun 2022 11:21 UTC

On 6/2/22 04:43, Bonita Montero wrote:

> inline double ftrunc( float f )
^^^^^^
> {
>     __m128 mm;
>     mm.m128_f32[0] = f;
>     _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>     return mm.m128_f32[0];
^^^
> }

Bug or feature ?

--
+------------------------------------------------------------------+
| https://framalibre.org/content/tetalab |
+------------------------------------------------------------------+

Re: Fast trunc() algorithm

<t7a74q$hq9$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21818&group=comp.lang.c#21818

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Fast trunc() algorithm
Date: Thu, 2 Jun 2022 13:32:58 +0200
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <t7a74q$hq9$4@dont-email.me>
References: <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>
<t782sg$qa4$1@dont-email.me> <t7983b$ufs$1@dont-email.me>
<t7a6fm$22gp$2@news.gegeweb.eu>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 2 Jun 2022 11:32:42 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2b499170ffb1cca81eae445753d54ece";
logging-data="18249"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18M0beED2VJ2RLvnKc/bVjYqo0jgBT+9T4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:5bbfIGa48aw7C2jNRrIRPCaFd9o=
In-Reply-To: <t7a6fm$22gp$2@news.gegeweb.eu>
Content-Language: de-DE
 by: Bonita Montero - Thu, 2 Jun 2022 11:32 UTC

Am 02.06.2022 um 13:21 schrieb tTh:
> On 6/2/22 04:43, Bonita Montero wrote:
>
>> inline double ftrunc( float f )
>          ^^^^^^
>> {
>>      __m128 mm;
>>      mm.m128_f32[0] = f;
>>      _mm_round_ss( mm, mm, _MM_FROUND_FLOOR );
>>      return mm.m128_f32[0];
>                       ^^^
>> }
>
>   Bug or feature ?

This intrinsic takes a 128 bit SIMD data type with four floats.
Only the lower one is needed but the whole SIMD data type has
to be feeded to _mm_round_ss(). And the result is taken from
the lowest of this four floats, i.e. .m128_f32[0].

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor