Skip to content

Commit cd0a2a3

Browse files
authored
[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.
1 parent 11b8699 commit cd0a2a3

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

flang-rt/lib/runtime/extensions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstring>
2121
#include <ctime>
2222
#include <signal.h>
23+
#include <stdlib.h>
2324
#include <thread>
2425

2526
#ifdef _WIN32
@@ -262,5 +263,10 @@ int RTNAME(Chdir)(const char *name) {
262263

263264
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
264265

266+
void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,
267+
int (*compar)(const void *, const void *)) {
268+
qsort(array, *len, *isize, compar);
269+
}
270+
265271
} // namespace Fortran::runtime
266272
} // extern "C"

flang/docs/Intrinsics.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,3 +1106,34 @@ end program chdir_func
11061106
- **Standard:** GNU extension
11071107
- **Class:** function
11081108
- **Syntax:** `RESULT = IERRNO()`
1109+
1110+
### Non-Standard Intrinsics: QSORT
1111+
1112+
#### Description
1113+
1114+
```
1115+
SUBROUTINE QSORT(ARRAY, LEN, ISIZE, COMPAR)
1116+
TYPE(*) :: ARRAY(*)
1117+
INTEGER(4) :: LEN, ISIZE
1118+
INTERFACE
1119+
INTEGER(4) FUNCTION COMPAR(A, B)
1120+
TYPE(*) :: A, B
1121+
END FUNCTION
1122+
END INTERFACE
1123+
END SUBROUTINE
1124+
```
1125+
1126+
Sort `ARRAY` in place in ascending order given the comparison function `COMPAR`.
1127+
The array number of elements is given by `LEN` and the element byte size is given
1128+
by `ISIZE`.
1129+
1130+
`COMPAR` function takes the addresses of element `A` and `B` and must return:
1131+
- a negative value if `A` < `B`
1132+
- zero if `A` == `B`
1133+
- a positive value otherwise.
1134+
1135+
#### Usage and Info
1136+
1137+
- **Standard:** lib3f (section 3f of old man pages).
1138+
- **Class:** subroutine
1139+
- **Syntax:** `CALL QSORT(ARRAY, LEN, ISIZE, COMPAR)`

0 commit comments

Comments
 (0)