--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/044dfdd3-2313-4ce3-a5e2-3a23c1c0d678%40isocpp.org.
`volatile` aren't supposed to be used as thread safe storage, they aren't designed to do that and any code that relies on that specific feature is broken.You should use volatile when loading or storing a variable causes side effects outside of your program. For example:- You set a pointer to volatile value to 0, so that the flow of blood from some device stops.- You read a value from a pointer to volatile to get the current temperature from a smart thermometer.Basically, whenever loading or storing to a value has side effects that the compiler doesn't know about. The compiler is this prohibited to optimize such accesses to volatile. This is a feature, not a bug.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f03a4ed6-632d-462d-849b-627b1cd7bd92%40isocpp.org.
volatile tells the compiler that the storage location might be changed by
things outside it's control.
Example:
volatile struct memory_mapped_thingie* thingieP;
for (;;) {
if (thingieP->flag) {
thingieP->control = value;
}
}
Without volatile the compiler is allowed to transform that code into
if (thingieP->flag) {
for (;;) {
thingieP->control = value;
}
} else {
for (;;)
;
}
since it knows that it never writes to thingieP->flag.
/MF
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/772c456b-2629-49c6-bdf3-a2c1877871a2%40isocpp.org.
While all of this is true volatile sees very little use compared to its rather large effects on the standard. I'm pretty sure that if we had a fresh starthandling of memory mapped IO would be relegated to a few library intrinsics.
While I don't subscribe to Hyman's philosophy regarding optimisation, he does
have a point on ensuring memory reads and writes. std::atomic is specified
with methods both volatile and non-volatile and that makes a difference.
Atomicity and volatility are not the same concept, especially for base
operations like load and store.
On Sunday, March 25, 2018 at 9:48:03 PM UTC+8, Thiago Macieira wrote:While I don't subscribe to Hyman's philosophy regarding optimisation, he does
have a point on ensuring memory reads and writes. std::atomic is specified
with methods both volatile and non-volatile and that makes a difference.
Atomicity and volatility are not the same concept, especially for base
operations like load and store.As far as I am concerned, there seems to be no guarantee in the standard that `volatile` could prevent using cache, although some compilers promise that.
IA-64 may even require it, to avoid a write-after-write collision (unless it
adds a stop bit between the two stores).
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3193822.uToWOrWuY4%40tjmaciei-mobl1.
Since this isn't volatile, even if a is a global variable, the compiler is
allowed to perform optimisations. None of them may do so right now, but they
may in the future and further drive Hyman crazy.
I probably read different article, where is any mention of compiler optimization?
BTW optimization is problem if you try be clever and work against it
I am confusing about the necessity of the keyword `volatile` in C++, as we already have the atomics, and this keyword is not able to deal with processor optimizations. Is there any meaningful use case that depends on this keyword in C++ (except for the atomic library in C)? If not, I think it shall be deprecated or removed, like the keyword `register`.I am looking forward to your comments!Mingxin Wang
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/044dfdd3-2313-4ce3-a5e2-3a23c1c0d678%40isocpp.org.
I can hardly imagine any other use case than external hardware communication such as DMA or memory mapped IO.
For the particular use of floating point precision control volatile seems very inefficient as you don't really need writing and reading main memory. Maybe an intrinsic std::preserve_precision() would be a good thing to propose?
AFAICS, C allows to perform operations with increased precision, see C11
6.3.1.8/2. I do not find a similar grant in C++ though, but I consider
it a very useful one, which allows things like FMA.
I'm struggling to see a use case where I would want reduced precision of
math operations where they could potentially be more precise for free.
There have been cases where I've needed, not "reduced" precision, but
consistent precision: computations that give binary-identical results across
implementations and even CPUs. Basically, "follow the IEEE-754 standard
exactly". You can get that with integer math, but with floats, it's a lot harder.
Your use of volatile for this purpose is certainly unusual and, I'd
wager, not the intended use case for it.
On 04/03/18 16:37, Hyman Rosen wrote:
On Tue, Apr 3, 2018 at 5:01 AM, Bengt Gustafsson <bengt.gu...@beamways.com <mailto:bengt.gustafsson@beamways.com>> wrote:
For the particular use of floating point precision control volatile
seems very inefficient as you don't really need writing and reading
main memory. Maybe an intrinsic std::preserve_precision() would be a
good thing to propose?
On the contrary, if the compiler is going to insist on doing the wrong thing
on behalf of the optimizationists, volatile gives me just the control I need
(i.e., on particular results) and does it in a standard-conforming way that's
already in the language, and in C as well, since the problem exists there also.
AFAICS, C allows to perform operations with increased precision, see C11 6.3.1.8/2. I do not find a similar grant in C++ though, but I consider it a very useful one, which allows things like FMA.
I'm struggling to see a use case where I would want reduced precision of math operations where they could potentially be more precise for free. Your use of volatile for this purpose is certainly unusual and, I'd wager, not the intended use case for it.
I think, that expectation is wrong. The language doesn't give you such guarantees.
Personally, I try to never depend on the exact result when FP is involved.
well, tough luck
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAHSYqdZ-BHgNWQSC4srXSP3oznBgnjGBBBE9cxdhr2mTmMQ%3DWA%40mail.gmail.com.
You are using the wrong tool for that purpose.
Every current compiler is able to support the full IEEE754 compliance if told so (even the greatest evil optimizationist compiler ever: icc).
If you want the compiler to generate loads and stores between floating point operations on x87, gcc has -ffloat-store which will do exactly what you want without the overhead of volatile.
So no and no, volatile is neither needed for this, nor giving you the right answer.
float std_precision(float f) { volatile float h = f; return h; }
One question, how many `volatile` your code use? IMHO it should be used only in one function:And this could be replaced by some compiler intrinsic or asm block.float std_precision(float f) { volatile float h = f; return h; }
This this will be more portable than trying rally on guaranties that nobody wants to give you.
Otherwise, you're incurring a high latency memory spill compared to register-only.
No one is asking to take this one away. What we're suggesting is that there
may be better ways to do this by eventually shifting the requirement to the
compiler.
The compiler already has this requirement. It has chosen to ignore it. According
to the standard, casting to a floating-point type or assigning to a floating-point
variable must do the downscaling. Gcc does not to do that.
float square(float x) {
return std::pow(x, 2);
} The compiler already has this requirement. It has chosen to ignore it. According
to the standard, casting to a floating-point type or assigning to a floating-point
variable must do the downscaling. Gcc does not to do that.Actually, GCC does... for non-x87 targets...
This is the source of this entire discussion, I guess?
https://gcc.gnu.org/wiki/x87note
The compiler already has this requirement. It has chosen to ignore it. According
to the standard, casting to a floating-point type or assigning to a floating-point
variable must do the downscaling. Gcc does not to do that.Actually, GCC does... for non-x87 targets...
The most frustrating and simple I have (requires C++11 or newer) is:float square(float x) {
return std::pow(x, 2);
}This is fully optimized by GCC, but the output is: converts x to double, multiply it by itself, and converts back to float.So here, in this case, GCC does respect the requirements (int cannot be promoted to float, but can be promoted to double, so the standard requires here that both get promoted to double).And if you use this inside a big chain of computation using only floats, I can assure you that the double conversion is kept.
I think your main problem is: x87 is not maintained anymore.
That's why your best bet here would be to use SSE2 when targeting x86 32bits. Doing that, you will have full IEEE754 compliance you need (and will most likely be faster). And I really doubt you still need to target machines with x87 and without SSE2.
Just to clarify, I'm not saying we should remove volatile, but just that you have
a volatile-based hack that appears to work, while there are proper solutions to
your problem (in that case).
So everything he said is basically right, but only for an old, deprecated (and buggy) architecture.
The most frustrating and simple I have (requires C++11 or newer) is:float square(float x) {
return std::pow(x, 2);
}This is fully optimized by GCC, but the output is: converts x to double, multiply it by itself, and converts back to float.So here, in this case, GCC does respect the requirements (int cannot be promoted to float, but can be promoted to double, so the standard requires here that both get promoted to double).And if you use this inside a big chain of computation using only floats, I can assure you that the double conversion is kept.
I don't understand how this applies to my problem.
float square(float x) {
return std::pow(x, 2);
}
square(float):
cvtss2sd %xmm0, %xmm0
mulsd %xmm0, %xmm0
cvtsd2ss %xmm0, %xmm0
ret
float dotprod(const float* A, int n) {
float s = 0.f;
for (int i = 0; i < n; i++) {
s += A[i];
}
return s;
}
I think your main problem is: x87 is not maintained anymore.
My main problem, as illustrated by the code above, is that g++ disobeys the
standard when doing floating-point arithmetic.
Notice how optimizationism poisons everything. Not only does the standard make
poor choices that privilege potential efficiency over code clarity and consistency, but
even when the standard makes the correct choice, that choice is ignored in order to
again privilege that potential efficiency.
You compute on the architecture you have, not on the architecture you want.
There isn't anything about the x87 architecture that precludes g++ from doing
the right thing, given that using volatile in fact does the right thing. The authors
of g++ deliberately chose to do the wrong thing for the sake of optimizationism;
they preferred to produce the incorrect, non-standard-complying result because
they could do that faster than producing the correct, standard-complying result.
What normative statement in the standard makes you believe that
"casting to a floating-point type [...] must do the downscaling"?