Search code examples
functionfortranproceduregfortran

How to find the source code of intrinsic functions - gfortran


Hi I am using gfortran for my project and my prof has told me to learn about bit manipulation functions code/ how they work.
These are the steps I have taken and failed:

  1. I tried to find the intrinsic functions file where all the functions are stored. Even though I did managed to find the file ,It doesn't seem to help as the file just has the name of the function and the variable type.(I will attach the intrinsic function fill below).

  2. I tried searching in google but I only get what the function does and not the source code.

Could someone point me where I can find these function's source code? Mainly I require bit manipulation functions.

OS: Windows
IDE: Codeblocks
Compiler: GNU compiler
This fortran file is inside codeblocks folder


Solution

  • Intrinsic functions are functions implemented inside the compiler. Some are in the runtime library (e.g., libgfortran) so you can find some source code for them, some may not actually exist as a function at all and the compiler will insert some (assembly, machine) code in the location where such a function is called.

    Intrinsic functions are typically not implemented in Fortran, but in a systems programming language like C or C++. Some will eventually call code written in the assembly language for a particular CPU architecture. Many will call functions from the standard C library, e.g. the GLIBC implementation.

    Some can theoretically be written in Fortran, some cannot (e.g. functions with indeterminate number of arguments like min or type transformation functions like transfer). Many use internal information only available to the compiler (e.g. the content of the array descriptor or the polymorphic object descriptor).

    If you are interested, you can certainly search how the functions are implemented in open source compilers, like in libgfortran https://github.com/gcc-mirror/gcc/tree/master/libgfortran but it is not in any way necessary for programming in Fortran.

    One just need to know their description from the standard documents, compiler manuals or reference textbooks.


    Specifically to the bit manipulation functions:

    They are well described in the manual linked above and there will be examples provided in textbooks.

    If you want to see the actual machine code, you can try compiling a simple code calling such a function, e.g. in an online compiler like https://godbolt.org/z/7Yq6acrYr

    As you can see, the compiler ultimately transforms the call to ishft to machine code including the x86-64 sal instruction. There is no external function called.


    Note that the file fortran_procedures.f90 is just provided by someone for your reference and contains the information you can find in the standard document or the manuals. It is not a Fortran source code that can be compiled.