From 15f22c5711c17268d4c051d7d641d5eb5f423b3c Mon Sep 17 00:00:00 2001 From: Ed J Date: Mon, 10 Jan 2022 03:51:58 +0000 Subject: [PATCH] generate pdl_datatypes enum with X macro --- Basic/Core/Types.pm.PL | 39 -------------------- Basic/Core/pdl.h.PL | 75 +++++++++++++++++++++++++-------------- Basic/Core/pdlsimple.h.PL | 44 ++++++++++++++++++----- 3 files changed, 84 insertions(+), 74 deletions(-) diff --git a/Basic/Core/Types.pm.PL b/Basic/Core/Types.pm.PL index 6f8810790..9825ad78c 100644 --- a/Basic/Core/Types.pm.PL +++ b/Basic/Core/Types.pm.PL @@ -606,45 +606,6 @@ sub typesynonyms { return "$add\n"; } -=head2 datatypes_header - -=for ref - -return C header text for F and F. - -=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; - - < - objects of this class diff --git a/Basic/Core/pdl.h.PL b/Basic/Core/pdl.h.PL index 2061853a8..648d858dd 100644 --- a/Basic/Core/pdl.h.PL +++ b/Basic/Core/pdl.h.PL @@ -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; @@ -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! @@ -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) = @_; @@ -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; diff --git a/Basic/Core/pdlsimple.h.PL b/Basic/Core/pdlsimple.h.PL index dcccbe7b1..0ef47e27e 100644 --- a/Basic/Core/pdlsimple.h.PL +++ b/Basic/Core/pdlsimple.h.PL @@ -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 @@ -27,7 +26,7 @@ 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 #include @@ -35,8 +34,37 @@ print OUT <<"!GROK!THIS!"; #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 @@ -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