C :: Possible To Make Compiler To Generate Smarter Code
Mar 6, 2015
I am not a C programmer (or a programmer at all). I am just a hobbyist who writes random code (i.e.: ignorant).C is not my area, and I my experience with it is quite LIMITED!Today, I found out that if I need to get "square root" in C I will be in trouble if I depend solely on the compiler..Is it possible to make the compiler to generate smarter code for this (quite trivial code):
Code:
void intprime_c(unsigned long* fprimes, unsigned long count)
{
unsigned long divc = 3;
unsigned long num = 2;
unsigned long sqr;
unsigned long pfound = 0;
}
[code]...
By "without ugly hacks" I mean using already existing code that came with GCC. Because the others did not need ugly hacks to perform better (it is all about being fair).To compute the "square root" in x86 one just need 3 instructions:
fild
fsqrt
fistp
But the compiler is generating a call to a "fsqrt" function and wasting a lot of time by doing this. If not, then why is it taking so long? Is it something wrong in my code (at least the output was verified to be the correct)?
Time taken to get the first 2500000 primes (x86_64):
Assembly = 72.400s (human written, unoptimized, coder level: newbie (i.e.: ignorant))
pascal = 75.200s (written by the same person)
C (fsqrt) = 83.600s (written by the same person)
C (sqrt) = 85.900s (written by the same person)
If it matters, I compiled the C code with this command: gcc -O2 -fno-omit-frame-pointer -fPIC -c ./prime.
This: "The compiler may not always be able to insert the code for a function inline (such as with recursive functions or functions for which you have obtained an address), but generally, it will work."
For a function inline why wont it work for a recursive function or a function for which you have obtained an address?
I have an application thats has been coded in C#. After build it generates a standalone dll file and the exe file.
I want to use this code in Matlab. I found out from this page that I can load the C# classes or codes into Matlab.
dll_in_matlab = NET.addAssembly('location to the dll file)
I have the code and the application buty unfortunately I do not understand C# at all. So after using this command I was able to see a .NET object in my workspace and this is what I got for its properties and its classes:
evmaps = NET.Assembly handle Package: NET Properties for class NET.Assembly: AssemblyHandle Classes Structures Enums GenericTypes
[code]....
This is the application it produces: [URL] and here is the virustotal result for my application just to ensure that its clean. [URL] .....
i wanna no if i can make the program generate a different number everytime you guess the number right and want to play again. it always generates the same number
Theres a class named "A" which has got a static function named "sfA".Now I instance an object of class A and call a method from A called "fA".
The method fA calls sfA. And now the issue is: i need the value of a member from the object which called fA respectivly sfA, inside sfA.Is there a smarter way to get the value of the member as to declare an new parameter for the sfA? sfA has to be static.
I am programming with the Code::Blocks IDE and using the GNU GCC compiler. When I create an simple console application that uses strings it kind of glitches out, but here's the code:
#include <iostream> #include <cstdlib> #include <string> using namespace std; int main() ered: " << x;
[code].....
What the output is:
Please enter a string of text: Hello World You entered: Hello Process returned 0 (0x0) execution time : 4.735 s Press any key to continue.
Anyway I don't know why it removes what I typed after the space I put in between Hello and World.
I have an issue. VS 2013 isn't recognizing objects that I've declared when I use class functions.I'm getting this error: "Line 14 and 15: Error C2228: left of '.asciiToFpc6' must have class/struct/union"...Here's the relevant code:
Additionally VS apparently doesn't like my bitwise operators in my class functions and doesn't think they're doing anything. It gives "warning C4552: ['|', '<<', '>>', '&'] : operator has no effect; expected operator with side-effect" for all of them, but it seems to me the code should work fine and actually accomplish things....
Code: activity = new Idle(this, NULL); class Idle : public Activity { private: float mTimeInIdle; public: Idle() : mTimeInIdle(0) { } Idle(Objects *actor, Goods *target) : Activity(actor, target) { }
Error 1 error C2514: 'Idle' : class has no constructors d:jackydocumentsvisual studio 2010projectsperfectsimperfectsimperfectsimObjectsObjects.h 43 1 PerfectSim
The activity = new Idle(this, NULL) line is located inside the Objects::Objects(...) constructor.
Would it be caused by some cyclic dependencies? How do I go about resolving it?
I'm working on this homework assignment where the program takes in the user's height in inches, weight in pounds, and age, then calculates their hat size, jacket size and waist size. The formulas for these are as follows:
Hat: (weight/height) x 2.9
Jacket: (height x weight)/288 then adjusted by adding 1/8 an inch for every 10 years over the age of 30 (The adjustment only takes place after a full 10 years, so there is no adjustment for 30-39, but there is for 40)
Waist: (weight/5.7) then adjusted by adding 1/10 of an inch for each 2 years over the age of 28 (the adjustment only takes place after a full 2 years, so no adjustment for 29, but there is for 30, etc)
I'm supposed to utilize functions for each of the three formulas.
There's a couple things I can't figure out.
1. Why won't the compiler recognize 2.9 and 5.7 as numbers?
2. How do I adjust the calculation for the jacket and waist based on age?
Here's what I've got so far:
#include <cstdlib> #include <iostream> using namespace std; double hatSize(int weight, int height); double jacketSize(int weight, int height, int age); double waistSize(int weight, int height, int age);
Fresh storage string not released before return A memory leak has been detected. Storage allocated locally is not released before the last reference to it is lost. (Use -mustfreefresh to inhibit warning) string_read.c:6:7: Fresh storage string created
Is there a way to check if a compiler has c++11 enabled?
I have a library and it has converters between std strings and the internal string type. I current have preprocessor surrounding the converters for u8string, u16string, and u32strings, but it requires the end user flip the switch manually. It would be nice if I could know at compile time without being told whether or not those types exist.
As we know in the compilation stage, the compiler will instantiate a concrete type for a template, for example:
template<class T> void test(T m ) { cout << m << endl; } int main() { int kl = 0; test<int>(kl); }
In the main function, the compiler will try to have a int entry. Also we know that in the compilation stage, generally we have several steps: lexical analysis, syntax analysis, grammer analysis and intermediate language(IR).
So my question is: In which step is the instantiation done? Before IR or after IR?
Working on a basic class program and I'm generating two compiler errors that I'm not sure how to fix. Header file, implementation cpp and main cpp are shown below. The specific errors are shown after the code.
The file generates the second error, C2228, at lines 37-41 and 43. Basically where I tried using the second created object. Error message is "left of '.setFirstName' must have class/struct/union"