annotate R/rdyncall/src/rutils_float.c @ 56:16151547265e

made callback example python 2 compatible, and added some randomness to numbers to sort
author Tassilo Philipp
date Tue, 02 Feb 2021 21:07:41 +0100
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /** ===========================================================================
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 ** R-Package: rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 ** File: src/rutils_float.c
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 ** Description: Utility functions for handling C float data types.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 **/
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 #define USE_RINTERNALS
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 #include <Rdefines.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 #include <Rinternals.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 #include <R_ext/RS.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 /* Float utils */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 SEXP r_as_floatraw(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 SEXP ans;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 int i, n;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 double *dp;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 float *fp;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 dp = (double*) REAL(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 n = LENGTH(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 if (n < 1) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 error("length of x should be >= 1");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 return R_NilValue;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 ans = PROTECT( Rf_allocVector(RAWSXP, sizeof(float)*n) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 fp = (float*) RAW(ans);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 for(i = 0 ; i < n ; ++i )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 fp[i] = (float) dp[i];
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 UNPROTECT(1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 return ans;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 SEXP r_floatraw2numeric(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 SEXP ans;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 int i, n;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 float *fp;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 double *dp;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 fp = (float*) RAW(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 n = LENGTH(x) / sizeof(float);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 ans = PROTECT( Rf_allocVector(REALSXP, n) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 dp = (double*) REAL(ans);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 for(i = 0 ; i < n ; ++i )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 dp[i] = (double) fp[i];
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 UNPROTECT(1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 return ans;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61