The return type of mxIsDouble, mxIsSingle, and mxIsClass (mex for Fortran)

1 visualizzazione (ultimi 30 giorni)
the return of mxIsDouble for Fortran (also mxIsSingle and mxIsClass) is INTEGER*4.
Here are my question:
  1. Should the declaration of mxIsDouble be always
INTEGER*4 :: mxIsDouble
What if I declare it as
INTEGER :: mxIsDouble
or
LOGICAL :: mxIsDouble
2. It is said that INTEGER*4 is not standard and should be replaced by kind. How should I do it? I know that INTEGER(KIND=4) is incorrect. In addition, why does mex continue to use such a nonstandard feature?
Thank you very much!

Risposte (1)

James Tursa
James Tursa il 29 Lug 2019
Do what the documentation says and use INTEGER*4. Yes, it is non-standard but you are very unlikely to run into a compiler that does not understand it the way you want it. That being said, just plain INTEGER will probably also work (unless you have compiler directives forcing INTEGER to be some other size), since most compilers will use a 4-byte integer for the default INTEGER type even on 64-bit systems (I suspect they do this to try and maximize backwards compatibility for older source code). The only thing I would add is the EXTERNAL qualifier. E.g.,
INTEGER*4, EXTERNAL :: mxIsDouble
Do NOT use LOGICAL for these functions, since that would bring into play the byte-size of a logical vs integer and the bit-patterns of true vs false for your particular compiler. No need to complicate it with this.
As for INTEGER(KIND=4), I know this isn't required to map into a 4-byte integer per the standard, but my guess is your compiler will do exactly that and it will probably also work.
  2 Commenti
Zaikun Zhang
Zaikun Zhang il 30 Lug 2019
Thank you very much for the informative answer!
Just a small observation on "most compilers will use a 4-byte integer for the default INTEGER type even on 64-bit systems": indeed, I found that mex -largeArrayDims will lead to a 8-byte detault INTEGER on my machine. I am using a 64-bit Linux PC with MATLAB R2018a, the compiler being gfortran6.
James Tursa
James Tursa il 30 Lug 2019
I suppose it is not suprising that the extra qualifier -largeArrayDims led to compiler directives forcing the default Fortran INTEGER type to be 8-bytes. But this just supports my original advice to use exactly what is in the doc for argument types.

Accedi per commentare.

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Prodotti


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by