minloc1_8_i1.c 13 KB
Newer Older
1
/* Implementation of the MINLOC intrinsic
2
   Copyright 2002, 2007, 2009, 2010 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_1) && defined (HAVE_GFC_INTEGER_8)


extern void minloc1_8_i1 (gfc_array_i8 * const restrict, 
	gfc_array_i1 * const restrict, const index_type * const restrict);
export_proto(minloc1_8_i1);

void
minloc1_8_i1 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i1 * 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_1 * 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 = sizeof (GFC_INTEGER_8) * 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
		       " MINLOC 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", "MINLOC");
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 140 141 142 143 144
    {
      const GFC_INTEGER_1 * restrict src;
      GFC_INTEGER_8 result;
      src = base;
      {

145 146 147 148 149 150 151 152
	GFC_INTEGER_1 minval;
#if defined (GFC_INTEGER_1_INFINITY)
	minval = GFC_INTEGER_1_INFINITY;
#else
	minval = GFC_INTEGER_1_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_INTEGER_1_QUIET_NAN)
		if (*src <= minval)
		  {
		    minval = *src;
		    result = (GFC_INTEGER_8)n + 1;
		    break;
		  }
	      }
	    for (; n < len; n++, src += delta)
	      {
#endif
		if (*src < minval)
		  {
		    minval = *src;
		    result = (GFC_INTEGER_8)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 212 213
    }
}


extern void mminloc1_8_i1 (gfc_array_i8 * const restrict, 
	gfc_array_i1 * const restrict, const index_type * const restrict,
214
	gfc_array_l1 * const restrict);
215 216 217 218 219 220
export_proto(mminloc1_8_i1);

void
mminloc1_8_i1 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i1 * const restrict array, 
	const index_type * const restrict pdim, 
221
	gfc_array_l1 * const restrict mask)
222 223 224 225 226 227 228 229
{
  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_1 * 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 241

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

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 271 272 273

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

    }
  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 280 281

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

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_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
298 299 300 301 302 303 304 305
    		   * extent[rank-1];

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

      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 MINLOC 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", "MINLOC");
	  bounds_equal_extents ((array_t *) mask, (array_t *) array,
	  			"MASK argument", "MINLOC");
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 340

  while (base)
    {
      const GFC_INTEGER_1 * restrict src;
341
      const GFC_LOGICAL_1 * restrict msrc;
342 343 344 345 346
      GFC_INTEGER_8 result;
      src = base;
      msrc = mbase;
      {

347 348 349 350 351 352 353 354 355 356 357
	GFC_INTEGER_1 minval;
#if defined (GFC_INTEGER_1_INFINITY)
	minval = GFC_INTEGER_1_INFINITY;
#else
	minval = GFC_INTEGER_1_HUGE;
#endif
#if defined (GFC_INTEGER_1_QUIET_NAN)
	GFC_INTEGER_8 result2 = 0;
#endif
	result = 0;
	if (len <= 0)
358 359 360 361 362 363
	  *dest = 0;
	else
	  {
	    for (n = 0; n < len; n++, src += delta, msrc += mdelta)
	      {

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


extern void sminloc1_8_i1 (gfc_array_i8 * const restrict, 
	gfc_array_i1 * const restrict, const index_type * const restrict,
	GFC_LOGICAL_4 *);
export_proto(sminloc1_8_i1);

void
sminloc1_8_i1 (gfc_array_i8 * const restrict retarray, 
	gfc_array_i1 * const restrict array, 
	const index_type * const restrict pdim, 
	GFC_LOGICAL_4 * mask)
{
440 441 442 443
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_8 * restrict dest;
444 445
  index_type rank;
  index_type n;
446 447
  index_type dim;

448 449 450 451 452 453

  if (*mask)
    {
      minloc1_8_i1 (retarray, array, pdim);
      return;
    }
454 455 456 457 458 459
  /* Make dim zero based to avoid confusion.  */
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

  for (n = 0; n < dim; n++)
    {
460
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
461 462 463 464 465 466 467 468

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

  for (n = dim; n < rank; n++)
    {
      extent[n] =
469
	GFC_DESCRIPTOR_EXTENT(array,n + 1);
470 471

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

475
  if (retarray->base_addr == NULL)
476
    {
477
      size_t alloc_size, str;
478 479

      for (n = 0; n < rank; n++)
480 481 482 483 484
	{
	  if (n == 0)
	    str = 1;
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
485 486 487

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

488
	}
489

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

493
      alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
494 495 496 497 498
    		   * extent[rank-1];

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
499
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
500 501 502
	  return;
	}
      else
503
	retarray->base_addr = xmalloc (alloc_size);
504 505 506
    }
  else
    {
507 508 509 510 511 512
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
	runtime_error ("rank of return array incorrect in"
		       " MINLOC intrinsic: is %ld, should be %ld",
		       (long int) (GFC_DESCRIPTOR_RANK (retarray)),
		       (long int) rank);

513
      if (unlikely (compile_options.bounds_check))
514
	{
515 516 517
	  for (n=0; n < rank; n++)
	    {
	      index_type ret_extent;
518

519
	      ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
520 521 522 523 524 525
	      if (extent[n] != ret_extent)
		runtime_error ("Incorrect extent in return value of"
			       " MINLOC intrinsic in dimension %ld:"
			       " is %ld, should be %ld", (long int) n + 1,
			       (long int) ret_extent, (long int) extent[n]);
	    }
526 527
	}
    }
528

529 530 531
  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
532
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
533 534
    }

535
  dest = retarray->base_addr;
536 537 538 539 540 541 542 543

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

#endif