maxloc1_16_i2.c 14.3 KB
Newer Older
1
/* Implementation of the MAXLOC intrinsic
2
   Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 4
   Contributed by Paul Brook <paul@nowt.org>

5
This file is part of the GNU Fortran runtime library (libgfortran).
6 7 8 9

Libgfortran is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
10
version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16

Libgfortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

17 18 19 20 21 22 23 24
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
25

26
#include "libgfortran.h"
27
#include <assert.h>
28 29 30 31


#if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_16)

32 33
#define HAVE_BACK_ARG 1

34 35

extern void maxloc1_16_i2 (gfc_array_i16 * const restrict, 
36
	gfc_array_i2 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
37 38 39 40 41
export_proto(maxloc1_16_i2);

void
maxloc1_16_i2 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
42
	const index_type * const restrict pdim, GFC_LOGICAL_4 back)
43 44 45 46 47 48 49 50 51 52 53 54
{
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type sstride[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  const GFC_INTEGER_2 * restrict base;
  GFC_INTEGER_16 * restrict dest;
  index_type rank;
  index_type n;
  index_type len;
  index_type delta;
  index_type dim;
55
  int continue_loop;
56 57 58

  /* Make dim zero based to avoid confusion.  */
  rank = GFC_DESCRIPTOR_RANK (array) - 1;
59 60 61 62 63 64 65 66
  dim = (*pdim) - 1;

  if (unlikely (dim < 0 || dim > rank))
    {
      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
 		     "is %ld, should be between 1 and %ld",
		     (long int) dim + 1, (long int) rank + 1);
    }
67

68
  len = GFC_DESCRIPTOR_EXTENT(array,dim);
69 70
  if (len < 0)
    len = 0;
71
  delta = GFC_DESCRIPTOR_STRIDE(array,dim);
72 73 74

  for (n = 0; n < dim; n++)
    {
75 76
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
77 78 79 80 81 82

      if (extent[n] < 0)
	extent[n] = 0;
    }
  for (n = dim; n < rank; n++)
    {
83 84
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
85 86 87 88 89

      if (extent[n] < 0)
	extent[n] = 0;
    }

90
  if (retarray->base_addr == NULL)
91
    {
92
      size_t alloc_size, str;
93 94

      for (n = 0; n < rank; n++)
95 96
	{
	  if (n == 0)
97
	    str = 1;
98 99
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
100 101 102

	  GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);

103
	}
104 105

      retarray->offset = 0;
106
      retarray->dtype.rank = rank;
107

108
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
109

110
      retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
111 112 113
      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
114
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
115
	  return;
116

117 118 119 120 121
	}
    }
  else
    {
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
122
	runtime_error ("rank of return array incorrect in"
123 124 125
		       " MAXLOC intrinsic: is %ld, should be %ld",
		       (long int) (GFC_DESCRIPTOR_RANK (retarray)),
		       (long int) rank);
126

127
      if (unlikely (compile_options.bounds_check))
Thomas Koenig committed
128 129
	bounds_ifunction_return ((array_t *) retarray, extent,
				 "return value", "MAXLOC");
130 131 132 133 134
    }

  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
135
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
136
      if (extent[n] <= 0)
137
	return;
138 139
    }

140 141
  base = array->base_addr;
  dest = retarray->base_addr;
142

143 144
  continue_loop = 1;
  while (continue_loop)
145 146 147 148 149 150
    {
      const GFC_INTEGER_2 * restrict src;
      GFC_INTEGER_16 result;
      src = base;
      {

151 152 153 154 155 156 157 158
	GFC_INTEGER_2 maxval;
#if defined (GFC_INTEGER_2_INFINITY)
	maxval = -GFC_INTEGER_2_INFINITY;
#else
	maxval = (-GFC_INTEGER_2_HUGE-1);
#endif
	result = 1;
	if (len <= 0)
159 160 161
	  *dest = 0;
	else
	  {
162
#if ! defined HAVE_BACK_ARG
163 164
	    for (n = 0; n < len; n++, src += delta)
	      {
165
#endif
166

167
#if defined (GFC_INTEGER_2_QUIET_NAN)
168 169
     	     for (n = 0; n < len; n++, src += delta)
	       {
170 171 172 173 174 175 176
		if (*src >= maxval)
		  {
		    maxval = *src;
		    result = (GFC_INTEGER_16)n + 1;
		    break;
		  }
	      }
177 178 179
#else
	    n = 0;
#endif
180 181
	    for (; n < len; n++, src += delta)
	      {
182
		if (back ? *src >= maxval : *src > maxval)
183 184 185 186 187
		  {
		    maxval = *src;
		    result = (GFC_INTEGER_16)n + 1;
		  }
	      }
188
	    
189 190 191 192 193 194 195 196 197
	    *dest = result;
	  }
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
198 199 200 201 202 203 204 205 206
	{
	  /* When we get to the end of a dimension, reset it and increment
	     the next dimension.  */
	  count[n] = 0;
	  /* We could precalculate these products, but this is a less
	     frequently used path so probably not worth it.  */
	  base -= sstride[n] * extent[n];
	  dest -= dstride[n] * extent[n];
	  n++;
207
	  if (n >= rank)
208
	    {
209
	      /* Break out of the loop.  */
210 211
	      continue_loop = 0;
	      break;
212 213 214 215 216 217 218 219
	    }
	  else
	    {
	      count[n]++;
	      base += sstride[n];
	      dest += dstride[n];
	    }
	}
220 221 222 223 224 225
    }
}


extern void mmaxloc1_16_i2 (gfc_array_i16 * const restrict, 
	gfc_array_i2 * const restrict, const index_type * const restrict,
226
	gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
227 228 229 230 231 232
export_proto(mmaxloc1_16_i2);

void
mmaxloc1_16_i2 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
	const index_type * const restrict pdim, 
233
	gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
234 235 236 237 238 239 240 241
{
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type sstride[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  index_type mstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_16 * restrict dest;
  const GFC_INTEGER_2 * restrict base;
242
  const GFC_LOGICAL_1 * restrict mbase;
243 244
  index_type rank;
  index_type dim;
245 246 247 248
  index_type n;
  index_type len;
  index_type delta;
  index_type mdelta;
249
  int mask_kind;
250

251 252 253 254 255 256 257 258 259 260
  if (mask == NULL)
    {
#ifdef HAVE_BACK_ARG
      maxloc1_16_i2 (retarray, array, pdim, back);
#else
      maxloc1_16_i2 (retarray, array, pdim);
#endif
      return;
    }

261 262 263
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

264 265 266 267 268 269 270 271

  if (unlikely (dim < 0 || dim > rank))
    {
      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
 		     "is %ld, should be between 1 and %ld",
		     (long int) dim + 1, (long int) rank + 1);
    }

272
  len = GFC_DESCRIPTOR_EXTENT(array,dim);
273 274
  if (len <= 0)
    return;
275

276
  mbase = mask->base_addr;
277 278 279 280 281 282 283 284 285 286 287 288

  mask_kind = GFC_DESCRIPTOR_SIZE (mask);

  if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
#ifdef HAVE_GFC_LOGICAL_16
      || mask_kind == 16
#endif
      )
    mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  else
    runtime_error ("Funny sized logical array");

289 290
  delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
291 292 293

  for (n = 0; n < dim; n++)
    {
294 295 296
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
      mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
297 298 299 300 301 302 303

      if (extent[n] < 0)
	extent[n] = 0;

    }
  for (n = dim; n < rank; n++)
    {
304 305 306
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
      mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
307 308 309 310 311

      if (extent[n] < 0)
	extent[n] = 0;
    }

312
  if (retarray->base_addr == NULL)
313
    {
314
      size_t alloc_size, str;
315 316

      for (n = 0; n < rank; n++)
317 318 319 320 321
	{
	  if (n == 0)
	    str = 1;
	  else
	    str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
322 323 324

	  GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);

325
	}
326

327
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
328 329

      retarray->offset = 0;
330
      retarray->dtype.rank = rank;
331 332 333 334

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
335
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
336 337 338
	  return;
	}
      else
339
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
340 341 342 343 344

    }
  else
    {
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
345 346
	runtime_error ("rank of return array incorrect in MAXLOC intrinsic");

347
      if (unlikely (compile_options.bounds_check))
348
	{
Thomas Koenig committed
349 350 351 352
	  bounds_ifunction_return ((array_t *) retarray, extent,
				   "return value", "MAXLOC");
	  bounds_equal_extents ((array_t *) mask, (array_t *) array,
	  			"MASK argument", "MAXLOC");
353
	}
354 355 356 357 358
    }

  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
359
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
360
      if (extent[n] <= 0)
361
	return;
362 363
    }

364 365
  dest = retarray->base_addr;
  base = array->base_addr;
366 367 368 369

  while (base)
    {
      const GFC_INTEGER_2 * restrict src;
370
      const GFC_LOGICAL_1 * restrict msrc;
371 372 373 374 375
      GFC_INTEGER_16 result;
      src = base;
      msrc = mbase;
      {

376 377 378 379 380 381 382 383 384 385
	GFC_INTEGER_2 maxval;
#if defined (GFC_INTEGER_2_INFINITY)
	maxval = -GFC_INTEGER_2_INFINITY;
#else
	maxval = (-GFC_INTEGER_2_HUGE-1);
#endif
#if defined (GFC_INTEGER_2_QUIET_NAN)
	GFC_INTEGER_16 result2 = 0;
#endif
	result = 0;
386
	for (n = 0; n < len; n++, src += delta, msrc += mdelta)
387 388
	  {

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
		if (*msrc)
		  {
#if defined (GFC_INTEGER_2_QUIET_NAN)
		    if (!result2)
		      result2 = (GFC_INTEGER_16)n + 1;
		    if (*src >= maxval)
#endif
		      {
			maxval = *src;
			result = (GFC_INTEGER_16)n + 1;
			break;
		      }
		  }
	      }
#if defined (GFC_INTEGER_2_QUIET_NAN)
	    if (unlikely (n >= len))
	      result = result2;
	    else
#endif
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	    if (back)
	      for (; n < len; n++, src += delta, msrc += mdelta)
	      	{
		  if (*msrc && unlikely (*src >= maxval))
		    {
		      maxval = *src;
		      result = (GFC_INTEGER_16)n + 1;
		    }
		}
	    else
	      for (; n < len; n++, src += delta, msrc += mdelta)
	        {
		  if (*msrc && unlikely (*src > maxval))
		    {
		      maxval = *src;
		      result = (GFC_INTEGER_16)n + 1;
		    }
425
	  }
426
	*dest = result;
427 428 429 430 431 432 433 434
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      mbase += mstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
435 436 437 438 439 440 441 442 443 444
	{
	  /* When we get to the end of a dimension, reset it and increment
	     the next dimension.  */
	  count[n] = 0;
	  /* We could precalculate these products, but this is a less
	     frequently used path so probably not worth it.  */
	  base -= sstride[n] * extent[n];
	  mbase -= mstride[n] * extent[n];
	  dest -= dstride[n] * extent[n];
	  n++;
445
	  if (n >= rank)
446
	    {
447
	      /* Break out of the loop.  */
448 449 450 451 452 453 454 455 456 457 458
	      base = NULL;
	      break;
	    }
	  else
	    {
	      count[n]++;
	      base += sstride[n];
	      mbase += mstride[n];
	      dest += dstride[n];
	    }
	}
459 460 461 462 463 464
    }
}


extern void smaxloc1_16_i2 (gfc_array_i16 * const restrict, 
	gfc_array_i2 * const restrict, const index_type * const restrict,
465
	GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
466 467 468 469 470 471
export_proto(smaxloc1_16_i2);

void
smaxloc1_16_i2 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
	const index_type * const restrict pdim, 
472
	GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
473
{
474 475 476 477
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_16 * restrict dest;
478 479
  index_type rank;
  index_type n;
480 481
  index_type dim;

482

483
  if (mask == NULL || *mask)
484
    {
485 486 487
#ifdef HAVE_BACK_ARG
      maxloc1_16_i2 (retarray, array, pdim, back);
#else
488
      maxloc1_16_i2 (retarray, array, pdim);
489
#endif
490 491
      return;
    }
492 493 494 495
  /* Make dim zero based to avoid confusion.  */
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

496 497 498 499 500 501 502
  if (unlikely (dim < 0 || dim > rank))
    {
      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
 		     "is %ld, should be between 1 and %ld",
		     (long int) dim + 1, (long int) rank + 1);
    }

503 504
  for (n = 0; n < dim; n++)
    {
505
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
506 507 508 509 510 511 512 513

      if (extent[n] <= 0)
	extent[n] = 0;
    }

  for (n = dim; n < rank; n++)
    {
      extent[n] =
514
	GFC_DESCRIPTOR_EXTENT(array,n + 1);
515 516

      if (extent[n] <= 0)
517
	extent[n] = 0;
518
    }
519

520
  if (retarray->base_addr == NULL)
521
    {
522
      size_t alloc_size, str;
523 524

      for (n = 0; n < rank; n++)
525 526 527 528 529
	{
	  if (n == 0)
	    str = 1;
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
530 531 532

	  GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);

533
	}
534

535
      retarray->offset = 0;
536
      retarray->dtype.rank = rank;
537

538
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
539 540 541 542

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
543
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
544 545 546
	  return;
	}
      else
547
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
548 549 550
    }
  else
    {
551 552 553 554 555 556
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
	runtime_error ("rank of return array incorrect in"
		       " MAXLOC intrinsic: is %ld, should be %ld",
		       (long int) (GFC_DESCRIPTOR_RANK (retarray)),
		       (long int) rank);

557
      if (unlikely (compile_options.bounds_check))
558
	{
559 560 561
	  for (n=0; n < rank; n++)
	    {
	      index_type ret_extent;
562

563
	      ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
564 565 566 567 568 569
	      if (extent[n] != ret_extent)
		runtime_error ("Incorrect extent in return value of"
			       " MAXLOC intrinsic in dimension %ld:"
			       " is %ld, should be %ld", (long int) n + 1,
			       (long int) ret_extent, (long int) extent[n]);
	    }
570 571
	}
    }
572

573 574 575
  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
576
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
577 578
    }

579
  dest = retarray->base_addr;
580 581 582 583 584 585 586 587

  while(1)
    {
      *dest = 0;
      count[0]++;
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
588
	{
589
	  /* When we get to the end of a dimension, reset it and increment
590 591 592 593 594 595
	     the next dimension.  */
	  count[n] = 0;
	  /* We could precalculate these products, but this is a less
	     frequently used path so probably not worth it.  */
	  dest -= dstride[n] * extent[n];
	  n++;
596
	  if (n >= rank)
597
	    return;
598 599 600 601 602
	  else
	    {
	      count[n]++;
	      dest += dstride[n];
	    }
603 604
      	}
    }
605 606 607
}

#endif