OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [math/] [Fixed.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* Fixed.java -- Utility methods for fixed point arithmetics
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.java.math;
40
 
41
/**
42
 * Utility methods for fixed point arithmetics.
43
 */
44
public final class Fixed
45
{
46
 
47
  /**
48
   * Private constructor to avoid instantiation.
49
   */
50
  private Fixed()
51
  {
52
    // Forbidden constructor.
53
  }
54
 
55
  /**
56
   * Divides two fixed point values with <code>n</code> digits.
57
   *
58
   * @param n the number of digits
59
   * @param a the first operand as fixed point value
60
   * @param b the second operand as fixed point value
61
   *
62
   * @return <code>a / b</code> as fixed point value
63
   */
64
  public static int div(int n, int a, int b)
65
  {
66
    return (int) ((((long) a) << n) / b);
67
  }
68
 
69
  /**
70
   * Multiplies two fixed point values with <code>n</code> digits.
71
   *
72
   * @param n the number of digits
73
   * @param a the first operand as fixed point value
74
   * @param b the second operand as fixed point value
75
   *
76
   * @return <code>a * b</code> as fixed point value
77
   */
78
  public static int mul(int n, int a, int b)
79
  {
80
    return (int) ((((long) a) * b) >> n);
81
  }
82
 
83
  /**
84
   * Returns the ceiling value of a fixed point value <code>a</code> with
85
   * the <code>n</code> digits.
86
   *
87
   * @param n the number of digits
88
   * @param a the fixed point value
89
   *
90
   * @return <code>ceil(a)</code> as fixed point value
91
   */
92
  public static int ceil(int n, int a)
93
  {
94
    return (a + (1 << n - 1)) & -(1 << n);
95
  }
96
 
97
  /**
98
   * Returns the floor value of a fixed point value <code>a</code> with
99
   * <code>n</code> digits.
100
   *
101
   * @param n the number of digits
102
   * @param a the fixed point value
103
   *
104
   * @return <code>floor(a)</code> as fixed point value
105
   */
106
  public static int floor(int n, int a)
107
  {
108
    return a & -(1 << n);
109
  }
110
 
111
  /**
112
   * Truncates the number so that only the digits after the point are left.
113
   *
114
   * @param n the number of digits
115
   * @param a the fixed point value
116
   *
117
   * @return the truncated value
118
   */
119
  public static int trunc(int n, int a)
120
  {
121
    return a & (0xFFFFFFFF >>> 32 - n);
122
  }
123
 
124
  /**
125
   * Returns the round value of a fixed point value <code>a</code> with
126
   * the <code>n</code> digits.
127
   *
128
   * @param n the number of digits
129
   * @param a the fixed point value
130
   *
131
   * @return <code>round(a)</code> as fixed point value
132
   */
133
  public static int round(int n, int a)
134
  {
135
    return (a + (1 << (n - 1))) & -(1 << n);
136
  }
137
 
138
  /**
139
   * Returns the fixed point value <code>a</code> with <code>n</code> digits
140
   * as float.
141
   *
142
   * @param n the number of digits
143
   * @param a the fixed point value
144
   *
145
   * @return the float value of <code>a</code>
146
   */
147
  public static float floatValue(int n, int a)
148
  {
149
    return ((float) a) / (1 << n);
150
  }
151
 
152
  /**
153
   * Returns the fixed point value <code>a</code> with <code>n</code> digits
154
   * as double.
155
   *
156
   * @param n the number of digits
157
   * @param a the fixed point value
158
   *
159
   * @return the double value of <code>a</code>
160
   */
161
  public static double doubleValue(int n, int a)
162
  {
163
    return ((double) a) / (1 << n);
164
  }
165
 
166
  /**
167
   * Returns the fixed point value that corresponds to the specified float
168
   * value <code>a</code> with <code>n</code> digits.
169
   *
170
   * @param n the number of digits
171
   * @param a the float value
172
   *
173
   * @return the fixed point value
174
   */
175
  public static int fixedValue(int n, float a)
176
  {
177
    return (int) (a * (1 << n));
178
  }
179
 
180
  /**
181
   * Returns the fixed point value that corresponds to the specified double
182
   * value <code>a</code> with <code>n</code> digits.
183
   *
184
   * @param n the number of digits
185
   * @param a the double value
186
   *
187
   * @return the fixed point value
188
   */
189
  public static int fixedValue(int n, double a)
190
  {
191
    return (int) (a * (1 << n));
192
  }
193
 
194
  /**
195
   * Returns the integer value of the specified fixed point value
196
   * <code>a</code>. This simply cuts of the digits (== floor(a)).
197
   *
198
   * @param n the number of digits
199
   * @param a the fixed point value
200
   *
201
   * @return the integer value
202
   */
203
  public static int intValue(int n, int a)
204
  {
205
    return a >> n;
206
  }
207
 
208
  /**
209
   * Returns a fixed point decimal as rounded integer value.
210
   *
211
   * @param n the number of digits
212
   * @param a the fixed point number
213
   *
214
   * @return the fixed point decimal as rounded integer value
215
   */
216
  public static int roundIntValue(int n, int a)
217
  {
218
    return (a + (1 << (n - 1))) >> n;
219
  }
220
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.