Visual Studio 2010 and SSE 4.2 - c++

I would like to know, what is necessary to set in Visual Studio 2010, to have SSE 4.2 enabled? I would like to use it because of optimized POPCNT...
How can I test, if all settings are ok?
thanks
well, I tried to use your solution, however <nmmintric.h> is not included in vstudio2010 and standard __popcnt requires int instead of std::bitset<> :(
any idea?
Thx for the hint with the correct header. However, it seems that: error C3861: '_mm_popcnt_u64': identifier not found, I found only _mm_popcnt_u32, however I don't know, how to use it with bitset, or should I use just bitset<>.count? It can't work without anz settings of compiler, can it?
nobody knows ?

you have to write _mm_popcnt_u64 in your code. Also better check it the cpu you run on supports the instruction.
And build for x64.
#include <stdio.h>
#include <nmmintrin.h>
int main ()
{
unsigned __int64 a = 0x123456789ABCDEF0;
int res = _mm_popcnt_u64(a);
printf_s("Result res should be 32: %d\n", res);
return 0;
}

MSDN example for __popcnt:
http://msdn.microsoft.com/en-us/library/bb385231.aspx

There is nothing special required for this to work.
You may use intrinsics mm* and include the appropriate header file, and it will compile if your system support the given features.
The compiler does not inspect or touch inline assembly, so whatever you put in there will go through the build, although your application will crash if your system does not support an instruction.
Other than that, the VS2010 optimizer only targets SSE2.

Related

Warnings not being generated in VS2017

It seems to me that VS2017 fails to catch some fairly obvious compiler warnings which older versions do not. This seems so fundamental to me that I'm guessing the problem has to be something I'm missing (e.g. some new default compiler setting, perhaps?). Has anyone else seen this?
To test this, I create a simple console application in both 2013 and 2017. The only change I made the to the project settings was to set the compiler warning level to 4 and to report warnings as errors. Below is the entirety of the source
In VS2013 this fails. In 2017, it builds just fine...
// TestWarning4127.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main()
{
if (true) // *** SHOULD generate warning 4127
std::cout << "Warning";
return 0;
}
Am I missing something obvious here...?
When used inside an if or while conditions the trivial constants such as 1 or true do not generate warnings in VS 2017 as described in the Compiler Warning (level4) official documentation. Excerpt from the official documentation:
The controlling expression of an if statement or while loop evaluates
to a constant. Because of their common idiomatic usage, trivial
constants such as 1 or true do not trigger the warning, unless they
are the result of an operation in an expression.
That being said it's not a warning in VS 2013 with the default warning levels of W3 either. Only with the elevated W4 settings does the VS 2013 report a warning. It was removed in VS 2017 altogether.
For comparison, the GCC does not generate the warning either:
Live example on Coliru.

My Visual studio 2013 compiler for C++ is not working correctly [duplicate]

The "not", "and", etc... are keywords in C++ (macros in C). Is there any way to "enable" them in Visual Studio 2013? I'm able to use the words as macroses with iso646.h included. But VS seems not be able to recognize them as keywords.
Using /Za seems to enable them without including iso646.h, see it live, the following program produces an error without using /Za but works fine otherwise:
int main()
{
int x = 1, y = 0 ;
if (x and y)
{
//...
}
return 0;
}
As ta.speot.is indicates /Za disables extensions, the following documentation indicates you must include ios646.h otherwise:
Under /Ze, you have to include iso646.h if you want to use text forms of the following operators:
and it lists the alternative tokens below.
Note, I knew I saw this before, I include a link to a bug report for this in my answer to a similar question. Although this does not include the workaround noted above.
Note 2: Cheers and hth. - Alf indicates that there may be many undesirable consequences to turning off extension and therefore you may be better off just including iso646.h.
Use a forced include (option /FI) of the iso646.h header.
For work in the command interpreter you can put this in your CL environment variable, or e.g. in a response file.
Turning off Visual C++ language extensions with /Za can also do the trick, at the cost of rendering the compiler pretty much useless – since Microsoft code such as the Windows API headers generally uses the extensions.
On a more recent version of VS (tested on Version 15.7.0 Preview 3.0); ensure that conformance mode is set for visual studio:
It then compiles with the alternative operator representations.

How to make sure code (c++) written in Xcode can compile on other platforms?

I am a beginner was trying to do some C++ programming on Xcode. It works fine, but when I try to compile the same c++ file on my windows pc using VS, there were some errors. After I look at my code closely, there are really some stupid mistakes that I have made which caused the errors, but Xcode seemed to have ignored them...
My question is that is there any setting that I need to change to prevent Xcode from being so smart?
For example, the following code can actually compile in xcode:
#include <iostream>
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
As far as I can see there is nothing wrong with your code.
The ISO C++ standard does not specify which standard headers are included by other standard headers. So, it is entirely possible that the version of iostream used by Xcode directly or indirectly includes ciso646. Whereas Visual Studio's version of iostream does not include ciso646. There are many similar cases with other headers. You just need to read the error messages and realize that your error (when you move your file to a different platform) is due to a missing header file.
It would be nice if writing portable code meant writing code in accordance with the C++ standard specification, but unfortunately that's not the case. Although there are various compiler options on various implementations which can help bring different implementations closer together, in general you will just have to bring the code into the target environment and actually test it there.
So ultimately writing portable code means you'll have to learn some subset of C++ that is accepted by all the implementations you want to target.
or is an 'alternative token' in C++, and VS is incorrect to reject it. There's no option in Xcode to disable support for alternative tokens. However VS has non-standard support for or as a macro using the header <ciso646>, and Xcode does have a header <ciso646> which does nothing (as the standard specifies). So you can write code which uses or and which works in both Xcode and VS by including this header.
#include <iostream>
#include <ciso646> // does nothing in Xcode, allows `or` in VS
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
Unfortunately VS can't support all of the alternative tokens through macros and so Xcode will still support some that VS doesn't.
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
If you give specific examples then I can provide additional advice on how to write portable code.
Rather than changing your Xcode settings, I suggest cross-checking your code using another development environment.
If you're looking for something cheap and full-proof. Download a VirtualBox Windows VM, and run download Dev C++ (bloodhshed)
VS does not support or: you need to use || instead.
You can include some special files but it doesn't inject or sufficiently well into the language for it to work in all instances.
If you want to suppress use of or (and your compiler supports no better way)
#define it to something that emits a compiler error, for example
#define or OR
This at least means that the nature of the compilation errors will be identical on Xcode and VC.

Alternative tokens (not, and, etc...) in VisualStudio 2013

The "not", "and", etc... are keywords in C++ (macros in C). Is there any way to "enable" them in Visual Studio 2013? I'm able to use the words as macroses with iso646.h included. But VS seems not be able to recognize them as keywords.
Using /Za seems to enable them without including iso646.h, see it live, the following program produces an error without using /Za but works fine otherwise:
int main()
{
int x = 1, y = 0 ;
if (x and y)
{
//...
}
return 0;
}
As ta.speot.is indicates /Za disables extensions, the following documentation indicates you must include ios646.h otherwise:
Under /Ze, you have to include iso646.h if you want to use text forms of the following operators:
and it lists the alternative tokens below.
Note, I knew I saw this before, I include a link to a bug report for this in my answer to a similar question. Although this does not include the workaround noted above.
Note 2: Cheers and hth. - Alf indicates that there may be many undesirable consequences to turning off extension and therefore you may be better off just including iso646.h.
Use a forced include (option /FI) of the iso646.h header.
For work in the command interpreter you can put this in your CL environment variable, or e.g. in a response file.
Turning off Visual C++ language extensions with /Za can also do the trick, at the cost of rendering the compiler pretty much useless – since Microsoft code such as the Windows API headers generally uses the extensions.
On a more recent version of VS (tested on Version 15.7.0 Preview 3.0); ensure that conformance mode is set for visual studio:
It then compiles with the alternative operator representations.

How to Make Visual Studio C++ 2010 Compilation Behave Like gcc/g++? (or vice-versa)

Say you've got the following simple main.cpp file:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const string FILENAME = "foo.txt";
ifstream somefile(FILENAME);
populations.close();
return 0;
}
This compiles fine via Visual Studio C++ 2010.
However, on a Linux-based system, if I execute make main and compile, we get an expected error since we didn't call c_str() on the string constant, like so:
ifstream somefile(FILENAME.c_str());
As is commonly known, and described in this SO thread.
How can I get VS to behave like gcc/g++ and raise a compilation error for the code above? Or, how can I get gcc/g++ to behave like VS and compile the above without error? (Is it a simple matter of upgrading my gnu compiler?)
(I don't believe disabling compiler extensions is a solution, as I've done this and it still compiles without error.)
Visual Studio behaves correctly in this case with respect to the C++11 standard (it works on g++ now, too). I'm not sure why would you want to do this, but you'll probably need to edit MSVC's headers (not advisable and rather drastic).
Strange thing is though, that they don't write it in their documentation. Can you check which constructor is actually being called?
It is available as part of the newer c++ standard.
To disable, add
#define _HAS_CPP0X 0
at the top before your includes.