lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lightning] How to return a floating point value from a function ?


From: Domingo Alvarez Duarte
Subject: [Lightning] How to return a floating point value from a function ?
Date: Fri, 4 Apr 2014 21:12:02 +0100

Hello !
I was looking on the docs and web but did not found an example explanation of how to return floating points from lightning generated functions, so I'm asking here to see if someone can help me, also this can be added to the documentation to make it easy for others.

Here is a example that I'm trying to create with lightning, the integer functions works as expected but the floating pointing ones doesn't.

Thanks in advance for any help !

-----
#include <stdio.h>
#include <lightning.h>

//static jit_state_t *_jit;

int inative_mult(int a, int b) {
  return a * b;
}

int inative_div(int a, int b) {
  return a / b;
}

typedef int (*pifii)(int, int);    /* Pointer to Int Function of Int Int */

double dnative_mult(double a, double b) {
  return a * b;
}

double dnative_div(double a, double b) {
  return a / b;
}

typedef double (*pdfdd)(double, double);    /* Pointer to double Function of double double */

int main(int argc, char *argv[])
{
  jit_state_t *_jit;
  pifii          imyMult, imyDiv;             /* ptr to generated code */
  jit_node_t    *startIMult, *startIDiv;           /* a couple of labels */

  pdfdd          dmyMult, dmyDiv;             /* ptr to generated code */
  jit_node_t    *startDMult, *startDDiv;           /* a couple of labels */

  jit_node_t    *inA, *inB;                    /* to get the argument */

  init_jit(argv[0]);
  _jit = jit_new_state();

  startIMult = jit_note(__FILE__, __LINE__);
  jit_prolog();
  inA = jit_arg();
  inB = jit_arg();
  jit_getarg(JIT_V0, inA);
  jit_getarg(JIT_V1, inB);
  jit_mulr(JIT_R0, JIT_V0, JIT_V1);
  jit_ret();
  jit_epilog();

  startIDiv = jit_note(__FILE__, __LINE__);
  jit_prolog();
  inA = jit_arg();
  inB = jit_arg();
  jit_getarg(JIT_V0, inA);
  jit_getarg(JIT_V1, inB);
  jit_divr(JIT_R0, JIT_V0, JIT_V1);
  jit_ret();
  jit_epilog();

  startDMult = jit_note(__FILE__, __LINE__);
  jit_prolog();
  inA = jit_arg_f();
  inB = jit_arg_f();
  jit_getarg_f(JIT_F0, inA);
  jit_getarg_f(JIT_F1, inB);
  jit_mulr_f(JIT_FA0, JIT_F0, JIT_F1);
  jit_retr_f(JIT_FA0);
  jit_epilog();

  startDDiv = jit_note(__FILE__, __LINE__);
  jit_prolog();
  inA = jit_arg_f();
  inB = jit_arg_f();
  jit_getarg_f(JIT_F0, inA);
  jit_getarg_f(JIT_F1, inB);
  jit_divr_f(JIT_FA0, JIT_F0, JIT_F1);
  jit_retr_f(JIT_FA0);
  jit_epilog();

  jit_emit();

  imyMult = (pifii)jit_address(startIMult);
  imyDiv = (pifii)jit_address(startIDiv);

  dmyMult = (pdfdd)jit_address(startDMult);
  dmyDiv = (pdfdd)jit_address(startDDiv);

  jit_clear_state();

  /* call the generated code, passing its size as argument */
  printf("jit_native_imult(%d, %d) = %d\n", 12, 27, imyMult(12, 27));
  printf("jit_native_idiv(%d, %d) = %d\n", 27, 12, imyDiv(27, 12));

  printf("jit_native_dmult(%f, %f) = %f\n", 12.3, 27.5, dmyMult(12.3, 27.5));
  printf("jit_native_ddiv(%f, %f) = %f\n", 27.5, 12.3, dmyDiv(27.5, 12.3));

  jit_disassemble();

  jit_destroy_state();
  finish_jit();
  return 0;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]