001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2005-2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc;
020:
021: import java.math.BigInteger;
022:
023: /**
024: * The platform-dependent C type limits.
025: *
026: * <p />To recreate this class, compile <code>limits.c</code> in the same
027: * directory as this interface and run the resulting executable while
028: * piping standard output to <code>Limits.java</code>.
029: *
030: * <p />The rank for pointer difference, sizeof, and wide character types
031: * reflects the ordering <code>char</code>, <code>short</code>,
032: * <code>int</code>, <code>long</code>, and <code>long long</code>,
033: * starting at 1 and ignoring the sign. This program requires that the
034: * <code>__PTRDIFF_TYPE__</code>, <code>__SIZE_TYPE__</code>, and
035: * <code>__WCHAR_TYPE__</code> preprocessor macros are defined.
036: *
037: * @author Robert Grimm
038: * @version $Revision: 1.21 $
039: */
040: public class Limits {
041:
042: /** Hide constructor. */
043: private Limits() { /* Nothing to do. */
044: }
045:
046: /** The flag for whether the machine is big endian. */
047: public static final boolean IS_BIG_ENDIAN = false;
048:
049: /** The size of void types. */
050: public static final int VOID_SIZE = 1;
051:
052: /** The alignment of void types. */
053: public static final int VOID_ALIGN = 1;
054:
055: /** The size of function types. */
056: public static final int FUNCTION_SIZE = 1;
057:
058: /** The alignment of function types. */
059: public static final int FUNCTION_ALIGN = 1;
060:
061: /** The size of pointer types. */
062: public static final int POINTER_SIZE = 4;
063:
064: /** The alignment of pointer types. */
065: public static final int POINTER_ALIGN = 4;
066:
067: /** The rank of pointer difference types. */
068: public static final int PTRDIFF_RANK = 3;
069:
070: /** The rank of sizeof expressions. */
071: public static final int SIZEOF_RANK = 4;
072:
073: /** The size of pointer difference types. */
074: public static final int PTRDIFF_SIZE = 4;
075:
076: /** The size of sizeof expressions. */
077: public static final int SIZEOF_SIZE = 4;
078:
079: /** The maximum size of fixed size arrays. */
080: public static final BigInteger ARRAY_MAX = BigInteger
081: .valueOf(1073741824L);
082:
083: /** The flag for whether <code>char</code> is signed. */
084: public static final boolean IS_CHAR_SIGNED = true;
085:
086: /** The bit width of char types. */
087: public static final int CHAR_BITS = 8;
088:
089: /** The minimum value of signed char types. */
090: public static final BigInteger CHAR_MIN = new BigInteger("-128");
091:
092: /** The maximum value of signed char types. */
093: public static final BigInteger CHAR_MAX = new BigInteger("127");
094:
095: /** The modulo of signed char types. */
096: public static final BigInteger CHAR_MOD = CHAR_MAX
097: .add(BigInteger.ONE);
098:
099: /** The maximum value of unsigned char types. */
100: public static final BigInteger UCHAR_MAX = new BigInteger("255");
101:
102: /** The modulo of unsigned char types. */
103: public static final BigInteger UCHAR_MOD = UCHAR_MAX
104: .add(BigInteger.ONE);
105:
106: /** The flag for whether <code>wchar_t</code> is signed. */
107: public static final boolean IS_WCHAR_SIGNED = true;
108:
109: /** The rank of wide character types. */
110: public static final int WCHAR_RANK = 3;
111:
112: /** The size of wide char types. */
113: public static final int WCHAR_SIZE = 4;
114:
115: /** The size of short types. */
116: public static final int SHORT_SIZE = 2;
117:
118: /** The alignment of short types. */
119: public static final int SHORT_ALIGN = 2;
120:
121: /** The bit width of short types. */
122: public static final int SHORT_BITS = 16;
123:
124: /** The minimum value of signed short types. */
125: public static final BigInteger SHORT_MIN = new BigInteger("-32768");
126:
127: /** The maximum value of signed short types. */
128: public static final BigInteger SHORT_MAX = new BigInteger("32767");
129:
130: /** The modulo of signed short types. */
131: public static final BigInteger SHORT_MOD = SHORT_MAX
132: .add(BigInteger.ONE);
133:
134: /** The maximum value of unsigned short types. */
135: public static final BigInteger USHORT_MAX = new BigInteger("65535");
136:
137: /** The modulo of unsigned short types. */
138: public static final BigInteger USHORT_MOD = USHORT_MAX
139: .add(BigInteger.ONE);
140:
141: /** The flag for whether <code>int</code> is signed in bit-fields. */
142: public static final boolean IS_INT_SIGNED = true;
143:
144: /** The size of int types. */
145: public static final int INT_SIZE = 4;
146:
147: /** The alignment of int types. */
148: public static final int INT_ALIGN = 4;
149:
150: /** The bit width of int types. */
151: public static final int INT_BITS = 32;
152:
153: /** The minimum value of signed int types. */
154: public static final BigInteger INT_MIN = new BigInteger(
155: "-2147483648");
156:
157: /** The maximum value of signed int types. */
158: public static final BigInteger INT_MAX = new BigInteger(
159: "2147483647");
160:
161: /** The modulo of signed int types. */
162: public static final BigInteger INT_MOD = INT_MAX
163: .add(BigInteger.ONE);
164:
165: /** The maximum value of unsigned int types. */
166: public static final BigInteger UINT_MAX = new BigInteger(
167: "4294967295");
168:
169: /** The modulo of unsigned int types. */
170: public static final BigInteger UINT_MOD = UINT_MAX
171: .add(BigInteger.ONE);
172:
173: /** The size of long types. */
174: public static final int LONG_SIZE = 4;
175:
176: /** The alignment of long types. */
177: public static final int LONG_ALIGN = 4;
178:
179: /** The bit width of long types. */
180: public static final int LONG_BITS = 32;
181:
182: /** The minimum value of signed long types. */
183: public static final BigInteger LONG_MIN = new BigInteger(
184: "-2147483648");
185:
186: /** The maximum value of signed long types. */
187: public static final BigInteger LONG_MAX = new BigInteger(
188: "2147483647");
189:
190: /** The modulo of signed long types. */
191: public static final BigInteger LONG_MOD = LONG_MAX
192: .add(BigInteger.ONE);
193:
194: /** The maximum value of unsigned long types. */
195: public static final BigInteger ULONG_MAX = new BigInteger(
196: "4294967295");
197:
198: /** The modulo of unsigned long types. */
199: public static final BigInteger ULONG_MOD = ULONG_MAX
200: .add(BigInteger.ONE);
201:
202: /** The size of long long types. */
203: public static final int LONG_LONG_SIZE = 8;
204:
205: /** The alignment of long long types. */
206: public static final int LONG_LONG_ALIGN = 4;
207:
208: /** The bit width of long long types. */
209: public static final int LONG_LONG_BITS = 64;
210:
211: /** The minimum value of signed long long types. */
212: public static final BigInteger LONG_LONG_MIN = new BigInteger(
213: "-9223372036854775808");
214:
215: /** The maximum value of signed long long types. */
216: public static final BigInteger LONG_LONG_MAX = new BigInteger(
217: "9223372036854775807");
218:
219: /** The modulo of signed long long types. */
220: public static final BigInteger LONG_LONG_MOD = LONG_LONG_MAX
221: .add(BigInteger.ONE);
222:
223: /** The maximum value of unsigned long long types. */
224: public static final BigInteger ULONG_LONG_MAX = new BigInteger(
225: "18446744073709551615");
226:
227: /** The modulo of unsigned long long types. */
228: public static final BigInteger ULONG_LONG_MOD = ULONG_LONG_MAX
229: .add(BigInteger.ONE);
230:
231: /** The size of float types. */
232: public static final int FLOAT_SIZE = 4;
233:
234: /** The alignment of float types. */
235: public static final int FLOAT_ALIGN = 4;
236:
237: /** The size of double types. */
238: public static final int DOUBLE_SIZE = 8;
239:
240: /** The alignment of double types. */
241: public static final int DOUBLE_ALIGN = 4;
242:
243: /** The size of long double types. */
244: public static final int LONG_DOUBLE_SIZE = 16;
245:
246: /** The alignment of long double types. */
247: public static final int LONG_DOUBLE_ALIGN = 16;
248:
249: /**
250: * Convert the specified size to the corresponding bit width.
251: *
252: * @param size The size.
253: * @return The corresponding bit width.
254: */
255: public static int toWidth(int size) {
256: return size * 8;
257: }
258:
259: /**
260: * Determine whether the specified value fits into a char.
261: *
262: * @param value The value.
263: * @return <code>true</code> if the value fits into a char.
264: */
265: public static boolean fitsChar(BigInteger value) {
266: return ((CHAR_MIN.compareTo(value) <= 0) && (CHAR_MAX
267: .compareTo(value) >= 0));
268: }
269:
270: /**
271: * Determine whether the specified value fits into an unsigned
272: * char.
273: *
274: * @param value The value.
275: * @return <code>true</code> if the value fits into an unsigned
276: * char.
277: */
278: public static boolean fitsUnsignedChar(BigInteger value) {
279: return ((BigInteger.ZERO.compareTo(value) <= 0) && (UCHAR_MAX
280: .compareTo(value) >= 0));
281: }
282:
283: /**
284: * Determine whether the specified value fits into a short.
285: *
286: * @param value The value.
287: * @return <code>true</code> if the value fits into a short.
288: */
289: public static boolean fitsShort(BigInteger value) {
290: return ((SHORT_MIN.compareTo(value) <= 0) && (SHORT_MAX
291: .compareTo(value) >= 0));
292: }
293:
294: /**
295: * Determine whether the specified value fits into an unsigned
296: * short.
297: *
298: * @param value The value.
299: * @return <code>true</code> if the value fits into an unsigned
300: * short.
301: */
302: public static boolean fitsUnsignedShort(BigInteger value) {
303: return ((BigInteger.ZERO.compareTo(value) <= 0) && (USHORT_MAX
304: .compareTo(value) >= 0));
305: }
306:
307: /**
308: * Determine whether the specified value fits into an int.
309: *
310: * @param value The value.
311: * @return <code>true</code> if the value fits into an int.
312: */
313: public static boolean fitsInt(BigInteger value) {
314: return ((INT_MIN.compareTo(value) <= 0) && (INT_MAX
315: .compareTo(value) >= 0));
316: }
317:
318: /**
319: * Determine whether the specified value fits into an unsigned
320: * int.
321: *
322: * @param value The value.
323: * @return <code>true</code> if the value fits into an unsigned
324: * int.
325: */
326: public static boolean fitsUnsignedInt(BigInteger value) {
327: return ((BigInteger.ZERO.compareTo(value) <= 0) && (UINT_MAX
328: .compareTo(value) >= 0));
329: }
330:
331: /**
332: * Determine whether the specified value fits into a long.
333: *
334: * @param value The value.
335: * @return <code>true</code> if the value fits into a long.
336: */
337: public static boolean fitsLong(BigInteger value) {
338: return ((LONG_MIN.compareTo(value) <= 0) && (LONG_MAX
339: .compareTo(value) >= 0));
340: }
341:
342: /**
343: * Determine whether the specified value fits into an unsigned
344: * long.
345: *
346: * @param value The value.
347: * @return <code>true</code> if the value fits into an unsigned
348: * long.
349: */
350: public static boolean fitsUnsignedLong(BigInteger value) {
351: return ((BigInteger.ZERO.compareTo(value) <= 0) && (ULONG_MAX
352: .compareTo(value) >= 0));
353: }
354:
355: /**
356: * Determine whether the specified value fits into a long long.
357: *
358: * @param value The value.
359: * @return <code>true</code> if the value fits into a long long.
360: */
361: public static boolean fitsLongLong(BigInteger value) {
362: return ((LONG_LONG_MIN.compareTo(value) <= 0) && (LONG_LONG_MAX
363: .compareTo(value) >= 0));
364: }
365:
366: /**
367: * Determine whether the specified value fits into an unsigned
368: * long long.
369: *
370: * @param value The value.
371: * @return <code>true</code> if the value fits into an unsigned
372: * long long.
373: */
374: public static boolean fitsUnsignedLongLong(BigInteger value) {
375: return ((BigInteger.ZERO.compareTo(value) <= 0) && (ULONG_LONG_MAX
376: .compareTo(value) >= 0));
377: }
378:
379: /**
380: * Mask the specified value as a signed char.
381: *
382: * @param value The value.
383: * @return The value as a signed char.
384: */
385: public static BigInteger maskAsSignedChar(BigInteger value) {
386: return value.remainder(CHAR_MOD);
387: }
388:
389: /**
390: * Mask the specified value as an unsigned char.
391: *
392: * @param value The value.
393: * @return The value as an unsigned char.
394: */
395: public static BigInteger maskAsUnsignedChar(BigInteger value) {
396: return (value.signum() >= 0) ? value.remainder(UCHAR_MOD)
397: : UCHAR_MOD.add(value.remainder(UCHAR_MOD));
398: }
399:
400: /**
401: * Mask the specified value as a signed short.
402: *
403: * @param value The value.
404: * @return The value as a signed short.
405: */
406: public static BigInteger maskAsShort(BigInteger value) {
407: return value.remainder(SHORT_MOD);
408: }
409:
410: /**
411: * Mask the specified value as an unsigned short.
412: *
413: * @param value The value.
414: * @return The value as an unsigned short.
415: */
416: public static BigInteger maskAsUnsignedShort(BigInteger value) {
417: return (value.signum() >= 0) ? value.remainder(USHORT_MOD)
418: : USHORT_MOD.add(value.remainder(USHORT_MOD));
419: }
420:
421: /**
422: * Mask the specified value as a signed int.
423: *
424: * @param value The value.
425: * @return The value as a signed int.
426: */
427: public static BigInteger maskAsInt(BigInteger value) {
428: return value.remainder(INT_MOD);
429: }
430:
431: /**
432: * Mask the specified value as an unsigned int.
433: *
434: * @param value The value.
435: * @return The value as an unsigned int.
436: */
437: public static BigInteger maskAsUnsignedInt(BigInteger value) {
438: return (value.signum() >= 0) ? value.remainder(UINT_MOD)
439: : UINT_MOD.add(value.remainder(UINT_MOD));
440: }
441:
442: /**
443: * Mask the specified value as a signed long.
444: *
445: * @param value The value.
446: * @return The value as a signed long.
447: */
448: public static BigInteger maskAsLong(BigInteger value) {
449: return value.remainder(LONG_MOD);
450: }
451:
452: /**
453: * Mask the specified value as an unsigned long.
454: *
455: * @param value The value.
456: * @return The value as an unsigned long.
457: */
458: public static BigInteger maskAsUnsignedLong(BigInteger value) {
459: return (value.signum() >= 0) ? value.remainder(ULONG_MOD)
460: : ULONG_MOD.add(value.remainder(ULONG_MOD));
461: }
462:
463: /**
464: * Mask the specified value as a signed long long.
465: *
466: * @param value The value.
467: * @return The value as a signed long long.
468: */
469: public static BigInteger maskAsLongLong(BigInteger value) {
470: return value.remainder(LONG_LONG_MOD);
471: }
472:
473: /**
474: * Mask the specified value as an unsigned long long.
475: *
476: * @param value The value.
477: * @return The value as an unsigned long long.
478: */
479: public static BigInteger maskAsUnsignedLongLong(BigInteger value) {
480: return (value.signum() >= 0) ? value.remainder(ULONG_LONG_MOD)
481: : ULONG_LONG_MOD.add(value.remainder(ULONG_LONG_MOD));
482: }
483:
484: }
|