maxloc1_8_i2.c 13 KB
Newer Older
1
/* Implementation of the MAXLOC intrinsic
2
   Copyright (C) 2002-2014 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <stdlib.h>
#include <assert.h>
#include <limits.h>


#if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_8)


extern void maxloc1_8_i2 (gfc_array_i8 * const restrict, 
	gfc_array_i2 * const restrict, const index_type * const restrict);
export_proto(maxloc1_8_i2);

void
maxloc1_8_i2 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
	const index_type * const restrict pdim)
{
  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_8 * restrict dest;
  index_type rank;
  index_type n;
  index_type len;
  index_type delta;
  index_type dim;
55
  int continue_loop;
56 57 58 59 60

  /* Make dim zero based to avoid confusion.  */
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

61
  len = GFC_DESCRIPTOR_EXTENT(array,dim);
62 63
  if (len < 0)
    len = 0;
64
  delta = GFC_DESCRIPTOR_STRIDE(array,dim);
65 66 67

  for (n = 0; n < dim; n++)
    {
68 69
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
70 71 72 73 74 75

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

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

83
  if (retarray->base_addr == NULL)
84
    {
85
      size_t alloc_size, str;
86 87

      for (n = 0; n < rank; n++)
88 89
	{
	  if (n == 0)
90
	    str = 1;
91 92
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
93 94 95

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

96
	}
97 98 99 100

      retarray->offset = 0;
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;

101
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
102

103
      retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
104 105 106
      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
107
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
108
	  return;
109

110 111 112 113 114
	}
    }
  else
    {
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
115
	runtime_error ("rank of return array incorrect in"
116 117 118
		       " MAXLOC intrinsic: is %ld, should be %ld",
		       (long int) (GFC_DESCRIPTOR_RANK (retarray)),
		       (long int) rank);
119

120
      if (unlikely (compile_options.bounds_check))
Thomas Koenig committed
121 122
	bounds_ifunction_return ((array_t *) retarray, extent,
				 "return value", "MAXLOC");
123 124 125 126 127
    }

  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
128
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
129
      if (extent[n] <= 0)
130
	return;
131 132
    }

133 134
  base = array->base_addr;
  dest = retarray->base_addr;
135

136 137
  continue_loop = 1;
  while (continue_loop)
138 139 140 141 142 143
    {
      const GFC_INTEGER_2 * restrict src;
      GFC_INTEGER_8 result;
      src = base;
      {

144 145 146 147 148 149 150 151
	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)
152 153 154 155 156 157
	  *dest = 0;
	else
	  {
	    for (n = 0; n < len; n++, src += delta)
	      {

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#if defined (GFC_INTEGER_2_QUIET_NAN)
		if (*src >= maxval)
		  {
		    maxval = *src;
		    result = (GFC_INTEGER_8)n + 1;
		    break;
		  }
	      }
	    for (; n < len; n++, src += delta)
	      {
#endif
		if (*src > maxval)
		  {
		    maxval = *src;
		    result = (GFC_INTEGER_8)n + 1;
		  }
	      }
175
	    
176 177 178 179 180 181 182 183 184
	    *dest = result;
	  }
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
185 186 187 188 189 190 191 192 193 194 195 196
	{
	  /* 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++;
	  if (n == rank)
	    {
	      /* Break out of the look.  */
197 198
	      continue_loop = 0;
	      break;
199 200 201 202 203 204 205 206
	    }
	  else
	    {
	      count[n]++;
	      base += sstride[n];
	      dest += dstride[n];
	    }
	}
207 208 209 210 211 212
    }
}


extern void mmaxloc1_8_i2 (gfc_array_i8 * const restrict, 
	gfc_array_i2 * const restrict, const index_type * const restrict,
213
	gfc_array_l1 * const restrict);
214 215 216 217 218 219
export_proto(mmaxloc1_8_i2);

void
mmaxloc1_8_i2 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
	const index_type * const restrict pdim, 
220
	gfc_array_l1 * const restrict mask)
221 222 223 224 225 226 227 228
{
  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_8 * restrict dest;
  const GFC_INTEGER_2 * restrict base;
229
  const GFC_LOGICAL_1 * restrict mbase;
230 231 232 233 234 235
  int rank;
  int dim;
  index_type n;
  index_type len;
  index_type delta;
  index_type mdelta;
236
  int mask_kind;
237 238 239 240

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

241
  len = GFC_DESCRIPTOR_EXTENT(array,dim);
242 243
  if (len <= 0)
    return;
244

245
  mbase = mask->base_addr;
246 247 248 249 250 251 252 253 254 255 256 257

  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");

258 259
  delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
260 261 262

  for (n = 0; n < dim; n++)
    {
263 264 265
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
      mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
266 267 268 269 270 271 272

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

    }
  for (n = dim; n < rank; n++)
    {
273 274 275
      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);
276 277 278 279 280

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

281
  if (retarray->base_addr == NULL)
282
    {
283
      size_t alloc_size, str;
284 285

      for (n = 0; n < rank; n++)
286 287 288 289 290
	{
	  if (n == 0)
	    str = 1;
	  else
	    str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
291 292 293

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

294
	}
295

296
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
297 298 299 300 301 302 303

      retarray->offset = 0;
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
304
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
305 306 307
	  return;
	}
      else
308
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
309 310 311 312 313

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

316
      if (unlikely (compile_options.bounds_check))
317
	{
Thomas Koenig committed
318 319 320 321
	  bounds_ifunction_return ((array_t *) retarray, extent,
				   "return value", "MAXLOC");
	  bounds_equal_extents ((array_t *) mask, (array_t *) array,
	  			"MASK argument", "MAXLOC");
322
	}
323 324 325 326 327
    }

  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
328
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
329
      if (extent[n] <= 0)
330
	return;
331 332
    }

333 334
  dest = retarray->base_addr;
  base = array->base_addr;
335 336 337 338

  while (base)
    {
      const GFC_INTEGER_2 * restrict src;
339
      const GFC_LOGICAL_1 * restrict msrc;
340 341 342 343 344
      GFC_INTEGER_8 result;
      src = base;
      msrc = mbase;
      {

345 346 347 348 349 350 351 352 353 354
	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_8 result2 = 0;
#endif
	result = 0;
355
	for (n = 0; n < len; n++, src += delta, msrc += mdelta)
356 357
	  {

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
		if (*msrc)
		  {
#if defined (GFC_INTEGER_2_QUIET_NAN)
		    if (!result2)
		      result2 = (GFC_INTEGER_8)n + 1;
		    if (*src >= maxval)
#endif
		      {
			maxval = *src;
			result = (GFC_INTEGER_8)n + 1;
			break;
		      }
		  }
	      }
#if defined (GFC_INTEGER_2_QUIET_NAN)
	    if (unlikely (n >= len))
	      result = result2;
	    else
#endif
	    for (; n < len; n++, src += delta, msrc += mdelta)
	      {
		if (*msrc && *src > maxval)
		  {
		    maxval = *src;
		    result = (GFC_INTEGER_8)n + 1;
		  }
384
	  }
385
	*dest = result;
386 387 388 389 390 391 392 393
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      mbase += mstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	{
	  /* 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++;
	  if (n == rank)
	    {
	      /* Break out of the look.  */
	      base = NULL;
	      break;
	    }
	  else
	    {
	      count[n]++;
	      base += sstride[n];
	      mbase += mstride[n];
	      dest += dstride[n];
	    }
	}
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    }
}


extern void smaxloc1_8_i2 (gfc_array_i8 * const restrict, 
	gfc_array_i2 * const restrict, const index_type * const restrict,
	GFC_LOGICAL_4 *);
export_proto(smaxloc1_8_i2);

void
smaxloc1_8_i2 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i2 * const restrict array, 
	const index_type * const restrict pdim, 
	GFC_LOGICAL_4 * mask)
{
433 434 435 436
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_8 * restrict dest;
437 438
  index_type rank;
  index_type n;
439 440
  index_type dim;

441 442 443 444 445 446

  if (*mask)
    {
      maxloc1_8_i2 (retarray, array, pdim);
      return;
    }
447 448 449 450 451 452
  /* Make dim zero based to avoid confusion.  */
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

  for (n = 0; n < dim; n++)
    {
453
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
454 455 456 457 458 459 460 461

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

  for (n = dim; n < rank; n++)
    {
      extent[n] =
462
	GFC_DESCRIPTOR_EXTENT(array,n + 1);
463 464

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

468
  if (retarray->base_addr == NULL)
469
    {
470
      size_t alloc_size, str;
471 472

      for (n = 0; n < rank; n++)
473 474 475 476 477
	{
	  if (n == 0)
	    str = 1;
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
478 479 480

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

481
	}
482

483
      retarray->offset = 0;
484 485
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;

486
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
487 488 489 490

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
491
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
492 493 494
	  return;
	}
      else
495
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
496 497 498
    }
  else
    {
499 500 501 502 503 504
      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);

505
      if (unlikely (compile_options.bounds_check))
506
	{
507 508 509
	  for (n=0; n < rank; n++)
	    {
	      index_type ret_extent;
510

511
	      ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
512 513 514 515 516 517
	      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]);
	    }
518 519
	}
    }
520

521 522 523
  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
524
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
525 526
    }

527
  dest = retarray->base_addr;
528 529 530 531 532 533 534 535

  while(1)
    {
      *dest = 0;
      count[0]++;
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
536
	{
537
	  /* When we get to the end of a dimension, reset it and increment
538 539 540 541 542 543 544
	     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++;
	  if (n == rank)
545
	    return;
546 547 548 549 550
	  else
	    {
	      count[n]++;
	      dest += dstride[n];
	    }
551 552
      	}
    }
553 554 555
}

#endif