⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fandc.me

📁 speech signal process tools
💻 ME
字号:
.lo.de RM          \"macro to restore original left margin.br.nr $i 0...sc.he 'FORTRAN FROM ESPS''page %'.fo '1.3'\'9/11/91'.nf.ce \s+4\fBENTROPIC RESEARCH LABORATORY, INC.\fR\s-4.sp .5i.ce 100\s+1\fBESPS APPLICATIONS NOTE: Calling FORTRAN subroutines from C\fP\s-1.sp .3i\fIDavid Burton\fP.sp .3iEntropic Research Laboratory, Inc.600 Pennsylvania Ave. SE, Suite 202Washington, D.C. 20003(202) 547-1420.sp .5i.fi.ft LR.RM.sh 1 "INTRODUCTION".sp 1.ppAlthough the Entropic Signal Processing System (\fIESPS\fP)is written entirely in the C programming language,FORTRAN functions and subroutines can be linked into\fIESPS\fP programs. This capability is a function of the C and FORTRAN language implementations,and is not a private feature of \fIESPS\fP. This applications note shows most ofthe necessary rules for linking in existing FORTRAN routines..pp\fIESPS\fP provides a rich set of signal processing and file utility functionsand a complete programming environment.Before modifying programs to link in private FORTRAN routines,you should  check the \fIESPS\fP library for an appropriate routine(see section 3 of the manual \- use \fIeman -k\fP).Also,consider writingnew functions in C;most likely, you will be more productive by using the ESPS programming tools..ppThe rest of this document contains the following:section 2 describes the C and FORTRAN naming conventions forglobal symbols;section 3 lists the corresponding C and FORTRANdata types; section 4 shows how argument variables arepassed from C to FORTRAN insubroutines and functions; section 5 describes theconvention for FORTRAN function return values;section 6 explains how to hook up FORTRAN I/O;and section 7 shows a full exampleof linking a FORTRAN subroutine into a C calling program..sh 1 "GLOBAL SYMBOLS AND COMPILER NAMING CONVENTIONS".ppGlobal symbols are named by FORTRAN and C compilersin different manners. FORTRAN truncates the name to 6 characters,converts the name to lower case, and prepends \fIand\fP appendsan underscore. For example,.nf.ft CW	SUBROUTINE FSUB()	INTEGER*4 GLOBAL	COMMON /EARTH/ GLOBAL	....	END.fi.ft LRresults in the definition of two global symbols:\fI_fsub_\fP and \fI_earth_\fP.Note that it is impossible to reference a C language upper-case symbol froma FORTRAN routine..ppA C compiler names global symbols by simply prependingan underscore to the variable name. No truncation orcase conversion is done. In the following code fragment,the global symbols produced by the C compilercorresponding to the variables.ft CWearth_.ft Rand .ft CWfsub_.ft Rare "\fI_earth_\fP" and"\fI_fsub_\fP", respectively..nf.ft CW	extern long int earth_	main()	{		earth_ = 0;		...		fsub_();		...		exit(0);	}.fi.ft LRThus,by using the appropriatevariable names,reference to variables defined in FORTRAN subroutinesor functions can be made directly in C calling routines..ppNote, however,if .ft CWEARTH.ft LRin the FORTRAN subroutine is a mulivariable COMMON block,then .ft CW earth_.ft LRshould be declared as an array. If aCOMMON block contains mixed data types,the calling program is responsible forkeeping track of which bytes contain what data types.If the COMMON block is all one data type,the calling program can simply declare an array of the appropriate type, and then use it as one would use any array..sh 1 "DATA TYPES".ppYou must understand the relationship between how values arestored in memory for C and FORTRAN in order topass values between C routines and FORTRAN subroutines.The following data declarations have identical memoryrepresentations:.nf.ft CW	FORTRAN			C		Number of bytes	INTEGER*2	 		short			2	INTEGER*4			long			4	REAL*4			float			4	REAL*8			double		8	COMPLEX*8			float_cplx		8	COMPLEX*16			double_cplx		16	LOGICAL*2			short			2	LOGICAL*4			long			4	CHARACTER*n s		char s[n]		n.fi.ft LRNote that the FORTRAN declaration .ft CWINTEGER*2.ft Ris not supportedunder all FORTRAN implementations, and thatthe C declarations .ft CWfloat_cplx\fR and .ft CWdouble_cplx\fRare \fIESPS\fP extensions of C data types.Also, there are no FORTRAN equivalents to the \fIESPS\fPdata type extensions \fIshort_cplx\fP, \fIlong_cplx\fP, and\fIbyte_cplx\fP..sh 1 "ARGUMENT LISTS".ppIn addition to global variables,it is possible to share data between C and FORTRANby use of argument lists in function and subroutine calls.The C and FORTRAN compilers both place the function argumentson the stack in the same order. C arguments are passedby value, however, and FORTRAN arguments are passed byreference. ("Passed by reference" means that the addressof arguments is put on the stack, while "passed by value"means that argument values are placed directly on the stack.).ppIn calling a FORTRAN language routine from a C program,the addresses of the argument variables must bepassed instead of the variable values themselves.C has an operator to do this for you;it is the address operator "&". C also has an indirectionoperator "*"; this is used to showthat a variable contains an address rather than a value(it is a "pointer" variable). Finally,there is a close correspondence between "array A" and "pointer to A." When an array name appears in an argument list (actually when it appears in any expression),the type is converted from "array" to "pointer to,"and the value of the array name is converted to a pointer containingthe address of the firstelement of the array.This means that pointer variables and array names can be passeddirectlyinto FORTRAN routines without any modification;regular scalar variable must be passed by using the address operator..ppThe following is an example of the data declarations and function callsfor a FORTRAN subroutine being called from a C program:note where and when the address operator "&" is used.".nf.ft CW/* C main program */main(){short i = 10, m = 4;long k = 10, *j = &k;float f = 20.;static double_cplx dc = {10., 20.};static long array[] = {1, 2, 3, 4}; ....    fsub_(&i, j, &f, &dc, array, &m); ....    exit(0);    }C..  FORTRAN subroutine definition       SUBROUTINE FSUB(SHORT, LONG, FLOAT, DC, ARRAY, SIZE)       INTEGER*2 SHORT, SIZE       INTEGER*4 LONG       REAL*4 FLOAT       COMPLEX*16 DC       INTEGER*4 ARRAY(SIZE)       ....             END.fi.ft LRIn the C .ft CWmain().ft Rroutine the variable .ft CWj.ft R is of type "pointer to long";it contains the address of the variable .ft CWk.ft R, and thusis in a form directly acceptable to the FORTRAN routine.Similarly, the variable .ft CWarray.ft R gets passed as the address ofthe first element in the array(or .ft CW&array[0].ft R), and is correct for passing into a FORTRAN routine.The rest of the variables need to be passed by using the addressoperator..sh 1 "FUNCTION RETURN VALUES".ppFORTRAN functions that return INTEGER*2, INTEGER*4, LOGICAL*2,LOGICAL*4, REAL*4, and REAL*8 values have equivalent standardC types and can be used directly in C routines. For functions returningfloat_cplx or double_cplx, an extra argument must be addedto the function argument list. Below is an example showinga FORTRAN function that returns a double_cplx being calledfrom a C routine. The first argument in the C subroutine call(.ft CWz.ft Rin .ft CWcadd_(z, &z1, &z2).ft R) is the address where the evaluated functionresult gets stored..nf.ft CW/* C main program */main(){double_cplx *z, z1, z2; ....	cadd_(z, &z1, &z2);	....}.fi.ft LR.nf.ft CWC..   FORTRAN function definition	 COMPLEX*16 FUNCTION CADD(Z1, Z2)	 	 COMPLEX*16 Z1, Z2	 CADD = Z1 + Z2	 RETURN	 END.fi.ft LR.sh 1 "FORTRAN I/O".ppFrom within a C program, FORTRAN I/O can be initialized byusing the "f_init()" function and closed up by usingthe "f_exit()" function. Both are called with no arguments.The "f_init()" call connects the C data streams stderr, stdout, and stdinto the FORTRAN logical units 0, 5, and 6, respectively..sh 1 "AN EXAMPLE".ppTo build a C program that links in FORTRAN object modules,FORTRAN libraries must also be linked into the program.You may need to link in "libF77" (standard FORTRAN calls), "libI77" (standard FORTRAN I/O calls), and/or "libU77" (FORTRAN system calls),depending on what calls the FORTRAN subroutine makes.The order of the libraries is also important on some machines:"libF77 builds on "libI77", and "libI77" builds on "libU77".(Note that in the MASSCOMP C compiler version 1.3the order of "libF77" and "libI77" must be reversedfrom the rule stated above, and on the CONVEX C2 compiler, libD77 and libmathC2must also be included.).ppBelow is an example of a C main thatcalls a FORTRAN subroutine. Several of the rules mentioned above are shown in use in thisprogram. To compile and run this program, first compile the FORTRAN functioninto an object module:.nf.ft CW	f77 -c fsub.f.fi.ft LRThis produces "fsub.o".Next compile and link this object module with the calling Cprogram:.nf.ft CW	ecc cmain.c fsub.o -lF77 -lI77 -o test.fi.ft LR.ft CWecc.ft Ris the \fIESPS\fP cover script for the standard C compiler"cc." .ft CWecc.ft Rautomatically links in the appropriate \fIESPS\fPlibraries and include files. .ft CWtest.ft Ris the executable binarythat calls the FORTRAN subroutine. If FORTRAN system callswere also used in .ft CWfsub.f.ft R, then "-lU77" would be added to the C compilecommand..sp 2.ce-------------------------------------.sp 2.nf/*    This shows how to call FORTRAN subroutines from within a C program.    The use of scalars, arrays, and complex numbers are shown in this example.    Also, notice the global variable naming convention ( earth_ and fsub_ ) and   how to do I/O from within the FORTRAN subroutine ( f_init() and f_exit() ).   When compiling programs that link in FORTRAN functions, the following    libraries should be linked in at compile time, if they are appropriate:	   libf77 - gives you standard FORTRAN calls, other than I/O	   libI77 - standard FORTRAN I/O calls	   libU77 - system subroutines and functions ("system calls") */.bp.ft CW# include <stdio.h># include <esps/esps.h>extern long int earth_;main(){short i = 10, m = 4;long k = 10, *j = &k;float f = 20.;static double_cplx dc = {10., 20.};static long array[] = {1, 2, 3, 4};earth_ = 0;/*  set up FORTRAN I/O */     f_init();    /*  print variable values before FORTRAN function */    (void)fprintf(stderr, "\\ni = %d\\nj = %d\\nf = %f\\ndc.real = %lf\\tdc.imag = %lf    \\nearth_ = %d\\narray[0 - 3] = %d, %d, %d, %d\\n\\n", i, *j, f, dc.real, dc.imag,    earth_, array[0], array[1], array[2], array[3]);/*  call FORTRAN subroutine */    fsub_(&i, j, &f, &dc, array, &m);/*  print variable values after FORTRAN function */   (void)fprintf(stderr, "\\ni = %d\\nj = %d\\nf = %f\\ndc.real = %lf\\tdc.imag = %lf   \\nearth_ = %d\\narray[0 - 3] = %d, %d, %d, %d\\n\\n", i, *j, f, dc.real, dc.imag,   earth_, array[0], array[1], array[2], array[3]);/*  close FORTRAN I/O and exit program */   f_exit();   exit(0);}.ft R.fi.ce-------------------------------------.bp.sp 2.nf.ft CWC..  FORTRAN subroutine definition       SUBROUTINE FSUB(SHORT, LONG, FLOAT, DC, ARRAY, SIZE)       INTEGER*4 GLOBAL       INTEGER*2 SHORT, SIZE       INTEGER*4 LONG       REAL*4 FLOAT       COMPLEX*16 DC       INTEGER*4 ARRAY(SIZE)       COMMON /EARTH/ GLOBAL       SHORT = 2*SHORT       LONG = LONG/FLOAT              FLOAT = FLOAT/SHORT       DC = DC + DC       GLOBAL = 1C..  unit 0 is stderr, unit 5 is stdin, and unit 6 is stdout       WRITE(6,*) "ARRAY VALUES FOLLOW:"       WRITE(6,*) ARRAY       RETURN       END.ft R.fi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -