Does clang++ with libc++ support constexpr math function - math

I know that g++ support constexpr math function. I want to do that on clang++. So I write a simple code.
#include<iostream>
#include<cmath>
int main()
{
constexpr auto a(std::floor(4.3));
std::cout<<a<<std::endl;
}
and then use clang++-libc++ -std=c++1y to compile it and the get the following error:
error: constexpr variable 'a' must be initialized by a constant expression
constexpr auto a(std::floor(4.3));
^ ~~~~~~~~~~~~~~~
note: non-constexpr function 'floor' cannot be used in a constant expression
constexpr auto a(std::floor(4.3));
^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:184:14: note: declared here
__MATHCALLX (floor,, (_Mdouble_ __x), (__const__));
^
/usr/include/math.h:58:26: note: expanded from macro '__MATHCALLX'
__MATHDECLX (_Mdouble_,function,suffix, args, attrib)
^
/usr/include/math.h:60:22: note: expanded from macro '__MATHDECLX'
__MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
^
/usr/include/math.h:63:31: note: expanded from macro '__MATHDECL_1'
extern type __MATH_PRECNAME(function,suffix) args __THROW
^
/usr/include/math.h:66:42: note: expanded from macro '__MATH_PRECNAME'
#define __MATH_PRECNAME(name,r) __CONCAT(name,r)
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:88:23: note: expanded from macro '__CONCAT'
#define __CONCAT(x,y) x ## y
I use clang-3.5. So I want to ask whether clang++ support constexpr math function. If so, what compiler flag I need to pass to clang?

cppreference doesn't declare std::floor as constexpr. Not sure whether any standard does. I guess compilers might want to avoid implementing this unless it's in some standard, to avoid incompatible behavior. According to the manual, clang aims for support of C++11 and C++1y (likely C++14), with no extensions of C++ features mentioned.

Related

Problem compiling Cypress software with IAR

I've a Cypress BLE module and have compilation problem with IAR.
In "ezsapi.h" are defined this macros:
#ifdef __GNUC__
/* standard GNU C */
#ifdef _WIN32
/* MinGW, Cygwin, TDM-GCC, etc. */
#define __PACKDEF(STRUCTNAME, STRUCTDEF) typedef struct STRUCTDEF __attribute__((__packed__,gcc_struct)) STRUCTNAME
#else
/* generic gcc */
#define __PACKDEF(STRUCTNAME, STRUCTDEF) typedef struct STRUCTDEF __attribute__((__packed__)) STRUCTNAME
#endif
#define ALIGNED __attribute__((aligned(0x4)))
#else
/* Microsoft Visual C++ */
#define __PACKDEF(STRUCTNAME, STRUCTDEF) __pragma(pack(push, 1)) STRUCTDEF __pragma(pack(pop)) STRUCTNAME
#define ALIGNED
#endif
IAR usually use #pragma pack(push,1) and #pragma pack(pop) and I tryed to modify the macro in:
#define __PACKDEF(STRUCTNAME, STRUCTDEF) #pragma(pack(push, 1)) STRUCTDEF #pragma(pack(pop)) STRUCTNAME
With original macros the errors reported is:
ezsapi.h(694) : Error[Pe020]: identifier "pack" is undefined
ezsapi.h(694) : Error[Pe018]: expected a ")" ezsapi.h(694) :
Error[Pe079]: expected a type specifier ezsapi.h(694) : Error[Pe260]:
explicit type is missing ("int" assumed) ezsapi.h(694) : Error[Pe141]:
unnamed prototyped parameters not allowed when body is present
ezsapi.h(694) : Error[Pe130]: expected a "{"
and with my macro the errors reported is:
(69 is the line where the macro is located)
ezsapi.h(69) : Error[Pe052]: expected a macro parameter name
ezsapi.h(69) : Error[Pe052]: expected a macro parameter name
ezsapi.h(694) : Error[Pe020]: identifier "pack" is undefined
ezsapi.h(694) : Error[Pe018]: expected a ")" ezsapi.h(694) :
Error[Pe079]: expected a type specifier ezsapi.h(694) : Error[Pe260]:
explicit type is missing ("int" assumed) ezsapi.h(694) : Error[Pe141]:
unnamed prototyped parameters not allowed when body is present
ezsapi.h(694) : Error[Pe130]: expected a "{"
What's the correct formula for IAR?
What's escaping me?
Thanks.
There are two ways of solving this problem. My suggestion is that you use the __packed type attibute instead of #pragma pack() as this has a more well defined meaning. This, however, needs IAR language extensions to be switched on. If you can't enable language extensions or for some other reason need to use pack-pragma you have to use an alternative pragma syntax to be able to include it in a preprocessor macro. If you use _Pragma("pack(push,1)") and _Pragma("pack(pop)") you macro should work as expected. Definitions of PACKDEF for both alternatives are shown below:
#define PACKDEF(STRUCTNAME, STRUCTDEF) typedef __packed struct STRUCTDEF STRUCTNAME
#define PACKDEF(STRUCTNAME, STRUCTDEF) _Pragma("pack(push,1)") typedef struct STRUCTDEF STRUCTNAME _Pragma("pack(pop)")

How to specify a relative library path in Julia ccall?

to ccall a custom made library, I need to write down the library full path on the system:
j = ccall((:add3, "[FULL_PATH]/libmylib.so"), Float32, (Float32,), 2)
I am trying to use instead a relative path with:
j = ccall((:add3, "$(pwd())/libmylib.so"), Float32, (Float32,), 2)
but, while "$(pwd())/libmylib.so" returns the right path for the library, ccall with pwd returns a TypeError: in ccall: first argument not a pointer or valid constant expression, expected Ptr, got Tuple{Symbol,String}.
So, how to ccall a library that is in the same folder than the Julia script/current working directory ?
I am puzzled, as according to this thread in Windows seems to work, even if the doc for ccall specify:
Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression.
For info, I'm in Ubuntu 18.04 and the library has been implemented with
mylib.c:
float add3 (float i){
return i+3;
}
mylib.h:
#ifndef _MYLIB_H_
#define _MYLIB_H_
extern float get2 ();
extern float add3 (float i);
Compilation (gcc):
gcc -o mylib.o -c mylib.c
gcc -shared -o libmylib.so mylib.o -lm -fPIC
As far as I know this is the most used pattern:
const mylib = joinpath(pwd(), "libmylib.so")
j = ccall((:add3, mylib), Cfloat, (Cfloat,), 2)
Note that pwd can be a bit more in "flux" than you want for a library path, it is probably better to relate it to the file, e.g.
const mylib = joinpath(#__DIR__, "libmylib.so")
where #__DIR__ expands to the directory of the file itself.
As often, I found the solution only after posting on SO.. it seems setting the question helps defining better the problem..
Any how, the solution is to first get the function pointer with cglobal - where I can use pwd() - and then use the ccall method with the function pointer:
f = cglobal((:add3, "$(pwd())/libmylib.so"))
j = ccall(f, Float32, (Float32,), i)

Error compiling E-ACSL FRAMA-C

I am new to Frama-C framework and I am trying to do some contract testing with C programs. I intend to use E-ACSL plugin for this, and I tried a test program to see how it works, but I get some compilation errors. Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x = 0;
/*# assert x == 1;*/
/*# assert x == 0;*/
return 0;
}
Then, here is the Frama-C annotated code:
/* Generated by Frama-C */
#include "stdio.h"
#include "stdlib.h"
struct __e_acsl_mpz_struct {
int _mp_alloc ;
int _mp_size ;
unsigned long *_mp_d ;
};
typedef struct __e_acsl_mpz_struct __e_acsl_mpz_struct;
typedef __e_acsl_mpz_struct ( __attribute__((__FC_BUILTIN__)) __e_acsl_mpz_t)[1];
/*# ghost extern int __e_acsl_init; */
/*# ghost extern int __e_acsl_internal_heap; */
extern size_t __e_acsl_heap_allocation_size;
/*#
predicate diffSize{L1, L2}(ℤ i) =
\at(__e_acsl_heap_allocation_size,L1) -
\at(__e_acsl_heap_allocation_size,L2) ≡ i;
*/
int main(void)
{
int __retres;
int x = 0;
/*# assert x ≡ 1; */ ;
/*# assert x ≡ 0; */ ;
__retres = 0;
return __retres;
}
Finally, I try to compile it with gcc and the flags the manual indicates (page 13) but I get the following errors (and warnings):
$ gcc monitored_second.c -o monitored_second -leacsl -leacsl-gmp -leacsl -jemalloc -lpthread -lm
monitored_second.c:10:1: warning: ‘__FC_BUILTIN__’ attribute directive ignored [-Wattributes]
typedef __e_acsl_mpz_struct ( __attribute__((__FC_BUILTIN__)) __e_acsl_mpz_t)[1];
monitored_second.c:18:55: warning: ‘__FC_BUILTIN__’ attribute directive ignored [-Wattributes]
int line);
^
monitored_second.c:25:60: warning: ‘__FC_BUILTIN__’ attribute directive ignored [-Wattributes]
size_t ptr_size);
^
/usr/bin/ld: cannot find -leacsl
/usr/bin/ld: cannot find -leacsl-jemalloc
collect2: error: ld returned 1 exit status
I've also removed the "-rtl-bittree" label because it returns another error.
Frama-C version is the latest: Sulfur-20171101
Got any idea of what is happening?
Thanks!
Normally, you should have a script called e-acsl-gcc.sh installed in the same directory as frama-c binary, that can take care of calling gcc with appropriate options. Its basic usage is documented in section 2.2 of the manual, and man e-acsl-gcc.sh gives more details on the options that can be used. In short, you should be able to type
e-acsl-gcc.sh -c \
--oexec-eacsl=first_monitored \
--oexec=first \
--ocode=first_monitored.i \
first.i
to obtain
an executable first_monitored with the e-acsl instrumentation
an executable first with the original program
a source file first_monitored.i with the e-acsl generated C code
Edit Looking at the linking command used by the script, I'd say that the command line proposed earlier in the manual is out of date (in particular, it refers to eacsl-jemalloc whereas e-acsl-gcc.sh seems to prefer eacsl-dlmalloc), which could probably be reported as a bug at https://bts.frama-c.com

Compilation issue with sitmo prng, c++11, and armadillo

I'm trying to compile the sitmo prng under C++11 within an R package. The problematic code has been packaged and is available here. The objective of this R package is to make available the sitmo header file so that other packages are able to use the LinkTo field within description. As an added bonus, the package is scheduled to ship with an Armadillo + OpenMP example. There is one other package, mvnfast, that uses sitmo, but only under c++98 and boost headers.
I believe that the error which I am receiving is specific to OS X and clang. I haven't been able to replicate it on Windows via win-build. With that being said, the error is:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: error: non-type template argument is not a constant expression
const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
The error has only popped up on the Rcpp dev list. The resolution in this case was to compile under C++98 and use boost.
The above error is followed by the following notes:
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3773:18: note: in instantiation of function template specialization 'std::__1::generate_canonical<double, 53, sitmo::prng_engine>' requested here
* _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3737:17: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
{return (*this)(__g, __p_);}
^
sitmo_test.cpp:77:26: note: in instantiation of function template specialization 'std::__1::uniform_real_distribution<double>::operator()<sitmo::prng_engine>' requested here
double u = distunif(engine);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/random:3641:44: note: non-constexpr function 'max' cannot be used in a constant expression
const size_t __logR = __log2<uint64_t, _URNG::max() - _URNG::min() + uint64_t(1)>::value;
^
../inst/include/prng_engine.hpp:100:23: note: declared here
static result_type (max)() { return 0xFFFFFFFF; }
The version of clang being used is:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
Looking into the code, there is a bug in the sitmo prng_engine.h. min() and max() were declared as
static result_type (min)() { return 0; }
static result_type (max)() { return 0xFFFFFFFF; }
If you take a look at, say, standard LCG max from here, you could see that it is declared constexpr, ditto for min.
As soon as you make those methods constexpr in the sitmo header file, I believe you could use them in template expression.
UPDATE
I've looked into GCC 5 headers, methods indeed are declared constexpr

Files for running aes only from Polarssl

I am trying to use only aes in my program. I have copied the files
config.h
aes.h
havege.h
to the folder polarssl. But when I run the program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "polarssl/aes.h"
#include "polarssl/havege.h"
int main()
{
char buff[2][64] = {"ABCDEFGHIJKLMN", ""};
havege_state hs;
int retval;
unsigned char IV[16];
unsigned char IV2[16];
unsigned char key[32];
aes_context enc_ctx;
aes_context dec_ctx;
havege_init(&hs);
havege_random(&hs, IV, 16);
havege_random(&hs, key, 32);
strncpy(IV, IV2, 16); //copy IV
aes_setkey_enc(&enc_ctx, key, 256);
aes_setkey_dec(&dec_ctx, key, 256);
//encrypt
aes_crypt_cbc(&enc_ctx, AES_ENCRYPT, 64, IV, buff[0], buff[1]);
printf("Before encrypt:%s\n", buff[0]);
//decrypt
aes_crypt_cbc(&dec_ctx, AES_DECRYPT, 64, IV2, buff[1],buff[0]);
printf("After decrypt:%s\n", buff[0]);
return 0;
}
I am getting the error
In function `main':
ex.c:(.text+0x68): undefined reference to `havege_init'
ex.c:(.text+0x86): undefined reference to `havege_random'
ex.c:(.text+0xa4): undefined reference to `havege_random'
ex.c:(.text+0xe0): undefined reference to `aes_setkey_enc'
ex.c:(.text+0xfe): undefined reference to `aes_setkey_dec'
ex.c:(.text+0x133): undefined reference to `aes_crypt_cbc'
ex.c:(.text+0x17e): undefined reference to `aes_crypt_cbc'
collect2: error: ld returned 1 exit status
Next to the header files, you will also need the .c files! (aes.c, havege.c) and compile those in your code.
On the implementation side:
* Are you sure you want to use HAVEGE? There are lots of doubts on its effectiveness (depending on the system you run on), the standardized CTR-DRBG seems to be a much better choice..
I think your error has something to do with linking to the Aes and Havege files. Your compiler is not recognizing them!
Are they in the same folder as your main? If they are in the same folder, then remove the "polarssl/" from the headerfile name at the top.
Or maybe, when compiling be sure to include aes.c and aes.h too. I found that I was getting the same error due to this. I was only including aes.h in compiling.
Example
$terminal: gcc main.c aes.h aes.c -o encrypt
Just wondering?
If you want to use only aes, why are you trying to use havege.h?

Resources