Skip to content

Commit d5d33eb

Browse files
authored
Merge pull request #327 from bonachea/issue-326
issue #326: Promote char_{min,max}_wrapper helpers to module scope
2 parents 575a36c + 065e83d commit d5d33eb

2 files changed

Lines changed: 62 additions & 42 deletions

File tree

‎src/caffeine/co_max_s.F90‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33

44
#include "assert_macros.h"
55

6+
module caf_prif_co_max_helper
7+
!! F23 C1807: bind(c) callbacks written in Fortran must appear in a top-level module
8+
use iso_c_binding, only: c_ptr, c_size_t, c_char, c_f_pointer
9+
implicit none
10+
11+
contains
12+
13+
subroutine caf_char_max_wrapper(arg1, arg2_and_out, count, cdata) bind(C)
14+
type(c_ptr), intent(in), value :: arg1, arg2_and_out
15+
integer(c_size_t), intent(in), value :: count
16+
type(c_ptr), intent(in), value :: cdata
17+
18+
integer(c_size_t), pointer :: char_len
19+
integer(c_size_t) :: i
20+
21+
if (count == 0) return
22+
call c_f_pointer(cdata, char_len)
23+
block
24+
character(len=char_len,kind=c_char), pointer :: lhs(:), rhs_and_result(:)
25+
call c_f_pointer(arg1, lhs, [count])
26+
call c_f_pointer(arg2_and_out, rhs_and_result, [count])
27+
do i = 1, count
28+
if (lhs(i) >= rhs_and_result(i)) rhs_and_result(i) = lhs(i)
29+
end do
30+
end block
31+
end subroutine
32+
33+
end module
34+
635
submodule(prif:prif_private_s) co_max_s
736
! DO NOT ADD USE STATEMENTS HERE
837
! All use statements belong in prif_private_s.F90
@@ -34,32 +63,13 @@ subroutine contiguous_co_max(a, result_image, stat, errmsg, errmsg_alloc)
3463
current_team%info%gex_team)
3564
end subroutine
3665

37-
subroutine char_max_wrapper(arg1, arg2_and_out, count, cdata) bind(C)
38-
type(c_ptr), intent(in), value :: arg1, arg2_and_out
39-
integer(c_size_t), intent(in), value :: count
40-
type(c_ptr), intent(in), value :: cdata
41-
42-
integer(c_size_t), pointer :: char_len
43-
integer(c_size_t) :: i
44-
45-
if (count == 0) return
46-
call c_f_pointer(cdata, char_len)
47-
block
48-
character(len=char_len,kind=c_char), pointer :: lhs(:), rhs_and_result(:)
49-
call c_f_pointer(arg1, lhs, [count])
50-
call c_f_pointer(arg2_and_out, rhs_and_result, [count])
51-
do i = 1, count
52-
if (lhs(i) >= rhs_and_result(i)) rhs_and_result(i) = lhs(i)
53-
end do
54-
end block
55-
end subroutine
56-
5766
module procedure prif_co_max_character
67+
use caf_prif_co_max_helper
5868
integer(c_size_t), target :: char_len
5969
procedure(prif_operation_wrapper_interface), pointer :: op
6070

6171
char_len = len(a)
62-
op => char_max_wrapper
72+
op => caf_char_max_wrapper
6373
#if defined(__GFORTRAN__) && 0
6474
! gfortran 13.2 (sometimes?) crashes on the call below
6575
! internal compiler error: in make_decl_rtl, at varasm.cc:1442

‎src/caffeine/co_min_s.F90‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33

44
#include "assert_macros.h"
55

6+
module caf_prif_co_min_helper
7+
!! F23 C1807: bind(c) callbacks written in Fortran must appear in a top-level module
8+
use iso_c_binding, only: c_ptr, c_size_t, c_char, c_f_pointer
9+
implicit none
10+
11+
contains
12+
13+
subroutine caf_char_min_wrapper(arg1, arg2_and_out, count, cdata) bind(C)
14+
type(c_ptr), intent(in), value :: arg1, arg2_and_out
15+
integer(c_size_t), intent(in), value :: count
16+
type(c_ptr), intent(in), value :: cdata
17+
18+
integer(c_size_t), pointer :: char_len
19+
integer(c_size_t) :: i
20+
21+
if (count == 0) return
22+
call c_f_pointer(cdata, char_len)
23+
block
24+
character(len=char_len,kind=c_char), pointer :: lhs(:), rhs_and_result(:)
25+
call c_f_pointer(arg1, lhs, [count])
26+
call c_f_pointer(arg2_and_out, rhs_and_result, [count])
27+
do i = 1, count
28+
if (lhs(i) <= rhs_and_result(i)) rhs_and_result(i) = lhs(i)
29+
end do
30+
end block
31+
end subroutine
32+
33+
end module
34+
635
submodule(prif:prif_private_s) co_min_s
736
! DO NOT ADD USE STATEMENTS HERE
837
! All use statements belong in prif_private_s.F90
@@ -34,32 +63,13 @@ subroutine contiguous_co_min(a, result_image, stat, errmsg, errmsg_alloc)
3463
current_team%info%gex_team)
3564
end subroutine
3665

37-
subroutine char_min_wrapper(arg1, arg2_and_out, count, cdata) bind(C)
38-
type(c_ptr), intent(in), value :: arg1, arg2_and_out
39-
integer(c_size_t), intent(in), value :: count
40-
type(c_ptr), intent(in), value :: cdata
41-
42-
integer(c_size_t), pointer :: char_len
43-
integer(c_size_t) :: i
44-
45-
if (count == 0) return
46-
call c_f_pointer(cdata, char_len)
47-
block
48-
character(len=char_len,kind=c_char), pointer :: lhs(:), rhs_and_result(:)
49-
call c_f_pointer(arg1, lhs, [count])
50-
call c_f_pointer(arg2_and_out, rhs_and_result, [count])
51-
do i = 1, count
52-
if (lhs(i) <= rhs_and_result(i)) rhs_and_result(i) = lhs(i)
53-
end do
54-
end block
55-
end subroutine
56-
5766
module procedure prif_co_min_character
67+
use caf_prif_co_min_helper
5868
integer(c_size_t), target :: char_len
5969
procedure(prif_operation_wrapper_interface), pointer :: op
6070

6171
char_len = len(a)
62-
op => char_min_wrapper
72+
op => caf_char_min_wrapper
6373
#if defined(__GFORTRAN__) && 0
6474
! gfortran 13.2 (sometimes?) crashes on the call below
6575
! internal compiler error: in make_decl_rtl, at varasm.cc:1442

0 commit comments

Comments
 (0)