sum_i16.c 11.8 KB
Newer Older
1
/* Implementation of the SUM intrinsic
Jakub Jelinek committed
2
   Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 4 5 6 7 8 9
   Contributed by Paul Brook <paul@nowt.org>

This file is part of the GNU Fortran 95 runtime library (libgfortran).

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


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


Janne Blomqvist committed
34 35
extern void sum_i16 (gfc_array_i16 * const restrict, 
	gfc_array_i16 * const restrict, const index_type * const restrict);
36 37 38
export_proto(sum_i16);

void
Janne Blomqvist committed
39 40 41
sum_i16 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i16 * const restrict array, 
	const index_type * const restrict pdim)
42 43 44 45 46
{
  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
47 48
  const GFC_INTEGER_16 * restrict base;
  GFC_INTEGER_16 * restrict dest;
49 50 51 52 53
  index_type rank;
  index_type n;
  index_type len;
  index_type delta;
  index_type dim;
54
  int continue_loop;
55 56 57 58 59

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

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

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

      if (extent[n] < 0)
	extent[n] = 0;
72 73 74
    }
  for (n = dim; n < rank; n++)
    {
75 76
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
77 78 79

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

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

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

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

95
	}
96 97 98

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

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

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

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

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

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

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

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

  result = 0;
144
	if (len <= 0)
145 146 147 148 149 150 151
	  *dest = 0;
	else
	  {
	    for (n = 0; n < len; n++, src += delta)
	      {

  result += *src;
152
	      }
153
	    
154 155 156 157 158 159 160 161 162
	    *dest = result;
	  }
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
163 164 165 166 167 168 169 170 171 172 173 174
	{
	  /* 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.  */
175 176
	      continue_loop = 0;
	      break;
177 178 179 180 181 182 183 184
	    }
	  else
	    {
	      count[n]++;
	      base += sstride[n];
	      dest += dstride[n];
	    }
	}
185 186 187 188
    }
}


Janne Blomqvist committed
189 190
extern void msum_i16 (gfc_array_i16 * const restrict, 
	gfc_array_i16 * const restrict, const index_type * const restrict,
191
	gfc_array_l1 * const restrict);
192 193 194
export_proto(msum_i16);

void
Janne Blomqvist committed
195 196 197
msum_i16 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i16 * const restrict array, 
	const index_type * const restrict pdim, 
198
	gfc_array_l1 * const restrict mask)
199 200 201 202 203 204
{
  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
205 206
  GFC_INTEGER_16 * restrict dest;
  const GFC_INTEGER_16 * restrict base;
207
  const GFC_LOGICAL_1 * restrict mbase;
208 209 210 211 212 213
  int rank;
  int dim;
  index_type n;
  index_type len;
  index_type delta;
  index_type mdelta;
214
  int mask_kind;
215 216 217 218

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

219
  len = GFC_DESCRIPTOR_EXTENT(array,dim);
220 221
  if (len <= 0)
    return;
222

223
  mbase = mask->base_addr;
224 225 226 227 228 229 230 231 232 233 234 235

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

236 237
  delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
238 239 240

  for (n = 0; n < dim; n++)
    {
241 242 243
      sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
      mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
244 245 246 247

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

248 249 250
    }
  for (n = dim; n < rank; n++)
    {
251 252 253
      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);
254 255 256

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

259
  if (retarray->base_addr == NULL)
260
    {
261
      size_t alloc_size, str;
262

263
      for (n = 0; n < rank; n++)
264 265 266 267 268
	{
	  if (n == 0)
	    str = 1;
	  else
	    str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
269 270 271

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

272
	}
273

274
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
275

276 277
      retarray->offset = 0;
      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
278 279 280 281

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
282
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
283 284 285
	  return;
	}
      else
286
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
287

288 289 290 291
    }
  else
    {
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
292 293
	runtime_error ("rank of return array incorrect in SUM intrinsic");

294
      if (unlikely (compile_options.bounds_check))
295
	{
Thomas Koenig committed
296 297 298 299
	  bounds_ifunction_return ((array_t *) retarray, extent,
				   "return value", "SUM");
	  bounds_equal_extents ((array_t *) mask, (array_t *) array,
	  			"MASK argument", "SUM");
300
	}
301 302 303 304 305
    }

  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
306
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
307
      if (extent[n] <= 0)
308
	return;
309 310
    }

311 312
  dest = retarray->base_addr;
  base = array->base_addr;
313 314 315

  while (base)
    {
Janne Blomqvist committed
316
      const GFC_INTEGER_16 * restrict src;
317
      const GFC_LOGICAL_1 * restrict msrc;
318 319 320 321 322 323
      GFC_INTEGER_16 result;
      src = base;
      msrc = mbase;
      {

  result = 0;
324
	for (n = 0; n < len; n++, src += delta, msrc += mdelta)
325 326 327 328 329
	  {

  if (*msrc)
    result += *src;
	  }
330
	*dest = result;
331 332 333 334 335 336 337 338
      }
      /* Advance to the next element.  */
      count[0]++;
      base += sstride[0];
      mbase += mstride[0];
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	{
	  /* 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];
	    }
	}
363 364 365
    }
}

366 367 368 369 370 371 372 373 374 375 376 377

extern void ssum_i16 (gfc_array_i16 * const restrict, 
	gfc_array_i16 * const restrict, const index_type * const restrict,
	GFC_LOGICAL_4 *);
export_proto(ssum_i16);

void
ssum_i16 (gfc_array_i16 * const restrict retarray, 
	gfc_array_i16 * const restrict array, 
	const index_type * const restrict pdim, 
	GFC_LOGICAL_4 * mask)
{
378 379 380 381
  index_type count[GFC_MAX_DIMENSIONS];
  index_type extent[GFC_MAX_DIMENSIONS];
  index_type dstride[GFC_MAX_DIMENSIONS];
  GFC_INTEGER_16 * restrict dest;
382 383
  index_type rank;
  index_type n;
384 385
  index_type dim;

386 387 388 389 390 391

  if (*mask)
    {
      sum_i16 (retarray, array, pdim);
      return;
    }
392 393 394 395 396 397
  /* Make dim zero based to avoid confusion.  */
  dim = (*pdim) - 1;
  rank = GFC_DESCRIPTOR_RANK (array) - 1;

  for (n = 0; n < dim; n++)
    {
398
      extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
399 400 401 402 403 404 405 406

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

  for (n = dim; n < rank; n++)
    {
      extent[n] =
407
	GFC_DESCRIPTOR_EXTENT(array,n + 1);
408 409

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

413
  if (retarray->base_addr == NULL)
414
    {
415
      size_t alloc_size, str;
416 417

      for (n = 0; n < rank; n++)
418 419 420 421 422
	{
	  if (n == 0)
	    str = 1;
	  else
	    str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
423 424 425

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

426
	}
427

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

431
      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
432 433 434 435

      if (alloc_size == 0)
	{
	  /* Make sure we have a zero-sized array.  */
436
	  GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
437 438 439
	  return;
	}
      else
440
	retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
441 442 443
    }
  else
    {
444 445 446 447 448 449
      if (rank != GFC_DESCRIPTOR_RANK (retarray))
	runtime_error ("rank of return array incorrect in"
		       " SUM intrinsic: is %ld, should be %ld",
		       (long int) (GFC_DESCRIPTOR_RANK (retarray)),
		       (long int) rank);

450
      if (unlikely (compile_options.bounds_check))
451
	{
452 453 454
	  for (n=0; n < rank; n++)
	    {
	      index_type ret_extent;
455

456
	      ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
457 458 459 460 461 462
	      if (extent[n] != ret_extent)
		runtime_error ("Incorrect extent in return value of"
			       " SUM intrinsic in dimension %ld:"
			       " is %ld, should be %ld", (long int) n + 1,
			       (long int) ret_extent, (long int) extent[n]);
	    }
463 464
	}
    }
465

466 467 468
  for (n = 0; n < rank; n++)
    {
      count[n] = 0;
469
      dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
470 471
    }

472
  dest = retarray->base_addr;
473 474 475 476 477 478 479 480

  while(1)
    {
      *dest = 0;
      count[0]++;
      dest += dstride[0];
      n = 0;
      while (count[n] == extent[n])
481
	{
482
	  /* When we get to the end of a dimension, reset it and increment
483 484 485 486 487 488 489
	     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)
490
	    return;
491 492 493 494 495
	  else
	    {
	      count[n]++;
	      dest += dstride[n];
	    }
496 497
      	}
    }
498 499
}

500
#endif