3

The mulx instruction was introduced with the BMI2 instruction set starting with the Haswell processor.

According to Intel's documentation there should be an intrinsic for mulx

unsigned __int64 umul128(unsigned __int64 a, unsigned __int64 b, unsigned __int64 * hi);

However, I find no such intrinsic from Intel's intrinsic guide online under BMI2 or in general. I do however find the addcarry intrinsics from the ADX instruction set.

According to this link the intrinsic is mulx_u64 but I don't find that one either.

MSVC added a _umul128 intrinsic in MSVC 2005 but that only produces mul and not mulx (and I have no idea how to enable BMI2 in MSVC).

I can produce the mulx instruction indirectly using __int128 in GCC with -mbmi2 (or -march=haswell) but I would prefer to do this more directly using an intrinsic.

Why do the ADX intrinsics exist but not one for mulx as defined in Intel's documentation?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • Are you sure that those intrinsics are in any sense "portable" and don't just refer to the Intel compiler? – Kerrek SB Mar 23 '15 at 09:45
  • @KerrekSB, I don't know. Clang already appears to [support the ADX intrinsics](http://clang.llvm.org/doxygen/adxintrin_8h_source.html). I assume GCC will as well. I tested `umul128` on ICC at http://gcc.godbolt.org/ and it does not appear that even ICC does suports it. But ICC supports `_addcarry_u64` – Z boson Mar 23 '15 at 09:58
  • @KerrekSB, I tested `_addcarry_u64` on Clang and it compiles (though I can't get it produced `adcx` yet). – Z boson Mar 23 '15 at 10:23
  • *"As an example, the following intrinsics can be used to develop 64-bit code:"* - I don't think you should consider this as an 'official' endorsement for an intrinsic. It's a white paper discussing possible uses. That's how I read it. The fact that gcc might implement `_addcarryx_u64` doesn't make it official. I would be disappointed with a compiler that doesn't use these instructions for `[unsigned] __int128` arithmetic. – Brett Hale Mar 23 '15 at 11:08
  • @BrettHale, good point. But then what makes an intrinsic official? Is there e.g. a committee which decides which intrinsics compilers should support? – Z boson Mar 23 '15 at 11:47
  • @Zboson - Well, there's no ISO body in charge:) But my feeling is that once it's part of ICC, that's as 'official' as it gets. But where does this leave AMD? Do they promote through GCC and shame Intel into adoption? BTW, that intrinsics guide is slightly terrifying. It's fast becoming too much for my pretty little head to keep up to date with. – Brett Hale Mar 23 '15 at 12:47
  • 1
    there's an old question [here](https://software.intel.com/en-us/forums/topic/543186) but still no answer – phuclv Mar 23 '15 at 17:26
  • @LưuVĩnhPhúc, I get the feeling these intrinsics were made primarily for MSVC because it does not allow inline assembly. For GCC, ICC, and Clang a solution is to use `__int128`. – Z boson Mar 24 '15 at 08:45

1 Answers1

5

The intrinsic which generates mulx instruction for 64 bit Integer multiplication is _mulx_u64(). Below is an example for the same:

    #include <stdio.h> 
    int main() 
    { 
        unsigned __int64 a = 0x0fffffffffffffff; 
        unsigned __int64 b = 0xf0000000; 
        unsigned __int64 c, d; 
        d = _mulx_u64(a, b, &c); 
        printf_s("%#I64x * %#I64x = %#I64x%I64x\n", a, b, c, d); 
    }

Variable "c" will hold the higher 64 bits of the result and variable "d" will hold the lower 64 bits of the result. This intrinsic is also supported in Microsoft Visual Studio Compiler. We are working on updating the white paper (New Instructions Support Large Integer Arithmetic) with the right intrinsic. Thanks for bringing this to our attention.

Anoop - Intel
  • 354
  • 2
  • 11
  • 1
    I tried your code in ICC 13.0.1 and it produces `mulx`. It does not work in Visual Studio 2013 though (I enabled /arch:AVX2 as well). Do you have any comments about this [addcarry-u64-and-addcarryx-u64-with-msvc-and-icc](https://stackoverflow.com/questions/29229371/addcarry-u64-and-addcarryx-u64-with-msvc-and-icc)? – Z boson Apr 10 '15 at 07:02
  • When the white paper is updated please update https://software.intel.com/sites/landingpage/IntrinsicsGuide/ as well because `_mulx_u64` is not described there either. – Z boson Apr 10 '15 at 07:52