source: trunk/libs/newlib/src/newlib/libm/common/s_trunc.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.5 KB
Line 
1/*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 *
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
10 */
11/*
12FUNCTION
13<<trunc>>, <<truncf>>---round to integer, towards zero
14INDEX
15        trunc
16INDEX
17        truncf
18
19SYNOPSIS
20        #include <math.h>
21        double trunc(double <[x]>);
22        float truncf(float <[x]>);
23
24DESCRIPTION
25        The <<trunc>> functions round their argument to the integer value, in
26        floating format, nearest to but no larger in magnitude than the
27        argument, regardless of the current rounding direction.  (While the
28        "inexact" floating-point exception behavior is unspecified by the C
29        standard, the <<trunc>> functions are written so that "inexact" is not
30        raised if the result does not equal the argument, which behavior is as
31        recommended by IEEE 754 for its related functions.)
32
33RETURNS
34<[x]> truncated to an integral value.
35
36PORTABILITY
37ANSI C, POSIX
38
39*/
40
41#include "fdlibm.h"
42
43#ifndef _DOUBLE_IS_32BITS
44
45#ifdef __STDC__
46        double trunc(double x)
47#else
48        double trunc(x)
49        double x;
50#endif
51{
52  int signbit;
53  /* Most significant word, least significant word. */
54  int msw;
55  unsigned int lsw;
56  int exponent_less_1023;
57
58  EXTRACT_WORDS(msw, lsw, x);
59
60  /* Extract sign bit. */
61  signbit = msw & 0x80000000;
62
63  /* Extract exponent field. */
64  exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023;
65
66  if (exponent_less_1023 < 20)
67    {
68      /* All significant digits are in msw. */
69      if (exponent_less_1023 < 0)
70        {
71          /* -1 < x < 1, so result is +0 or -0. */
72          INSERT_WORDS(x, signbit, 0);
73        }
74      else
75        {
76          /* All relevant fraction bits are in msw, so lsw of the result is 0. */
77          INSERT_WORDS(x, signbit | (msw & ~(0x000fffff >> exponent_less_1023)), 0);
78        }
79    }
80  else if (exponent_less_1023 > 51)
81    {
82      if (exponent_less_1023 == 1024)
83        {
84          /* x is infinite, or not a number, so trigger an exception. */
85          return x + x;
86        }
87      /* All bits in the fraction fields of the msw and lsw are needed in the result. */
88    }
89  else
90    {
91      /* All fraction bits in msw are relevant.  Truncate irrelevant
92         bits from lsw. */
93      INSERT_WORDS(x, msw, lsw & ~(0xffffffffu >> (exponent_less_1023 - 20)));
94    }
95  return x;
96}
97
98#endif /* _DOUBLE_IS_32BITS */
Note: See TracBrowser for help on using the repository browser.