1 
2 /*
3 
4   Example C routine for how to use callext() in PDL
5   - return log x to base y vector.
6 
7   E.g. on Solaris this would be compiled:
8 
9   cc -o callext.so -G -Kpic callext.c
10 
11   to generate dynamically loadable code. For other systems
12   see the man pages on your C compiler or the Perl config
13   information.
14 
15 */
16 
17 #include <stdio.h>
18 #include <math.h>
19 
20 /* For WindowsNT you have to make sure that the relevant routine
21    is exported by the dll otherwise it will not be loadable even if it
22    can be linked. In this example we use the __declspec declaration.
23    The same effect can be achieved with .def files although currently
24    CallExt.pm does not know to use them */
25 #ifdef WIN32
26 #define DLLEXPORT __declspec(dllexport)
27 #else
28 #define DLLEXPORT
29 #endif
30 
31 #include "pdlsimple.h"
32 
33 /* This is the action routine */
34 
loglog_doit(float * x,float * y,int nvals)35 void loglog_doit( float *x, float *y, int nvals) {
36 
37    int i;
38 
39    for (i=0; i<nvals; i++)
40       x[i] = log((float)x[i])/log((float)y[i]);
41 }
42 
43 /*
44    This is the hook routine - nargs is the number of
45    arguments and *args is an array of pdl structures
46 */
47 
loglog_ext(int nargs,pdlsimple ** args)48 DLLEXPORT int loglog_ext(int nargs, pdlsimple **args) {
49 
50    pdlsimple* x;
51    pdlsimple* y;
52 
53    /* Check args */
54 
55    printf("\nExecuting the C external routine\n\n");
56 
57    if (nargs != 2) {
58       fprintf(stderr, "Error in number of arguments\n");
59       return (0); /* Failure */
60    }
61 
62    x = args[0]; y = args[1];
63 
64    if (x->datatype != PDL_F || y->datatype != PDL_F) {
65       fprintf(stderr, "Error in data type of arguments %d %d\n",
66               x->datatype, y->datatype);
67       return (0); /* Failure */
68    }
69 
70    if (x->nvals != y->nvals) {
71       fprintf(stderr, "Number of data values unequal in arguments\n");
72       return(0); /* Failure */
73    }
74 
75    /* Now do the buisness! */
76 
77    loglog_doit( (float*) x->data, (float*) y->data, (int) x->nvals);
78 
79    return(1);  /* Success! */
80 }
81 
82