Re: [v8-dev] optimise Math.floor(x/y) to use integer divisiion for specific divisor.... (issue 9638018)

17 views
Skip to first unread message

fschn...@chromium.org

unread,
Apr 23, 2012, 1:29:59 PM4/23/12
to rodolph....@gmail.com, da...@chromium.org, alexand...@gmail.com, ale...@chromium.org, v8-...@googlegroups.com

Florian Schneider

unread,
Apr 23, 2012, 2:14:15 PM4/23/12
to rodolph....@gmail.com, da...@chromium.org, alexand...@gmail.com, ale...@chromium.org, v8-...@googlegroups.com
Unfortunately I had to revert the change because of Win32 compilation errors:

Alexandre Rames

unread,
Apr 24, 2012, 4:43:10 AM4/24/12
to Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
It seems
#include <intrin.h>
directive is missing.

msdn example use of __popcnt includes it.
But it seemed to work fine without it so I am not sure.

Florian Schneider

unread,
Apr 24, 2012, 6:38:01 AM4/24/12
to Alexandre Rames, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
Maybe - at the moment I don't have a Windows machine set up to test it. @danno: Maybe someone else with a Windows setup can try this quickly?

Alexandre Rames

unread,
Apr 24, 2012, 6:43:12 AM4/24/12
to Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
We are trying on an old windows machine we have here. I'll update you asap.

Alexandre Rames

unread,
Apr 24, 2012, 9:00:31 AM4/24/12
to Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
__popcnt was introduced with VS2008.
Our VS2005 failed to compile for the same reason.

We acan test MSC_VER and use a manual CountSetBits for versions prior to 2008:

For MSVC++ 8.0 ,   _MSC_VER = 1400

So we can do 
#if _MSV_VER >= 1400
  // Use __popcnt
#else
  // Use hand coded CountSetBits.
#endif

I'll test this solution and upload it if it works.

Alexandre

Florian Schneider

unread,
Apr 24, 2012, 9:10:10 AM4/24/12
to Alexandre Rames, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
Thanks! That sounds good to me.

Alexandre Rames

unread,
Apr 24, 2012, 9:39:13 AM4/24/12
to Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
Ok that works on our machine (with correct VS2008's _MSC_VER 1500).
The patch should come soon.

rodolph....@gmail.com

unread,
Apr 24, 2012, 11:25:14 AM4/24/12
to da...@chromium.org, fschn...@chromium.org, alexand...@gmail.com, ale...@chromium.org, v8-...@googlegroups.com

fschn...@chromium.org

unread,
Apr 24, 2012, 11:36:54 AM4/24/12
to rodolph....@gmail.com, da...@chromium.org, alexand...@gmail.com, ale...@chromium.org, v8-...@googlegroups.com
Thanks. Still LGTM.

I'll re-land it

http://codereview.chromium.org/9638018/

Alexandre Rames

unread,
Apr 24, 2012, 12:14:34 PM4/24/12
to rodolph....@gmail.com, da...@chromium.org, fschn...@chromium.org, alexand...@gmail.com, ale...@chromium.org, v8-...@googlegroups.com
Thanks!

Florian Schneider

unread,
Apr 24, 2012, 1:03:22 PM4/24/12
to Alexandre Rames, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org, v8-...@googlegroups.com
You're welcome. The Win32 builder still not happy. I'm not so familiar with our Windows buildbot configuration, so I'm fixing it for now by just avoiding the problematic compiler intrinsic (http://code.google.com/p/v8/source/detail?r=11428) It should not make a big difference, should it? 

Vitaly Repeshko

unread,
Apr 24, 2012, 1:20:51 PM4/24/12
to v8-...@googlegroups.com, Alexandre Rames, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
FYI. http://msdn.microsoft.com/en-us/library/bb385231.aspx says "If
you run code that uses this intrinsic on hardware that does not
support the popcnt instruction, the results are unpredictable."


-- Vitaly

> --
> v8-dev mailing list
> v8-...@googlegroups.com
> http://groups.google.com/group/v8-dev

Florian Schneider

unread,
Apr 25, 2012, 4:22:18 AM4/25/12
to v8-...@googlegroups.com, Alexandre Rames, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
Hm, interesting. I assume that we don't instruct g++ or msvc to generate any fancy SSE4.1 instructions anyway because V8 has to run on old non-SSE hardware - in which case the benefit of using the compiler intrinsics is probably void.

Alexandre Rames

unread,
Apr 25, 2012, 4:24:38 AM4/25/12
to Florian Schneider, v8-...@googlegroups.com, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
Not using the intrinsic shouldn't make any difference here.

But __popcnt is the intrinsic, not the instruction. Shouldn't MS compiler automatically detect hardware support and use another solution if the popcnt isn't available.

Vitaly Repeshko

unread,
Apr 25, 2012, 12:57:16 PM4/25/12
to v8-...@googlegroups.com, Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
On Wed, Apr 25, 2012 at 1:24 AM, Alexandre Rames
<alexand...@gmail.com> wrote:
> Not using the intrinsic shouldn't make any difference here.
>
> But __popcnt is the intrinsic, not the instruction. Shouldn't MS compiler
> automatically detect hardware support and use another solution if the popcnt
> isn't available.

Maybe it should, but it's not what the MSDN page says it does. If it
did, this code code would have to check some global flag every time,
which would be more expensive than the operation itself, or require
some patching in runtime.


-- Vitaly

Alexei Filippov

unread,
Apr 25, 2012, 1:18:22 PM4/25/12
to Vitaly Repeshko, v8-...@googlegroups.com, Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org

2012/4/25 Vitaly Repeshko <vit...@chromium.org>

On Wed, Apr 25, 2012 at 1:24 AM, Alexandre Rames
<alexand...@gmail.com> wrote:
> Not using the intrinsic shouldn't make any difference here.
>
> But __popcnt is the intrinsic, not the instruction. Shouldn't MS compiler
> automatically detect hardware support and use another solution if the popcnt
> isn't available.

Maybe it should, but it's not what the MSDN page says it does. If it
did, this code code would have to check some global flag every time,
which would be more expensive than the operation itself, or require
some patching in runtime.

He probably meant compile time checking.
I used to implement some sse and avx intrinsics in Sun compiler and it also does not provide generic implementation (emulation) for instruction related intrinsics.
Btw their declarations are guarded by corresponding ifdefs in system headers, so you cannot place a call to intrinsic which is not supported by the target arch.
As for some other intrinsics (not instruction related), e.g. memcpy, strlen, compiler provides multiple implementations that make full use of available instruction set. 

Alexei

Vitaly Repeshko

unread,
Apr 25, 2012, 1:28:09 PM4/25/12
to Alexei Filippov, v8-...@googlegroups.com, Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
On Wed, Apr 25, 2012 at 10:18 AM, Alexei Filippov <ale...@google.com> wrote:
>
> 2012/4/25 Vitaly Repeshko <vit...@chromium.org>
>>
>> On Wed, Apr 25, 2012 at 1:24 AM, Alexandre Rames
>> <alexand...@gmail.com> wrote:
>> > Not using the intrinsic shouldn't make any difference here.
>> >
>> > But __popcnt is the intrinsic, not the instruction. Shouldn't MS
>> > compiler
>> > automatically detect hardware support and use another solution if the
>> > popcnt
>> > isn't available.
>>
>> Maybe it should, but it's not what the MSDN page says it does. If it
>> did, this code code would have to check some global flag every time,
>> which would be more expensive than the operation itself, or require
>> some patching in runtime.
>>
> He probably meant compile time checking.

Compile time checking won't work because this code is compiled only
once and then used on different platforms, including old ones. E.g.,
there is no SSE2 build of V8; whether processor supports SSE2 is
checked in runtime before JIT code is compiled.


-- Vitaly

Alexandre Rames

unread,
Apr 26, 2012, 4:59:26 AM4/26/12
to v8-...@googlegroups.com, Alexei Filippov, Florian Schneider, rodolph....@gmail.com, da...@chromium.org, ale...@chromium.org
I did mean compile time checking.
I agree the msdn page specifies that support for the popcnt instruction should be checked before using the corresponding intrinsic.
I did not realize that the behaviour differs here because we are using an intrinsic, not a builtin like we do for GCC. I agree the compiler should not try to emulate it, even though it would not break porting compatibility.

Cheers!

Alexandre
Reply all
Reply to author
Forward
0 new messages