Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7957 Discussions

addcarry intrinsics generates wrong assembly and constexpr requires the variable to be initialized

cqwrteur
Beginner
968 Views

Please fix the bug.

 

https://godbolt.org/z/GPfEaP

0 Kudos
5 Replies
RahulV_intel
Moderator
954 Views

Hi,


Thanks for reporting the bugs. I have forwarded your issues to the concerned team for a fix.



Regards,

Rahul


0 Kudos
Viet_H_Intel
Moderator
936 Views

I copied the test case from godbolt.org, it compiled successfully with IPSXE2020.

If it is a runtime problem with Intel compiler, can you please provide us a test case?

$ icc -V

Intel(R) C Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.1.2.275 Build 20200623

$ icpc t.cpp -std=c++20 -Ofast -march=native -c

$ cat t.cpp

#include<cstdint>

#include<array>

#if defined(_MSC_VER)

#include<intrin.h>

#elif defined(__x86_64__) || defined(__i386__)

#include<immintrin.h>

#endif

using field_number = std::conditional_t<sizeof(std::size_t)>=8,std::array<std::uint64_t,4>,std::array<std::uint32_t,8>>;

namespace intrinsics

{

template<typename T>

inline constexpr bool add_carry(bool carry,T a,T b,T& out) noexcept

{

#if defined(_MSC_VER) || defined(__x86_64__) || defined(__i386__)

#if __cpp_lib_is_constant_evaluated >= 201811L

    if(std::is_constant_evaluated())

        return (out=a+b+carry)<a;

    else

#endif

  {

    if constexpr(sizeof(T)==8)

      return _addcarry_u64(carry,a,b,

#if !defined(__INTEL_COMPILER ) &&(defined(__GNUC__) || defined(__clang__))

      reinterpret_cast<unsigned long long*>(std::addressof(out)));

#else

      std::addressof(out));

#endif

    else

      return _addcarry_u32(carry,a,b,std::addressof(out));

  }

#else

    return (out=a+b+carry)<a;

#endif

}


}


inline constexpr

field_number add_reduce(field_number const& x,field_number const& y) noexcept

{

    using namespace intrinsics;

    field_number f

#ifdef __INTEL_COMPILER

  {}

#endif

  ;

    if constexpr(sizeof(std::size_t)>=8)

    {

/*

 * GCC addcarry bug

 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97387

 * */

        bool carry{add_carry(false,x[0],y[0],f[0])};

        carry=add_carry(carry,x[1],y[1],f[1]);

        carry=add_carry(carry,x[2],y[2],f[2]);

        add_carry(carry,x[3],y[3],f[3]);

    }

    else

    {

        bool carry{add_carry(false,x[0],y[0],f[0])};

        carry=add_carry(carry,x[1],y[1],f[1]);

        carry=add_carry(carry,x[2],y[2],f[2]);

        carry=add_carry(carry,x[3],y[3],f[3]);

        carry=add_carry(carry,x[4],y[4],f[4]);

        carry=add_carry(carry,x[5],y[5],f[5]);

        carry=add_carry(carry,x[6],y[6],f[6]);

        add_carry(carry,x[7],y[7],f[7]);

    }

    return f;

}


#ifdef __INTEL_COMPILER

field_number add_reduce2(field_number const& x,field_number const& y) noexcept

{

    using namespace intrinsics;

    field_number f

#ifdef __INTEL_COMPILER

  {}

#endif

  ;

  bool carry{_addcarry_u64(false,x[0],y[0],f.data()+0)};

  carry=_addcarry_u64(carry,x[1],y[1],f.data()+1);

  carry=_addcarry_u64(carry,x[2],y[2],f.data()+2);

  _addcarry_u64(carry,x[3],y[3],f.data()+3);

  return f;

}

#endif




0 Kudos
Viet_H_Intel
Moderator
919 Views

This smaller test case (constructed on your test case) compiled fine with either icpc (19.1.2) or g++ (8.1.0)

$ icpc t.cpp -std=c++2a -c

$g++ t.cpp -std=c++2a -c

$cat t.cpp

#include<cstdint>

#include<array>

#include<immintrin.h>

using field_number = std::conditional_t<sizeof(std::size_t)>=8,std::array<std::uint64_t,4>,std::array<std::uint32_t,8>>;

namespace intrinsics

{

template<typename T>

inline constexpr bool add_carry(bool carry,T a,T b,T& out) noexcept

{

    return (out=a+b+carry)<a;

}


}


inline constexpr

field_number add_reduce(field_number const& x,field_number const& y) noexcept

{

    using namespace intrinsics;

    field_number f

#ifdef __INTEL_COMPILER

  {}


#elif defined __GNUC__

  {}

#endif


  ;

  return f;

}



0 Kudos
Viet_H_Intel
Moderator
742 Views

Let us know if this is still an issue. Otherwise, we will close it.


Thanks,


0 Kudos
Viet_H_Intel
Moderator
728 Views

We will no longer respond to this thread.  

If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

Thanks,


0 Kudos
Reply