(root)/
gcc-13.2.0/
libgomp/
testsuite/
libgomp.c-c++-common/
reduction-2.c
       1  /* C / C++'s logical AND and OR operators take any scalar argument
       2     which compares (un)equal to 0 - the result 1 or 0 and of type int.
       3  
       4     In this testcase, the int result is again converted to a floating-poing
       5     or complex type.
       6  
       7     While having a floating-point/complex array element with || and && can make
       8     sense, having a non-integer/non-bool reduction variable is odd but valid.
       9  
      10     Test: FP reduction variable + integer array.  */
      11  
      12  #define N 1024
      13  char rcf[N];
      14  short rcd[N];
      15  int rf[N];
      16  long rd[N];
      17  
      18  int
      19  reduction_or ()
      20  {
      21    float orf = 0;
      22    double ord = 0;
      23    _Complex float orfc = 0;
      24    _Complex double ordc = 0;
      25  
      26    #pragma omp parallel reduction(||: orf)
      27    for (int i=0; i < N; ++i)
      28      orf = orf || rf[i];
      29  
      30    #pragma omp parallel for reduction(||: ord)
      31    for (int i=0; i < N; ++i)
      32      ord = ord || rcd[i];
      33  
      34    #pragma omp parallel for simd reduction(||: orfc)
      35    for (int i=0; i < N; ++i)
      36      orfc = orfc || rcf[i];
      37  
      38    #pragma omp parallel loop reduction(||: ordc)
      39    for (int i=0; i < N; ++i)
      40      ordc = ordc || rcd[i];
      41  
      42    return orf + ord + __real__ orfc + __real__ ordc;
      43  }
      44  
      45  int
      46  reduction_or_teams ()
      47  {
      48    float orf = 0;
      49    double ord = 0;
      50    _Complex float orfc = 0;
      51    _Complex double ordc = 0;
      52  
      53    #pragma omp teams distribute parallel for reduction(||: orf)
      54    for (int i=0; i < N; ++i)
      55      orf = orf || rf[i];
      56  
      57    #pragma omp teams distribute parallel for simd reduction(||: ord)
      58    for (int i=0; i < N; ++i)
      59      ord = ord || rcd[i];
      60  
      61    #pragma omp teams distribute parallel for reduction(||: orfc)
      62    for (int i=0; i < N; ++i)
      63      orfc = orfc || rcf[i];
      64  
      65    #pragma omp teams distribute parallel for simd reduction(||: ordc)
      66    for (int i=0; i < N; ++i)
      67      ordc = ordc || rcd[i];
      68  
      69    return orf + ord + __real__ orfc + __real__ ordc;
      70  }
      71  
      72  int
      73  reduction_and ()
      74  {
      75    float andf = 1;
      76    double andd = 1;
      77    _Complex float andfc = 1;
      78    _Complex double anddc = 1;
      79  
      80    #pragma omp parallel reduction(&&: andf)
      81    for (int i=0; i < N; ++i)
      82      andf = andf && rf[i];
      83  
      84    #pragma omp parallel for reduction(&&: andd)
      85    for (int i=0; i < N; ++i)
      86      andd = andd && rcd[i];
      87  
      88    #pragma omp parallel for simd reduction(&&: andfc)
      89    for (int i=0; i < N; ++i)
      90      andfc = andfc && rcf[i];
      91  
      92    #pragma omp parallel loop reduction(&&: anddc)
      93    for (int i=0; i < N; ++i)
      94      anddc = anddc && rcd[i];
      95  
      96    return andf + andd + __real__ andfc + __real__ anddc;
      97  }
      98  
      99  int
     100  reduction_and_teams ()
     101  {
     102    float andf = 1;
     103    double andd = 1;
     104    _Complex float andfc = 1;
     105    _Complex double anddc = 1;
     106  
     107    #pragma omp teams distribute parallel for reduction(&&: andf)
     108    for (int i=0; i < N; ++i)
     109      andf = andf && rf[i];
     110  
     111    #pragma omp teams distribute parallel for simd reduction(&&: andd)
     112    for (int i=0; i < N; ++i)
     113      andd = andd && rcd[i];
     114  
     115    #pragma omp teams distribute parallel for reduction(&&: andfc)
     116    for (int i=0; i < N; ++i)
     117      andfc = andfc && rcf[i];
     118  
     119    #pragma omp teams distribute parallel for simd reduction(&&: anddc)
     120    for (int i=0; i < N; ++i)
     121      anddc = anddc && rcd[i];
     122  
     123    return andf + andd + __real__ andfc + __real__ anddc;
     124  }
     125  
     126  int
     127  main ()
     128  {
     129    for (int i = 0; i < N; ++i)
     130      {
     131        rf[i] = 0;
     132        rd[i] = 0;
     133        rcf[i] = 0;
     134        rcd[i] = 0;
     135      }
     136  
     137    if (reduction_or () != 0)
     138      __builtin_abort ();
     139    if (reduction_or_teams () != 0)
     140      __builtin_abort ();
     141    if (reduction_and () != 0)
     142      __builtin_abort ();
     143    if (reduction_and_teams () != 0)
     144      __builtin_abort ();
     145  
     146    rf[10] = 1;
     147    rd[15] = 1;
     148    rcf[10] = 1;
     149    rcd[15] = 1;
     150  
     151    if (reduction_or () != 4)
     152      __builtin_abort ();
     153    if (reduction_or_teams () != 4)
     154      __builtin_abort ();
     155    if (reduction_and () != 0)
     156      __builtin_abort ();
     157    if (reduction_and_teams () != 0)
     158      __builtin_abort ();
     159  
     160    for (int i = 0; i < N; ++i)
     161      {
     162        rf[i] = 1;
     163        rd[i] = 1;
     164        rcf[i] = 1;
     165        rcd[i] = 1;
     166      }
     167  
     168    if (reduction_or () != 4)
     169      __builtin_abort ();
     170    if (reduction_or_teams () != 4)
     171      __builtin_abort ();
     172    if (reduction_and () != 4)
     173      __builtin_abort ();
     174    if (reduction_and_teams () != 4)
     175      __builtin_abort ();
     176  
     177    rf[10] = 0;
     178    rd[15] = 0;
     179    rcf[10] = 0;
     180    rcd[15] = 0;
     181  
     182    if (reduction_or () != 4)
     183      __builtin_abort ();
     184    if (reduction_or_teams () != 4)
     185      __builtin_abort ();
     186    if (reduction_and () != 0)
     187      __builtin_abort ();
     188    if (reduction_and_teams () != 0)
     189      __builtin_abort ();
     190  
     191    return 0;
     192  }