I have a pointer to a function that I need to invoke without going through llvm::Module::getOrInsertFunction. This example does not work:
static int add(int x, int y);
llvm::Value *one, *two;
llvm::Constant* addfn
= llvm::ConstantInt::get(JB->getIntPtrTy(DataLayout), (intptr_t)add);
llvm::Type* args[] = { Int32Ty, Int32Ty };
llvm::FunctionType* ftype = llvm::FunctionType::get(Int32Ty, args);
addfn = llvm::ConstantExpr::getPointerCast(addfn, ftype);
CreateCall(addfn, one, two);
Is there a way? Or must I call getOrInsertFunction?
Looks like this works:
static int add(int x, int y) { return x + y; }
llvm::Constant* addfn
= llvm::ConstantInt::get(Builder->getIntPtrTy(DataLayout), (intptr_t)add);
llvm::FunctionType* ftype = 0;
{
llvm::Type* args[] = { Int32Ty, Int32Ty };
ftype = ftype->get(Int32Ty, args);
}
llvm::Constant* pp
= llvm::ConstantExpr::getIntToPtr(addfn, llvm::PointerType::getUnqual(ftype));
addfn = llvm::ConstantExpr::getPointerCast(pp, ftype->getPointerTo());
jit_value_t sum = JB->CreateCall2(addfn, Value1, Value2);
Related
I'm wrapping GSL's ODE functions is a class I'm defining. When written with no class involved, the functions are working perfectly. However, when using the class I get an error after the second gsl_odeiv2_evolve_apply_fixed_step, getting the error gsl: evolve.c:317: ERROR: step dimension must match evolution size. I'm confused why the first step should work but the second shouldn't. In printing out the dimension of the step and evolve functions I get the same thing. Here's what I have for my class and what I'm calling inside the main() function.
class DynSys {
public:
DynSys(const size_t size, double startTime, double endTime, double stepSize,
double* iState,
int (*func)(double, const double*, double*, void*),
int (*jac)(double, const double*, double*, double*, void*),
const gsl_odeiv2_step_type* T = gsl_odeiv2_step_rk8pd)
: T(T), size(size), t(startTime), t1(endTime), h(stepSize) {
y = new double[size];
y = iState;
yPrev = new double[size];
s = gsl_odeiv2_step_alloc(T, size);
c = gsl_odeiv2_control_y_new(1e-6, 0.0);
e = gsl_odeiv2_evolve_alloc(size);
sys = { func, jac, size, 0 };
}
~DynSys() {
delete [] y;
delete [] yPrev;
gsl_odeiv2_evolve_free(e);
gsl_odeiv2_control_free(c);
gsl_odeiv2_step_free(s);
}
void step() {
printf("e dim: %ld\n", e->dimension);
printf("s dim: %ld\n", s->dimension);
printf("y: %.5f %.5f %.5f %.5f %.5f %.5f\n", y[0], y[1],
y[2], y[3], y[4], y[5]);
tPrev = t;
yPrev = std::copy(y, y+size, yPrev);
int status = gsl_odeiv2_evolve_apply_fixed_step(e, c, s, &sys,
&t, h, y);
if (status != GSL_SUCCESS) {
printf("Error: %s\n", gsl_strerror(status));
throw std::logic_error(gsl_strerror(status));
}
}
double getT() {
return t;
}
void setY(double* y) {
y = y;
}
private:
const gsl_odeiv2_step_type* T;
gsl_odeiv2_step* s;
gsl_odeiv2_control* c;
gsl_odeiv2_evolve* e;
gsl_odeiv2_system sys;
const size_t size;
double t;
double t1;
double h;
double* y;
double tPrev;
double* yPrev;
};
and then
int main() {
double state[] = { 1.0, 0.0, 0.0, 0.796975, 0.11637, 0.0185312};
const size_t size = 6;
DynSys system(size, 0.0, 40.0, 1e-3, state, func, jac);
system.step();
printf("t: %.5f\n", system.getT());
system.step();
printf("t: %.5f\n", system.getT());
return 0;
}
edit: Here's a link to the GSL functions GSL ODE
I'm practicing c++ and I got stuck on the following codes trying to optimize them. I'd like to know if there is something I can do to optimize their method's implementation. Because the methods are the same except for the consts. Thanks in advance.
dominios.h
class HP {
private:
int valor;
static const int LIMITE_INFERIOR = 0;
static const int LIMITE_SUPERIOR = 1000;
public:
void setValor(int);
int getValor() {
return valor;
}
};
class MP {
private:
int valor;
static const int LIMITE_INFERIOR = 0;
static const int LIMITE_SUPERIOR = 500;
public:
void setValor(int);
int getValor() {
return valor;
}
};
dominios.cpp
void HP::setValor(int valor) {
if (valor < LIMITE_INFERIOR) this->valor = LIMITE_INFERIOR;
else if (valor > LIMITE_SUPERIOR) this->valor = LIMITE_SUPERIOR;
else this->valor = valor;
}
void MP::setValor(int valor) {
if (valor < LIMITE_INFERIOR) this->valor = LIMITE_INFERIOR;
else if (valor > LIMITE_SUPERIOR) this->valor = LIMITE_SUPERIOR;
else this->valor = valor;
}
As you can see the setValor of both classes are the same. I tried to do hierarchy using a "template" but that didn't work for me because of the consts.
this->valor = std::clamp(valor, LIMITE_INFERIOR, LIMITE_SUPERIOR);
template <typename Tag, int lo, int hi>
class Metric {
private:
int valor;
public:
void setValor(int v) { valor = std::clamp(v, lo, hi); }
int getValor() { return valor; }
};
struct HPTag;
using HP = Metric<HPTag, 0, 1000>;
struct MPTag;
using MP = Metric<MPTag, 0, 500>;
How can I pass args to this function:
int myClass::myFunc(void * aArgs){
return 0;
}
I call it this way:
thrd_create(&t, myClass::myFunc, (void*)0);
I need to pass multiple arguments to the function, how can I achieve it?
There are plenty of ways. For example:
struct Arg_Struct
{
int m_nArg1;
float m_fArg2;
bool m_bArg3;
}
//...
Arg_Struct* pStruct = new Arg_Struct;
pStruct->m_nArg1 = 0;
thrd_create(&t, myClass::myFunc, (void*)pStruct);
//...
int myClass::myFunc(void * aArgs){
Arg_Struct* pArgs = (Arg_Struct*)aArgs;
int n = pArgs->m_nArg1;
//...
return 0;
}
I'm passing a pointer to a function. I'd like to assign a new address to the passed pointer inside the function, and I'd like that address to be used after the function returns. I'm not sure if this is possible, but I'd like to do:
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
foo(intPtr, j);
// I want intPtr to point to j, which contains 200 after returning from foo.
}
void foo( int * fooPtr, int & newInt )
{
int * newIntPtr = &newInt;
fooPtr = newIntPtr;
}
Is this possible, or will intPtr not maintain the new assignment after returning from foo? Could this work (if it doesn't: why)?
Pass a reference to the pointer:
void foo( int *& fooPtr, int & newInt )
The reason why your method does not work is that you're passing the pointer by-value. Passing by-value creates a temporary within the function, so as soon as the function returns, any changes to the temporary go away.
It is no different than this:
void foo(int x)
{
x = 10;
}
int main()
{
int a = 0;
foo( a );
// a is still 0, not 10
}
The a is passed by value, so the foo() function changes the parameter to 10 within the function. However, you will see that a in main does not change to 10 after the function returns.
To change a, you need to pass the int by reference:
void foo(int& x)
{
x = 10;
}
int main()
{
int a = 0;
foo( a );
// a is now 10
}
Pass a pointer of the pointer and assign to it
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
foo( &intPtr, j );
// I want intPtr to point to j, which contains 200 after returning from foo.
}
void foo( int ** fooPtr, int & newInt )
{
int * newIntPtr = newInt;
*fooPtr = newIntPtr;
}
If you programing in pure C you can do like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void foo(int **, int *);
int main()
{
int i = 100, j = 200;
int * intPtr = &i;
int *intPtr2=&j;
foo( &intPtr, intPtr2 );
// I want intPtr to point to j, which contains 200 after returning from foo.
printf("%d",*intPtr);
}
void foo( int ** fooPtr, int * newInt )
{
int * newIntPtr = newInt;
*fooPtr = newIntPtr;
}
I would like to know, how to loop through a array of int to get its value and set its value. I know how to use the for loop to to get instantly, but I am not sure how it works, when I am using in user created objects and esp using the get set method.
I am totally new to this and have very little guidance from my lectures. I hope you guys can assist to help me. This up to where I have done.
//point.h
class point {
private:
int x[4];
public:
int getx();
void setx();
};
//point.cpp
class point {
point::getx(){
// ??????
}
point::setx(){
// ???????
}
//main.cpp
int main(){
point objPoint;
objPoint.setx(/* ???? */);
???? = objPoint.getx();
}
First of all, your getx method should return int*, not just int, and your setx should receive const int* as parameter. Second, in your point.cpp file you shouldn't redeclare class point.
int* point::getx() { //version with copying
int* ans = new int[4];
for (int i = 0; i < 4; i++) {
ans[i] = x[i];
}
return ans;
}
void point::setx(const int* y) {
for (int i = 0; i < 4; i++) {
x[i] = y[i];
}
}
Then you can use them like this
int y[4] = {1, 2, 3, 4};
int* z;
objPoint.setx(y);
z = objPoint.getx();
Just don't forget to delete[] z when you're done.
If I'm understanding you correctly, you probably want something like more this:
point.h:
class Point{
private:
int x, y;
public:
int getx();
int gety();
void setx(int value);
void sety(int value);
};
point.cpp
int Point::getx() { return x; }
int Point::gety() { return y; }
void Point::setx(int value) { x = value; }
void Point::sety(int value) { x = value; }
main.cpp
int main(int argc, char *argv[])
{
Point objPoint;
objPoint.setx(1);
int x = objPoint.getx();
cout << "x=" << x << endl;
return 0
}
Even better, you might wish to define a constructor like Point (int xvalue, int yvalue).
IMHO ...