maxloc1_4_r4.c 12.9 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

Libgfortran is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public
9
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

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
15
GNU General Public License for more details.
16

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
#include <stdlib.h>
#include <assert.h>
#include <limits.h>

31

32 33 34
#if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_INTEGER_4)


Janne Blomqvist committed
35 36
extern void maxloc1_4_r4 (gfc_array_i4 * const restrict, 
	gfc_array_r4 * const restrict, const index_type * const restrict);
37
export_proto(maxloc1_4_r4);
38

39
void
Janne Blomqvist committed
40 41 42
maxloc1_4_r4 (gfc_array_i4 * const restrict retarray, 
	gfc_array_r4 * const restrict array, 
	const index_type * const restrict pdim)
43
{
44 45 46 47
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type sstride[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
Janne Blomqvist committed
48 49
  const GFC_REAL_4 * restrict base;
  GFC_INTEGER_4 * restrict dest;
50 51 52 53 54
  index_type rank;
  index_type n;
  index_type len;
  index_type delta;
  index_type dim;
55
  int continue_loop;
56 57 58 59

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

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

      if (extent[n] < 0)
	extent[n] = 0;
73 74 75
    }
  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

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

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
      retarray->offset = 0;
99
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
100

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

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

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

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

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

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

137 138
  continue_loop = 1;
  while (continue_loop)
139
    {
Janne Blomqvist committed
140
      const GFC_REAL_4 * restrict src;
141 142 143 144
      GFC_INTEGER_4 result;
      src = base;
      {

145 146 147 148 149 150 151 152
	GFC_REAL_4 maxval;
#if defined (GFC_REAL_4_INFINITY)
	maxval = -GFC_REAL_4_INFINITY;
#else
	maxval = -GFC_REAL_4_HUGE;
#endif
	result = 1;
	if (len <= 0)
153 154 155 156 157 158
	  *dest = 0;
	else
	  {
	    for (n = 0; n < len; n++, src += delta)
	      {

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

211

Janne Blomqvist committed
212 213
extern void mmaxloc1_4_r4 (gfc_array_i4 * const restrict, 
	gfc_array_r4 * const restrict, const index_type * const restrict,
214
	gfc_array_l1 * const restrict);
215
export_proto(mmaxloc1_4_r4);
216

217
void
Janne Blomqvist committed
218 219 220
mmaxloc1_4_r4 (gfc_array_i4 * const restrict retarray, 
	gfc_array_r4 * const restrict array, 
	const index_type * const restrict pdim, 
221
	gfc_array_l1 * const restrict mask)
222
{
223 224 225 226 227
  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];
Janne Blomqvist committed
228 229
  GFC_INTEGER_4 * restrict dest;
  const GFC_REAL_4 * restrict base;
230
  const GFC_LOGICAL_1 * restrict mbase;
231 232 233 234 235 236
  int rank;
  int dim;
  index_type n;
  index_type len;
  index_type delta;
  index_type mdelta;
237
  int mask_kind;
238 239 240

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

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

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

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

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

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

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

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

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

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

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

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

295
	}
296

297
      alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
298 299
    		   * extent[rank-1];

300
      retarray->offset = 0;
301
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
302 303 304 305

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

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

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

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

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

  while (base)
    {
Janne Blomqvist committed
340
      const GFC_REAL_4 * restrict src;
341
      const GFC_LOGICAL_1 * restrict msrc;
342 343 344 345 346
      GFC_INTEGER_4 result;
      src = base;
      msrc = mbase;
      {

347 348 349 350 351 352 353 354 355 356
	GFC_REAL_4 maxval;
#if defined (GFC_REAL_4_INFINITY)
	maxval = -GFC_REAL_4_INFINITY;
#else
	maxval = -GFC_REAL_4_HUGE;
#endif
#if defined (GFC_REAL_4_QUIET_NAN)
	GFC_INTEGER_4 result2 = 0;
#endif
	result = 0;
357
	for (n = 0; n < len; n++, src += delta, msrc += mdelta)
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 384 385
		if (*msrc)
		  {
#if defined (GFC_REAL_4_QUIET_NAN)
		    if (!result2)
		      result2 = (GFC_INTEGER_4)n + 1;
		    if (*src >= maxval)
#endif
		      {
			maxval = *src;
			result = (GFC_INTEGER_4)n + 1;
			break;
		      }
		  }
	      }
#if defined (GFC_REAL_4_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_4)n + 1;
		  }
386
	  }
387
	*dest = result;
388 389 390 391 392 393 394 395
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      mbase += mstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	{
	  /* 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];
	    }
	}
420 421 422
    }
}

423 424 425 426 427 428 429 430 431 432 433 434

extern void smaxloc1_4_r4 (gfc_array_i4 * const restrict, 
	gfc_array_r4 * const restrict, const index_type * const restrict,
	GFC_LOGICAL_4 *);
export_proto(smaxloc1_4_r4);

void
smaxloc1_4_r4 (gfc_array_i4 * const restrict retarray, 
	gfc_array_r4 * const restrict array, 
	const index_type * const restrict pdim, 
	GFC_LOGICAL_4 * mask)
{
435 436 437 438
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_4 * restrict dest;
439 440
  index_type rank;
  index_type n;
441 442
  index_type dim;

443 444 445 446 447 448

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

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

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

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

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

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

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

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

483
	}
484

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

488
      alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
489 490 491 492 493
    		   * extent[rank-1];

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

508
      if (unlikely (compile_options.bounds_check))
509
	{
510 511 512
	  for (n=0; n < rank; n++)
	    {
	      index_type ret_extent;
513

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

524 525 526
  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
527
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
528 529
    }

530
  dest = retarray->base_addr;
531 532 533 534 535 536 537 538

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

558
#endif