constexpr, cmath functions and other functions and distributions
After much head scratching (and some head banging) - against a string of "Computer Says No" inscrutable diagnostic messages. I believe that I have now produced a (partial) version of Boost.Math normal_distribution that can be a constexpr using GCC6.1.1. (diagnosis has got to improve if constexprizing existing code is to become popular? ) constexpr normal_distribution<double> n01(0., 1.); producing this code 960 .loc 1 47 0 961 000d 660FEFC0 pxor %xmm0, %xmm0 # tmp89 962 0011 F20F1145 movsd %xmm0, -32(%rbp) # tmp89, n01.m_mean 962 E0 963 0016 F20F1005 movsd .LC1(%rip), %xmm0 #, tmp90 963 80080000 964 001e F20F1145 movsd %xmm0, -24(%rbp) # tmp90, n01.m_sd 964 E8 and more interestingly, a PDF can be calculated at compile-time constexpr double p = pdf(n01, 0.5); 965 .loc 1 56 0 966 0023 F20F1005 movsd .LC2(%rip), %xmm0 #, tmp91 966 88080000 967 002b F20F1145 movsd %xmm0, -8(%rbp) # tmp91, p 967 F8 Perhaps the assembler whizzes can confirm that this looks like a compile-time computation? This relies on lots of C++14 and 17 constexpr features, and Most crucially it relies on being able to use several cmath functions as constexpr. For a couple of trivially simple examples: abs(x) and isfinite(x). constexpr double ax42 = abs(-42.); // Unspecified version of abs. constexpr double ax42g = ::abs(-42.); // C double version? constexpr double ax42b = __builtin_abs(-42.); // builtin GCC only constexpr double ax42s = std::abs(-42.); // std:: version Without most cmath functions it seems to me impossible to write any reasonable (so-called special or not) mathy function or statistical distribution that can be constexpr. At present, only GCC 6.1.1 authors have foreseen this need and provided these so-called built_in_* cmath function versions that are constexpr, see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html. (Aside on other compilers: · Clang 3.8 has some problem deep inside type_traits that I haven't yet investigated. · VS2015 update 3 still doesn't compile many essential C++17 features from my experiments so far, and doesn't provide any constexpr cmath functions. ) The many potential benefits of compiler-time computations are quite enticing to contemplate :-) * No run-time where possible * Errors caught at compile-time * Improved code readability * Reduce executable footprint and potentially most important * Avoid global initialization order issues * Avoid initialization thread races See · Scott Schurr, C++ Developer, Ripple Labs https://cppnow2015.sched.org/event/2oLK/constexpr-c-at-compile-time · Dietmar Khul Constant Fun https://www.youtube.com/watch?list=PL9hrFapz4dsM1B9bI8VmEE4JJlR0m-dvo https://www.youtube.com/watch?list=PL9hrFapz4dsM1B9bI8VmEE4JJlR0m-dvo&v=T37ElKPICvo&app=desktop &v=T37ElKPICvo&app=desktop Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard? Paul PS I can't conceive of any UDT using or providing floating-point without some function like frexp to get and set exponent and significand. I also note that making constexpr frexp and ldexp and other functions passing a pointer to a variable to be updated may be more difficult/impossible, and is very likely to require at least initialization of the to-be-updated variable before calling frexp. I've investigated overcoming this by writing a naïve new constexpr version of frexp returning a std::pair (significand and exponent) but, of course, it can never be a 'drop-in' replacement because it has a different signature, so lots of code changes are inevitable. --- Paul A. Bristow Prizet Farmhouse Kendal UK LA8 8AB +44 (0) 1539 561830
On 07/28/16 16:24, Paul A. Bristow wrote:
After much head scratching (and some head banging) - against a string of "Computer Says No" inscrutable diagnostic messages.
I believe that I have now produced a (partial) version of Boost.Math normal_distribution that can be a constexpr using GCC6.1.1.
(diagnosis has got to improve if constexprizing existing code is to become popular? )
constexpr normal_distribution<double> n01(0., 1.);
producing this code
960 .loc 1 47 0
961 000d 660FEFC0 pxor %xmm0, %xmm0 # tmp89
962 0011 F20F1145 movsd %xmm0, -32(%rbp) # tmp89, n01.m_mean
962 E0
963 0016 F20F1005 movsd .LC1(%rip), %xmm0 #, tmp90
963 80080000
964 001e F20F1145 movsd %xmm0, -24(%rbp) # tmp90, n01.m_sd
964 E8
and more interestingly, a PDF can be calculated at compile-time
constexpr double p = pdf(n01, 0.5);
965 .loc 1 56 0
966 0023 F20F1005 movsd .LC2(%rip), %xmm0 #, tmp91
966 88080000
967 002b F20F1145 movsd %xmm0, -8(%rbp) # tmp91, p
967 F8
Perhaps the assembler whizzes can confirm that this looks like a compile-time computation?
The instrunctions just transfer double values from one location to another, so if the constants are results of some computation then yes, the computation took place at compile time. However, I'm not so sure that constexpr took part in that optimization. First, I think FP math has always been out of the compile time (as far as the language is concerned). For instance, you can't put a FP operation in a static_assert. Has this changed in the latest standards? Second, gcc is known to be able to perform FP computations on constants in compile time. This is not related to constexpr but rather is an optimization. The computation simply has to be visible to the compiler (e.g. with help of the old-fashion inline functions). Other compilers may be similarly capable, but AFAIK are not required to.
2016-07-28 16:24 GMT+03:00 Paul A. Bristow
After much head scratching (and some head banging) - against a string of "Computer Says No" inscrutable diagnostic messages.
It often helps to debug the constexpr realted issue using Clang compiler. Sometimes it may provide more diagnostics than GCC (but sometimes it compiles code that GCC refuses).
I believe that I have now produced a (partial) version of Boost.Math normal_distribution that can be a constexpr using GCC6.1.1.
That's very very very good. However see the bad news down below.
Without most cmath functions it seems to me impossible to write any reasonable (so-called special or not) mathy function or statistical
distribution that can be constexpr.
<...>
Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. I've tried to add constexpr to the <cstring> in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html The committee sad 'No', so I've removed the changes to <cstring> and only then the paper passed into C++Next. There is a small chance that a paper on making some of the C math functions constexpr may be accepted if functions are trivial to implement efficiently. I can write such paper and represent in to the C++ committee, but I'll need a lot of help with: * writing a list of C functions that could be simply implemented from scratch without affecting performance (functions that usually take 1-3 lines to implement). * implementing each of those function using constexpr At least users will have the constexpr implementation in Boost.Math if the paper won't be accepted. -- Best regards, Antony Polukhin
Is there any support for making std::abs and friends constexpr where>> possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. Well, I was also thinking along these lines. I am not sure if the C++language requires constexpr versions of any mathematical functionsor if this is left as an implementation-specific artifact, or evenif it is specified as non-constexpr for C compatibility. In othercommunications, it has been mentioned that GCC providessome constexpr built-ins for certain <cmath> functions.But this might be an implementation-specific design choice,not a specification fulfillment. There is a small chance that a paper on making some of the C math> functions constexpr may be accepted if functions are trivial to> implement efficiently. Basic functions such as absoulute value, sign manipulationas well as elementary transcendental functions such as trigonometricfunctions and power functions could readily be implemented asconstexpr. In my opinion, it depends on the direction of the C++language if these should actually be required in a constexpr form.
There is also a technical detal. Another Boost author reminded
that C++ can't overload on constexpr-ness. So there might bedifficulties with naming conventions for constexpr math functions.
Questions might arise like: Do you want sqrt(x) and alsoconstexpr_sqrt(x)? Do you need a <cmath> constexpr namespace?
Best regards, Chris
On Thursday, July 28, 2016 5:44 PM, Antony Polukhin
After much head scratching (and some head banging) - against a string of "Computer Says No" inscrutable diagnostic messages.
It often helps to debug the constexpr realted issue using Clang compiler. Sometimes it may provide more diagnostics than GCC (but sometimes it compiles code that GCC refuses).
I believe that I have now produced a (partial) version of Boost.Math normal_distribution that can be a constexpr using GCC6.1.1.
That's very very very good. However see the bad news down below.
Without most cmath functions it seems to me impossible to write any reasonable (so-called special or not) mathy function or statistical
distribution that can be constexpr.
<...>
Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. I've tried to add constexpr to the <cstring> in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html The committee sad 'No', so I've removed the changes to <cstring> and only then the paper passed into C++Next. There is a small chance that a paper on making some of the C math functions constexpr may be accepted if functions are trivial to implement efficiently. I can write such paper and represent in to the C++ committee, but I'll need a lot of help with: * writing a list of C functions that could be simply implemented from scratch without affecting performance (functions that usually take 1-3 lines to implement). * implementing each of those function using constexpr At least users will have the constexpr implementation in Boost.Math if the paper won't be accepted. -- Best regards, Antony Polukhin _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
On Thu, Jul 28, 2016 at 11:44 AM, Antony Polukhin
2016-07-28 16:24 GMT+03:00 Paul A. Bristow
: Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. I've tried to add constexpr to the <cstring> in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html The committee sad 'No', so I've removed the changes to <cstring> and only then the paper passed into C++Next.
There is a small chance that a paper on making some of the C math functions constexpr may be accepted if functions are trivial to implement efficiently.
The chance of that happening is much increased if there are National Body comments to the C++17 ballot requesting constexpr C math functions. So Paul should file a comment with the BSI (Roger Orr, IIRC), and anyone else who cares should file a comment with their National Body.
I can write such paper and represent in to the C++ committee, but I'll need a lot of help with: * writing a list of C functions that could be simply implemented from scratch without affecting performance (functions that usually take 1-3 lines to implement). * implementing each of those function using constexpr
Having such a paper also would increase the chance that the C++ committee will act.
At least users will have the constexpr implementation in Boost.Math if the paper won't be accepted.
Again, the fact that Boost.Math has implemented such constexpr functions increases the chance that the committee will add them to the standard. Thanks, --Beman
-----Original Message----- From: Boost [mailto:boost-bounces@lists.boost.org] On Behalf Of Beman Dawes Sent: 29 July 2016 14:07 To: Boost Developers List Subject: Re: [boost] constexpr, cmath functions and other functions and distributions
On Thu, Jul 28, 2016 at 11:44 AM, Antony Polukhin
wrote: 2016-07-28 16:24 GMT+03:00 Paul A. Bristow
: Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. I've tried to add constexpr to the <cstring> in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html The committee sad 'No', so I've removed the changes to <cstring> and only then the paper passed into C++Next.
There is a small chance that a paper on making some of the C math functions constexpr may be accepted if functions are trivial to implement efficiently.
The chance of that happening is much increased if there are National Body comments to the C++17 ballot requesting constexpr C math functions.
So Paul should file a comment with the BSI (Roger Orr, IIRC), and anyone else who cares should file a comment with their National Body.
I have made some remarks on this already (though there were some dark mutterings about undefined behaviour).
I can write such paper and represent in to the C++ committee, but I'll need a lot of help with: * writing a list of C functions that could be simply implemented from scratch without affecting performance (functions that usually take 1-3 lines to implement). * implementing each of those function using constexpr
Having such a paper also would increase the chance that the C++ committee will act.
At least users will have the constexpr implementation in Boost.Math if the paper won't be accepted.
Again, the fact that Boost.Math has implemented such constexpr functions increases the chance that the committee will add them to the standard.
I don't want to overplay this - I've only done one function of one distribution as a 'proof of concept'. It was tiresome and the prospect of doing all the functions and all the distributions is daunting. And making of code that will work for constexpr-savvy and older compilers that are not isn't appealing either. I also ran into the sand trying to do the erf function (needed for normal CDF). Ironically it was in the complexity of extra code ensuring that the order of initialisation is correct, one of the potentially important features of constexpr, if I understand correctly. I also worry about creating three sets of cmath functions, some constexpr and some not yet, and others that are impossible like frexp because they 'return' by assigning via a pointer. This is going to be confusing? GCC seems well on the way with its so-called __builtin_* versions. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html I am also sensitive to the feeling that this is not a top priority for C++ standardization compared to other new features, not just in constexpr. But making cmath functions constexpr surely should be a long-term ambition, especially for embedded IOT and phones. HTH Paul --- Paul A. Bristow Prizet Farmhouse Kendal UK LA8 8AB +44 (0) 1539 561830
-----Original Message----- From: Boost [mailto:boost-bounces@lists.boost.org] On Behalf Of Beman Dawes Sent: 29 July 2016 14:07 To: Boost Developers List Subject: Re: [boost] constexpr, cmath functions and other functions and distributions
On Thu, Jul 28, 2016 at 11:44 AM, Antony Polukhin
wrote: 2016-07-28 16:24 GMT+03:00 Paul A. Bristow
: Is there any support for making std::abs and friends constexpr where possible in future versions of the C++ Standard?
Here are the bad news: C++ committee wishes to keep it possible for the implementors of the Standard Library to reuse C headers. I've tried to add constexpr to the <cstring> in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html The committee sad 'No', so I've removed the changes to <cstring> and only then the paper passed into C++Next.
There is a small chance that a paper on making some of the C math functions constexpr may be accepted if functions are trivial to implement efficiently.
The chance of that happening is much increased if there are National Body comments to the C++17 ballot requesting constexpr C math functions.
So Paul should file a comment with the BSI (Roger Orr, IIRC), and anyone else who cares should file a comment with their National Body.
I have made some remarks on this already (though there were some dark mutterings about undefined behaviour).
I can write such paper and represent in to the C++ committee, but I'll need a lot of help with: * writing a list of C functions that could be simply implemented from scratch without affecting performance (functions that usually take 1-3 lines to implement). * implementing each of those function using constexpr
Having such a paper also would increase the chance that the C++ committee will act.
At least users will have the constexpr implementation in Boost.Math if the paper won't be accepted.
Again, the fact that Boost.Math has implemented such constexpr functions increases the chance that the committee will add them to the standard.
I don't want to overplay this - I've only done one function of one distribution as a 'proof of concept'. It was tiresome and the prospect of doing all the functions and all the distributions is daunting. And making of code that will work for constexpr-savvy and older compilers that are not isn't appealing either. I also ran into the sand trying to do the erf function (needed for normal CDF). Ironically it was in the complexity of extra code ensuring that the order of initialisation is correct, one of the potentially important features of constexpr, if I understand correctly. I also worry about creating three sets of cmath functions, some constexpr and some not yet, and others that are impossible like frexp because they 'return' by assigning via a pointer. This is going to be confusing? GCC seems well on the way with its so-called __builtin_* versions. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html I am also sensitive to the feeling that this is not a top priority for C++ standardization compared to other new features, not just in constexpr. But making cmath functions constexpr surely should be a long-term ambition, especially for embedded IOT and phones. HTH Paul --- Paul A. Bristow Prizet Farmhouse Kendal UK LA8 8AB +44 (0) 1539 561830
participants (5)
-
Andrey Semashev
-
Antony Polukhin
-
Beman Dawes
-
Christopher Kormanyos
-
Paul A. Bristow