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


Groups > comp.lang.c++ > #84439

Re: Fast trunc() algorithm

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++, comp.lang.c, de.comp.lang.c
Subject Re: Fast trunc() algorithm
Date 2022-06-01 18:07 +0200
Organization A noiseless patient Spider
Message-ID <t782sg$qa4$1@dont-email.me> (permalink)
References <t6va0v$eno$1@dont-email.me> <t742o0$pha$1@dont-email.me>

Cross-posted to 3 groups.

Show all headers | View raw


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.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 10:14 +0200
  Re: Fast trunc() algorithm Muttley@dastardlyhq.com - 2022-05-29 08:21 +0000
    Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 10:29 +0200
      Re: Fast trunc() algorithm Freethinker <freethinker@mymail.com> - 2022-05-29 15:20 +0200
        Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 15:27 +0200
          Re: Fast trunc() algorithm Freethinker <freethinker@mymail.com> - 2022-05-29 15:37 +0200
            Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-29 15:46 +0200
    Re: Fast trunc() algorithm John McCue <jmccue@magnetar.hsd1.ma.comcast.net> - 2022-05-29 21:32 +0000
  Re: Fast trunc() algorithm Christian Gollwitzer <auriocus@gmx.de> - 2022-05-29 23:36 +0200
    Re: Fast trunc() algorithm Juha Nieminen <nospam@thanks.invalid> - 2022-05-30 06:22 +0000
      Re: Fast trunc() algorithm David Brown <david.brown@hesbynett.no> - 2022-05-30 08:59 +0200
      Re: Fast trunc() algorithm muttley@dastardlyhq.com - 2022-05-30 07:52 +0000
      Re: Fast trunc() algorithm Paavo Helde <eesnimi@osa.pri.ee> - 2022-05-30 11:58 +0300
    Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-30 11:32 +0200
  Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-30 11:34 +0200
  Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-31 05:41 +0200
    Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 18:07 +0200
      Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 18:09 +0200
      Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 04:43 +0200
        Re: Fast trunc() algorithm jak <nospam@please.ty> - 2022-06-02 07:44 +0200
          Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 08:14 +0200
            Re: Fast trunc() algorithm jak <nospam@please.ty> - 2022-06-02 11:14 +0200
              Re: Fast trunc() algorithm Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-02 12:48 +0200

csiph-web