[flang-commits] [flang] cd0a2a3 - [flang] add QSORT extension intrinsic to the runtime (#132033)
via flang-commits
flang-commits at lists.llvm.org
Wed Mar 19 08:14:42 PDT 2025
Author: jeanPerier
Date: 2025-03-19T16:14:37+01:00
New Revision: cd0a2a3f1bdaf8dc54d4a4d3c478d41345671e72
URL: https://github.com/llvm/llvm-project/commit/cd0a2a3f1bdaf8dc54d4a4d3c478d41345671e72
DIFF: https://github.com/llvm/llvm-project/commit/cd0a2a3f1bdaf8dc54d4a4d3c478d41345671e72.diff
LOG: [flang] add QSORT extension intrinsic to the runtime (#132033)
Add support for legacy Fortran intrinsic QSORT from lib 3f.
This is a thin Fortran wrapper over libc qsort.
Added:
Modified:
flang-rt/lib/runtime/extensions.cpp
flang/docs/Intrinsics.md
Removed:
################################################################################
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 <cstring>
#include <ctime>
#include <signal.h>
+#include <stdlib.h>
#include <thread>
#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)`
More information about the flang-commits
mailing list