Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Implementation of the degree trignometric functions COSD, SIND, TAND.
   Copyright (C) 2020 Free Software Foundation, Inc.
   Contributed by Steven G. Kargl <kargl@gcc.gnu.org>
   and Fritz Reese <foreese@gcc.gnu.org>

This file is part of the GNU Fortran 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
version 3 of the License, or (at your option) any later version.

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.

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/>.  */


/*

This file is included from both the FE and the runtime library code.
Operations are generalized using GMP/MPFR functions. When included from
libgfortran, these should be overridden using macros which will use native
operations conforming to the same API. From the FE, the GMP/MPFR functions can
be used as-is.

The following macros are used and must be defined, unless listed as [optional]:

FTYPE
    Type name for the real-valued parameter.
    Variables of this type are constructed/destroyed using mpfr_init()
    and mpfr_clear.

RETTYPE
    Return type of the functions.

RETURN(x)
    Insert code to return a value.
    The parameter x is the result variable, which was also the input parameter.

ITYPE
    Type name for integer types.

SIND, COSD, TRIGD
    Names for the degree-valued trig functions defined by this module.

ENABLE_SIND, ENABLE_COSD, ENABLE_TAND
    Whether the degree-valued trig functions can be enabled.

ERROR_RETURN(f, k, x)
    If ENABLE_<xxx>D is not defined, this is substituted to assert an
    error condition for function f, kind k, and parameter x.
    The function argument is one of {sind, cosd, tand}.

ISFINITE(x)
    Whether x is a regular number or zero (not inf or NaN).

D2R(x)
    Convert x from radians to degrees.

SET_COSD30(x)
    Set x to COSD(30), or equivalently, SIND(60).

TINY_LITERAL [optional]
    Value subtracted from 1 to cause raise INEXACT for COSD(x) for x << 1.
    If not set, COSD(x) for x <= COSD_SMALL_LITERAL simply returns 1.

COSD_SMALL_LITERAL [optional]
    Value such that x <= COSD_SMALL_LITERAL implies COSD(x) = 1 to within the
    precision of FTYPE. If not set, this condition is not checked.

SIND_SMALL_LITERAL [optional]
    Value such that x <= SIND_SMALL_LITERAL implies SIND(x) = D2R(x) to within
    the precision of FTYPE. If not set, this condition is not checked.

*/


#ifdef SIND
/* Compute sind(x) = sin(x * pi / 180). */

RETTYPE
SIND (FTYPE x)
{
#ifdef ENABLE_SIND
  if (ISFINITE (x))
    {
      FTYPE s, one;

      /* sin(-x) = - sin(x).  */
      mpfr_init (s);
      mpfr_init_set_ui (one, 1, GFC_RND_MODE);
      mpfr_copysign (s, one, x, GFC_RND_MODE);
      mpfr_clear (one);

#ifdef SIND_SMALL_LITERAL
      /* sin(x) = x as x -> 0; but only for some precisions. */
      FTYPE ax;
      mpfr_init (ax);
      mpfr_abs (ax, x, GFC_RND_MODE);
      if (mpfr_cmp_ld (ax, SIND_SMALL_LITERAL) < 0)
	{
	  D2R (x);
	  mpfr_clear (ax);
	  return x;
	}

      mpfr_swap (x, ax);
      mpfr_clear (ax);

#else
      mpfr_abs (x, x, GFC_RND_MODE);
#endif /* SIND_SMALL_LITERAL */

      /* Reduce angle to x in [0,360].  */
      FTYPE period;
      mpfr_init_set_ui (period, 360, GFC_RND_MODE);
      mpfr_fmod (x, x, period, GFC_RND_MODE);
      mpfr_clear (period);

      /* Special cases with exact results.  */
      ITYPE n;
      mpz_init (n);
      if (mpfr_get_z (n, x, GFC_RND_MODE) == 0 && mpz_divisible_ui_p (n, 30))
	{
	  /* Flip sign for odd n*pi (x is % 360 so this is only for 180).
	     This respects sgn(sin(x)) = sgn(d/dx sin(x)) = sgn(cos(x)). */
	  if (mpz_divisible_ui_p (n, 180))
	    {
	      mpfr_set_ui (x, 0, GFC_RND_MODE);
	      if (mpz_cmp_ui (n, 180) == 0)
		mpfr_neg (s, s, GFC_RND_MODE);
	    }
	  else if (mpz_divisible_ui_p (n, 90))
	    mpfr_set_si (x, (mpz_cmp_ui (n, 90) == 0 ? 1 : -1), GFC_RND_MODE);
	  else if (mpz_divisible_ui_p (n, 60))
	    {
	      SET_COSD30 (x);
	      if (mpz_cmp_ui (n, 180) >= 0)
		mpfr_neg (x, x, GFC_RND_MODE);
	    }
	  else
	    mpfr_set_ld (x, (mpz_cmp_ui (n, 180) < 0 ? 0.5L : -0.5L),
			 GFC_RND_MODE);
	}

      /* Fold [0,360] into the range [0,45], and compute either SIN() or
         COS() depending on symmetry of shifting into the [0,45] range.  */
      else
	{
	  bool fold_cos = false;
	  if (mpfr_cmp_ui (x, 180) <= 0)
	    {
	      if (mpfr_cmp_ui (x, 90) <= 0)
		{
		  if (mpfr_cmp_ui (x, 45) > 0)
		    {
		      /* x = COS(D2R(90 - x)) */
		      mpfr_ui_sub (x, 90, x, GFC_RND_MODE);
		      fold_cos = true;
		    }
		}
	      else
		{
		  if (mpfr_cmp_ui (x, 135) <= 0)
		    {
		      mpfr_sub_ui (x, x, 90, GFC_RND_MODE);
		      fold_cos = true;
		    }
		  else
		    mpfr_ui_sub (x, 180, x, GFC_RND_MODE);
		}
	    }

	  else if (mpfr_cmp_ui (x, 270) <= 0)
	    {
	      if (mpfr_cmp_ui (x, 225) <= 0)
		mpfr_sub_ui (x, x, 180, GFC_RND_MODE);
	      else
		{
		  mpfr_ui_sub (x, 270, x, GFC_RND_MODE);
		  fold_cos = true;
		}
	      mpfr_neg (s, s, GFC_RND_MODE);
	    }

	  else
	    {
	      if (mpfr_cmp_ui (x, 315) <= 0)
		{
		  mpfr_sub_ui (x, x, 270, GFC_RND_MODE);
		  fold_cos = true;
		}
	      else
		mpfr_ui_sub (x, 360, x, GFC_RND_MODE);
	      mpfr_neg (s, s, GFC_RND_MODE);
	    }

	  D2R (x);

	  if (fold_cos)
	    mpfr_cos (x, x, GFC_RND_MODE);
	  else
	    mpfr_sin (x, x, GFC_RND_MODE);
	}

      mpfr_mul (x, x, s, GFC_RND_MODE);
      mpz_clear (n);
      mpfr_clear (s);
    }

  /* Return NaN for +-Inf and NaN and raise exception.  */
  else
    mpfr_sub (x, x, x, GFC_RND_MODE);

  RETURN (x);

#else
  ERROR_RETURN(sind, KIND, x);
#endif // ENABLE_SIND
}
#endif // SIND


#ifdef COSD
/* Compute cosd(x) = cos(x * pi / 180).  */

RETTYPE
COSD (FTYPE x)
{
#ifdef ENABLE_COSD
#if defined(TINY_LITERAL) && defined(COSD_SMALL_LITERAL)
  static const volatile FTYPE tiny = TINY_LITERAL;
#endif

  if (ISFINITE (x))
    {
#ifdef COSD_SMALL_LITERAL
      FTYPE ax;
      mpfr_init (ax);

      mpfr_abs (ax, x, GFC_RND_MODE);
      /* No spurious underflows!.  In radians, cos(x) = 1-x*x/2 as x -> 0.  */
      if (mpfr_cmp_ld (ax, COSD_SMALL_LITERAL) <= 0)
	{
	  mpfr_set_ui (x, 1, GFC_RND_MODE);
#ifdef TINY_LITERAL
	  /* Cause INEXACT.  */
	  if (!mpfr_zero_p (ax))
	    mpfr_sub_d (x, x, tiny, GFC_RND_MODE);
#endif

	  mpfr_clear (ax);
	  return x;
	}

      mpfr_swap (x, ax);
      mpfr_clear (ax);
#else
      mpfr_abs (x, x, GFC_RND_MODE);
#endif /* COSD_SMALL_LITERAL */

      /* Reduce angle to ax in [0,360].  */
      FTYPE period;
      mpfr_init_set_ui (period, 360, GFC_RND_MODE);
      mpfr_fmod (x, x, period, GFC_RND_MODE);
      mpfr_clear (period);

      /* Special cases with exact results.
         Return negative zero for cosd(270) for consistency with libm cos().  */
      ITYPE n;
      mpz_init (n);
      if (mpfr_get_z (n, x, GFC_RND_MODE) == 0 && mpz_divisible_ui_p (n, 30))
	{
	  if (mpz_divisible_ui_p (n, 180))
	    mpfr_set_si (x, (mpz_cmp_ui (n, 180) == 0 ? -1 : 1),
			 GFC_RND_MODE);
	  else if (mpz_divisible_ui_p (n, 90))
	    mpfr_set_zero (x, 0);
	  else if (mpz_divisible_ui_p (n, 60))
	    {
	      mpfr_set_ld (x, 0.5, GFC_RND_MODE);
	      if (mpz_cmp_ui (n, 60) != 0 && mpz_cmp_ui (n, 300) != 0)
		mpfr_neg (x, x, GFC_RND_MODE);
	    }
	  else
	    {
	      SET_COSD30 (x);
	      if (mpz_cmp_ui (n, 30) != 0 && mpz_cmp_ui (n, 330) != 0)
		mpfr_neg (x, x, GFC_RND_MODE);
	    }
	}

      /* Fold [0,360] into the range [0,45], and compute either SIN() or
         COS() depending on symmetry of shifting into the [0,45] range.  */
      else
	{
	  bool neg = false;
	  bool fold_sin = false;
	  if (mpfr_cmp_ui (x, 180) <= 0)
	    {
	      if (mpfr_cmp_ui (x, 90) <= 0)
		{
		  if (mpfr_cmp_ui (x, 45) > 0)
		    {
		      mpfr_ui_sub (x, 90, x, GFC_RND_MODE);
		      fold_sin = true;
		    }
		}
	      else
		{
		  if (mpfr_cmp_ui (x, 135) <= 0)
		    {
		      mpfr_sub_ui (x, x, 90, GFC_RND_MODE);
		      fold_sin = true;
		    }
		  else
		    mpfr_ui_sub (x, 180, x, GFC_RND_MODE);
		  neg = true;
		}
	    }

	  else if (mpfr_cmp_ui (x, 270) <= 0)
	    {
	      if (mpfr_cmp_ui (x, 225) <= 0)
		mpfr_sub_ui (x, x, 180, GFC_RND_MODE);
	      else
		{
		  mpfr_ui_sub (x, 270, x, GFC_RND_MODE);
		  fold_sin = true;
		}
	      neg = true;
	    }

	  else
	    {
	      if (mpfr_cmp_ui (x, 315) <= 0)
		{
		  mpfr_sub_ui (x, x, 270, GFC_RND_MODE);
		  fold_sin = true;
		}
	      else
		mpfr_ui_sub (x, 360, x, GFC_RND_MODE);
	    }

	  D2R (x);

	  if (fold_sin)
	    mpfr_sin (x, x, GFC_RND_MODE);
	  else
	    mpfr_cos (x, x, GFC_RND_MODE);

	  if (neg)
	    mpfr_neg (x, x, GFC_RND_MODE);
	}

      mpz_clear (n);
    }

  /* Return NaN for +-Inf and NaN and raise exception.  */
  else
    mpfr_sub (x, x, x, GFC_RND_MODE);

  RETURN (x);

#else
  ERROR_RETURN(cosd, KIND, x);
#endif // ENABLE_COSD
}
#endif // COSD


#ifdef TAND
/* Compute tand(x) = tan(x * pi / 180).  */

RETTYPE
TAND (FTYPE x)
{
#ifdef ENABLE_TAND
  if (ISFINITE (x))
    {
      FTYPE s, one;

      /* tan(-x) = - tan(x).  */
      mpfr_init (s);
      mpfr_init_set_ui (one, 1, GFC_RND_MODE);
      mpfr_copysign (s, one, x, GFC_RND_MODE);
      mpfr_clear (one);

#ifdef SIND_SMALL_LITERAL
      /* tan(x) = x as x -> 0; but only for some precisions. */
      FTYPE ax;
      mpfr_init (ax);
      mpfr_abs (ax, x, GFC_RND_MODE);
      if (mpfr_cmp_ld (ax, SIND_SMALL_LITERAL) < 0)
	{
	  D2R (x);
	  mpfr_clear (ax);
	  return x;
	}

      mpfr_swap (x, ax);
      mpfr_clear (ax);

#else
      mpfr_abs (x, x, GFC_RND_MODE);
#endif /* SIND_SMALL_LITERAL */

      /* Reduce angle to x in [0,360].  */
      FTYPE period;
      mpfr_init_set_ui (period, 360, GFC_RND_MODE);
      mpfr_fmod (x, x, period, GFC_RND_MODE);
      mpfr_clear (period);

      /* Special cases with exact results. */
      ITYPE n;
      mpz_init (n);
      if (mpfr_get_z (n, x, GFC_RND_MODE) == 0 && mpz_divisible_ui_p (n, 45))
	{
	  if (mpz_divisible_ui_p (n, 180))
	    mpfr_set_zero (x, 0);

	  /* Though mathematically NaN is more appropriate for tan(n*90),
	     returning +/-Inf offers the advantage that 1/tan(n*90) returns 0,
	     which is mathematically sound. In fact we rely on this behavior
	     to implement COTAND(x) = 1 / TAND(x).
	   */
	  else if (mpz_divisible_ui_p (n, 90))
	    mpfr_set_inf (x, mpz_cmp_ui (n, 90) == 0 ? 0 : 1);

	  else
	    {
	      mpfr_set_ui (x, 1, GFC_RND_MODE);
	      if (mpz_cmp_ui (n, 45) != 0 && mpz_cmp_ui (n, 225) != 0)
		mpfr_neg (x, x, GFC_RND_MODE);
	    }
	}

      else
	{
	  /* Fold [0,360] into the range [0,90], and compute TAN().  */
	  if (mpfr_cmp_ui (x, 180) <= 0)
	    {
	      if (mpfr_cmp_ui (x, 90) > 0)
		{
		  mpfr_ui_sub (x, 180, x, GFC_RND_MODE);
		  mpfr_neg (s, s, GFC_RND_MODE);
		}
	    }
	  else
	    {
	      if (mpfr_cmp_ui (x, 270) <= 0)
		{
		  mpfr_sub_ui (x, x, 180, GFC_RND_MODE);
		}
	      else
		{
		  mpfr_ui_sub (x, 360, x, GFC_RND_MODE);
		  mpfr_neg (s, s, GFC_RND_MODE);
		}
	    }

	  D2R (x);
	  mpfr_tan (x, x, GFC_RND_MODE);
	}

      mpfr_mul (x, x, s, GFC_RND_MODE);
      mpz_clear (n);
      mpfr_clear (s);
    }

  /* Return NaN for +-Inf and NaN and raise exception.  */
  else
    mpfr_sub (x, x, x, GFC_RND_MODE);

  RETURN (x);

#else
  ERROR_RETURN(tand, KIND, x);
#endif // ENABLE_TAND
}
#endif // TAND

/* vim: set ft=c: */