Hi Damian -

On 5/19/15, 7:50 PM, "Damian McGuckin" <[email protected]> wrote:

>> (Brad was saying)
>>* Generally, my answer for doing C-style unions or casts would be to do
>>   that in a C (static inline?) function or macro and call it from
>>Chapel.
>>   I.e., leave the gross stuff in C rather than Chapel.
>
>Any notes or examples on this? I went fishing but could not find anything
>but my choice of keywords to 'grep' might have been sub-optimal.
>



Have a look at
 README.extern
which you can read here or find in doc/{release,}/technotes

https://github.com/chapel-lang/chapel/blob/master/doc/release/technotes/REA
DME.extern

You generally have two options:
 1) use extern block support with an LLVM-enabled Chapel compiler.
    This lets you write C within a Chapel file, and the Chapel
    code below will be able to call the C functions.

    Try filling this in (I've verified it works for me,
    even though it doesn't do the floating point part):

    extern {
      // this is C code
      #include <inttypes.h>
     
      // unpack a double, returning the integer representing
      // the mantissa
      static inline
      uint64_t unpack_double_to_ints(double x, int64_t* exponent) {
        // TODO: fill this in
        *exponent = 1;
         return 2;
      }
    }

    // create Chapel-friendly versions of C functions
    proc unpackRealToInts(x: real, ref exponent:int):uint {
      // Call the C function
      return unpack_double_to_ints(x, c_ptrTo(exponent));
    }

    var x = 4.125;
    var exponent:int;

    var mantissa = unpackRealToInts(x, exponent);

    writeln("Unpacked ", x, " to mantissa ", mantissa, " exponent ",
exponent);


2) Use extern procs, which are described in README.extern. In that

   case, you'd put the C code in a .c file, put the relevant functions
   in a .h file, and include both the .h and .c files on your
   chpl compile command. You also would need to write Chapel
   prototypes for the C functions you wish to use, e.g.

   extern proc unpack_double_to_int(x:real, ref exponent:int):uint;

I prefer option 1 because it's slicker, but that does require you
to have an LLVM-enabled compiler.


Cheers,

-michael


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users

Reply via email to