Skip to content

Commit

Permalink
generate pdl_datatypes enum with X macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 10, 2022
1 parent c18cde0 commit 15f22c5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 74 deletions.
39 changes: 0 additions & 39 deletions Basic/Core/Types.pm.PL
Original file line number Diff line number Diff line change
Expand Up @@ -606,45 +606,6 @@ sub typesynonyms {
return "$add\n";
}
=head2 datatypes_header
=for ref
return C header text for F<pdl.h> and F<pdlsimple.h>.
=cut
sub datatypes_header {
my $enum = join ', ', 'PDL_INVALID=-1', map $_->{sym}, @HASHES;
my $typedefs = join '', map "typedef $_->{realctype} $_->{ctype};\n", @HASHES;
my $union = join "\n", map " $_->{ctype} $_->{ppsym};", @HASHES;
<<EOD . "\n".typesynonyms()."\n";
/*****************************************************************************/
/*** This section of .h file generated automatically by ***/
/*** PDL::Types::datatypes_header() - don't edit manually ***/
/* Data types/sizes [must be in order of complexity] */
typedef enum { $enum } pdl_datatypes;
/* Define the pdl data types */
$typedefs
typedef struct {
pdl_datatypes type;
union {
$union
} value;
} PDL_Anyval;
/*****************************************************************************/
EOD
}
=head1 PDL::Type OBJECTS
This module declares one class - C<PDL::Type> - objects of this class
Expand Down
75 changes: 49 additions & 26 deletions Basic/Core/pdl.h.PL
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use strict;
use warnings;

require './Types.pm';
my $PDL_DATATYPES = PDL::Types::datatypes_header();

require './Config.pm';
die "No PDL::Config found" unless %PDL::Config;
Expand All @@ -14,7 +13,7 @@ open OUT,">$file" or die "Can't create $file: $!";
chmod 0644, $file;

print OUT sprintf qq{#line %d "%s"\n}, __LINE__ + 2, __FILE__;
print OUT <<"!GROK!THIS!";
print OUT <<'EOF';
/*
* THIS FILE IS GENERATED FROM pdl.h.PL! Do NOT edit!
Expand All @@ -40,31 +39,9 @@ extern int pdl_debugging;
#endif
typedef struct pdl pdl;
EOF

$PDL_DATATYPES
#include "pdlthread.h"
/* Auto-PThreading (i.e. multi-threading) settings for PDL functions */
/* Target number of pthreads: Actual will be this number or less.
A 0 here means no pthreading */
extern int pdl_autopthread_targ;
/* Actual number of pthreads: This is the number of pthreads created for the last
operation where pthreading was used
A 0 here means no pthreading */
extern int pdl_autopthread_actual;
/* Minimum size of the target PDL involved in pdl function to attempt pthreading (in MBytes )
For small PDLs, it probably isn't worth starting multiple pthreads, so this variable
is used to define that threshold (in M-elements, or 2^20 elements ) */
extern int pdl_autopthread_size;
extern PDL_Indx pdl_autopthread_dim;
$mymalloc
!GROK!THIS!

my @methods = qw(symbol ctype ppsym shortctype defbval);
my @methods = qw(symbol ctype ppsym shortctype defbval realctype);
sub makeg { map { my $t = $_; [map $t->$_, @methods] } grep $_[0]->($_), PDL::Types::types() }
sub makelister {
my ($name, $is2, $underscore, @list) = @_;
Expand All @@ -86,6 +63,52 @@ print OUT makelister('REAL', 0, 0, @generics_real);
print OUT makelister('REAL', 1, 0, @generics_real);
print OUT makelister('COMPLEX', 0, 0, @generics_real);
print OUT makelister('COMPLEX', 1, 0, @generics_real);

print OUT sprintf qq{#line %d "%s"\n}, __LINE__ + 2, __FILE__;
print OUT <<'EOF';
#define X(sym, ...) \
, sym
typedef enum {
PDL_INVALID=-1
PDL_TYPELIST_ALL(X)
} pdl_datatypes;
#undef X
#define X(sym, ctype, ppsym, shortctype, defbval, realctype, ...) \
typedef realctype ctype;
PDL_TYPELIST_ALL(X)
#undef X
typedef struct {
pdl_datatypes type;
union {
#define X(sym, ctype, ppsym, shortctype, defbval, realctype, ...) \
ctype ppsym;
PDL_TYPELIST_ALL(X)
#undef X
} value;
} PDL_Anyval;
#include "pdlthread.h"
/* Auto-PThreading (i.e. multi-threading) settings for PDL functions */
/* Target number of pthreads: Actual will be this number or less.
A 0 here means no pthreading */
extern int pdl_autopthread_targ;
/* Actual number of pthreads: This is the number of pthreads created for the last
operation where pthreading was used
A 0 here means no pthreading */
extern int pdl_autopthread_actual;
/* Minimum size of the target PDL involved in pdl function to attempt pthreading (in MBytes )
For small PDLs, it probably isn't worth starting multiple pthreads, so this variable
is used to define that threshold (in M-elements, or 2^20 elements ) */
extern int pdl_autopthread_size;
extern PDL_Indx pdl_autopthread_dim;
EOF
print OUT "$mymalloc\n";

print OUT <<'EOF';
#define PDL_GENERICSWITCH_CASE(X, symbol, ...) \
case symbol: { X(symbol, __VA_ARGS__) } break;
Expand Down
44 changes: 35 additions & 9 deletions Basic/Core/pdlsimple.h.PL
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use Config;
use File::Basename qw(&basename &dirname);

require './Types.pm';
my $PDL_DATATYPES = PDL::Types::datatypes_header();

# List explicitly here the variables you want Configure to
# generate. Metaconfig only looks for shell variables, so you
Expand All @@ -27,16 +26,45 @@ print "Extracting $file (with variable substitutions)\n";
# You can use $Config{...} to use Configure variables.

print OUT sprintf qq{#line %d "%s"\n}, __LINE__ + 2, __FILE__;
print OUT <<"!GROK!THIS!";
print OUT <<'EOF';

#include <complex.h>
#include <stddef.h>
#include <stdint.h>
#ifndef __PDL_H

/* These are kept automatically in sync with pdl.h during perl build */
EOF

$PDL_DATATYPES
my @methods = qw(symbol ctype ppsym shortctype defbval realctype);
sub makeg { map { my $t = $_; [map $t->$_, @methods] } grep $_[0]->($_), PDL::Types::types() }
sub makelister {
my ($name, $is2, $underscore, @list) = @_;
my $suff = $is2 ? '2' : '';
my $arg1 = $is2 ? 'X, X2' : 'X';
my $arg2 = $is2 ? 'X2, ' : '';
$underscore = $underscore ? '_' : '';
("#define PDL_TYPELIST${suff}_$name$underscore($arg1) \\\n",
(map " X($arg2".join(',', @$_).")\\\n", @list), "\n\n");
}
my @generics = makeg(sub {1});
print OUT makelister('ALL', 0, 0, @generics);

print OUT sprintf qq{#line %d "%s"\n}, __LINE__ + 2, __FILE__;
print OUT <<'EOF';

#define X(sym, ...) \
, sym
typedef enum {
PDL_INVALID=-1
PDL_TYPELIST_ALL(X)
} pdl_datatypes;
#undef X

#define X(sym, ctype, ppsym, shortctype, defbval, realctype, ...) \
typedef realctype ctype;
PDL_TYPELIST_ALL(X)
#undef X

#endif

Expand All @@ -49,13 +77,11 @@ $PDL_DATATYPES
PP anyway (which is fairly trivial).
*/

struct pdlsimple {
int datatype; /* whether byte/int/float etc. */
typedef struct {
pdl_datatypes datatype; /* whether byte/int/float etc. */
void *data; /* Generic pointer to the data block */
PDL_Indx nvals; /* Number of data values */
PDL_Indx *dims; /* Array of data dimensions */
PDL_Indx ndims; /* Number of data dimensions */
};
typedef struct pdlsimple pdlsimple;
!GROK!THIS!
} pdlsimple;
EOF

0 comments on commit 15f22c5

Please sign in to comment.