diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp index 75195c33a6c21..7e9e512778a75 100644 --- a/flang-rt/lib/runtime/extensions.cpp +++ b/flang-rt/lib/runtime/extensions.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -262,5 +263,10 @@ int RTNAME(Chdir)(const char *name) { int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; } +void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize, + int (*compar)(const void *, const void *)) { + qsort(array, *len, *isize, compar); +} + } // namespace Fortran::runtime } // extern "C" diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md index 5b671d1b2c740..eb09d550504d0 100644 --- a/flang/docs/Intrinsics.md +++ b/flang/docs/Intrinsics.md @@ -1106,3 +1106,34 @@ end program chdir_func - **Standard:** GNU extension - **Class:** function - **Syntax:** `RESULT = IERRNO()` + +### Non-Standard Intrinsics: QSORT + +#### Description + +``` +SUBROUTINE QSORT(ARRAY, LEN, ISIZE, COMPAR) + TYPE(*) :: ARRAY(*) + INTEGER(4) :: LEN, ISIZE + INTERFACE + INTEGER(4) FUNCTION COMPAR(A, B) + TYPE(*) :: A, B + END FUNCTION + END INTERFACE +END SUBROUTINE +``` + +Sort `ARRAY` in place in ascending order given the comparison function `COMPAR`. +The array number of elements is given by `LEN` and the element byte size is given +by `ISIZE`. + +`COMPAR` function takes the addresses of element `A` and `B` and must return: +- a negative value if `A` < `B` +- zero if `A` == `B` +- a positive value otherwise. + +#### Usage and Info + +- **Standard:** lib3f (section 3f of old man pages). +- **Class:** subroutine +- **Syntax:** `CALL QSORT(ARRAY, LEN, ISIZE, COMPAR)`