X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Ffloat-format.c;h=ee2d1228aa1bb09a076550d69dacfe8c1e414084;hb=b525a9596e60d5ae4c6c464b4a426b77ade3dd72;hp=50a4eae6005b071e1d61320621aa424614fd2654;hpb=c3bd77adba5746aae895e6a354aada4e694c0e3f;p=pspp diff --git a/src/libpspp/float-format.c b/src/libpspp/float-format.c index 50a4eae600..ee2d1228aa 100644 --- a/src/libpspp/float-format.c +++ b/src/libpspp/float-format.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,18 +16,17 @@ #include -#include +#include "libpspp/float-format.h" +#include #include #include #include +#include -#include -#include -#include +#include "libpspp/assertion.h" +#include "libpspp/integer-format.h" -#include "error.h" -#include /* Neutral intermediate representation for binary floating-point numbers. */ struct fp @@ -69,11 +68,9 @@ struct fp static void extract_number (enum float_format, const void *, struct fp *); static void assemble_number (enum float_format, struct fp *, void *); -static inline uint16_t get_uint16 (const void *); static inline uint32_t get_uint32 (const void *); static inline uint64_t get_uint64 (const void *); -static inline void put_uint16 (uint16_t, void *); static inline void put_uint32 (uint32_t, void *); static inline void put_uint64 (uint64_t, void *); @@ -170,22 +167,22 @@ float_identify (double expected_value, const void *number, size_t length, FLOAT_Z_SHORT, FLOAT_Z_LONG, }; - const size_t candidate_cnt = sizeof candidates / sizeof *candidates; + const size_t n_candidates = sizeof candidates / sizeof *candidates; enum float_format *p; - int match_cnt; + int n_matches; - match_cnt = 0; - for (p = candidates; p < candidates + candidate_cnt; p++) + n_matches = 0; + for (p = candidates; p < candidates + n_candidates; p++) if (float_get_size (*p) == length) { char tmp[8]; assert (sizeof tmp >= float_get_size (*p)); float_convert (FLOAT_NATIVE_DOUBLE, &expected_value, *p, tmp); - if (!memcmp (tmp, number, length) && match_cnt++ == 0) + if (!memcmp (tmp, number, length) && n_matches++ == 0) *best_guess = *p; } - return match_cnt; + return n_matches; } /* Returns the double value that is just greater than -DBL_MAX, @@ -206,108 +203,14 @@ float_get_lowest (void) return x; } -/* Returns CNT bits in X starting from the given bit OFS. */ +/* Returns N bits in X starting from the given bit OFS. */ static inline uint64_t -get_bits (uint64_t x, int ofs, int cnt) +get_bits (uint64_t x, int ofs, int n) { assert (ofs >= 0 && ofs < 64); - assert (cnt > 0 && cnt < 64); - assert (ofs + cnt <= 64); - return (x >> ofs) & ((UINT64_C(1) << cnt) - 1); -} - -/* Returns the 16-bit unsigned integer at P, - which need not be aligned. */ -static inline uint16_t -get_uint16 (const void *p) -{ - uint16_t x; - memcpy (&x, p, sizeof x); - return x; -} - -/* Returns the 32-bit unsigned integer at P, - which need not be aligned. */ -static inline uint32_t -get_uint32 (const void *p) -{ - uint32_t x; - memcpy (&x, p, sizeof x); - return x; -} - -/* Returns the 64-bit unsigned integer at P, - which need not be aligned. */ -static inline uint64_t -get_uint64 (const void *p) -{ - uint64_t x; - memcpy (&x, p, sizeof x); - return x; -} - -/* Stores 16-bit unsigned integer X at P, - which need not be aligned. */ -static inline void -put_uint16 (uint16_t x, void *p) -{ - memcpy (p, &x, sizeof x); -} - -/* Stores 32-bit unsigned integer X at P, - which need not be aligned. */ -static inline void -put_uint32 (uint32_t x, void *p) -{ - memcpy (p, &x, sizeof x); -} - -/* Stores 64-bit unsigned integer X at P, - which need not be aligned. */ -static inline void -put_uint64 (uint64_t x, void *p) -{ - memcpy (p, &x, sizeof x); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in little-endian byte order. */ -static inline uint16_t -native_to_le16 (uint16_t native) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_16 (native); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in big-endian byte order. */ -static inline uint16_t -native_to_be16 (uint16_t native) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_16 (native); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in VAX-endian byte order. */ -static inline uint16_t -native_to_vax16 (uint16_t native) -{ - return native_to_le16 (native); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in little-endian byte order. */ -static inline uint32_t -native_to_le32 (uint32_t native) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_32 (native); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in big-endian byte order. */ -static inline uint32_t -native_to_be32 (uint32_t native) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_32 (native); + assert (n > 0 && n < 64); + assert (ofs + n <= 64); + return (x >> ofs) & ((UINT64_C(1) << n) - 1); } /* Returns NATIVE converted to a form that, when stored in @@ -319,22 +222,6 @@ native_to_vax32 (uint32_t native) ((native & 0x00ff00ff) << 8)); } -/* Returns NATIVE converted to a form that, when stored in - memory, will be in little-endian byte order. */ -static inline uint64_t -native_to_le64 (uint64_t native) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_64 (native); -} - -/* Returns NATIVE converted to a form that, when stored in - memory, will be in big-endian byte order. */ -static inline uint64_t -native_to_be64 (uint64_t native) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_64 (native); -} - /* Returns NATIVE converted to a form that, when stored in memory, will be in VAX-endian byte order. */ static inline uint64_t @@ -346,46 +233,6 @@ native_to_vax64 (uint64_t native) ((native & UINT64_C(0x0000000000ff00ff)) << 40)); } -/* Given LE, obtained from memory in little-endian format, - returns its value. */ -static inline uint16_t -le_to_native16 (uint16_t le) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_16 (le); -} - -/* Given BE, obtained from memory in big-endian format, returns - its value. */ -static inline uint16_t -be_to_native16 (uint16_t be) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_16 (be); -} - -/* Given VAX, obtained from memory in VAX-endian format, returns - its value. */ -static inline uint16_t -vax_to_native16 (uint16_t vax) -{ - return le_to_native16 (vax); -} - -/* Given LE, obtained from memory in little-endian format, - returns its value. */ -static inline uint32_t -le_to_native32 (uint32_t le) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_32 (le); -} - -/* Given BE, obtained from memory in big-endian format, returns - its value. */ -static inline uint32_t -be_to_native32 (uint32_t be) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_32 (be); -} - /* Given VAX, obtained from memory in VAX-endian format, returns its value. */ static inline uint32_t @@ -395,22 +242,6 @@ vax_to_native32 (uint32_t vax) return ((be & 0xff00ff00) >> 8) | ((be & 0x00ff00ff) << 8); } -/* Given LE, obtained from memory in little-endian format, - returns its value. */ -static inline uint64_t -le_to_native64 (uint64_t le) -{ - return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_64 (le); -} - -/* Given BE, obtained from memory in big-endian format, returns - its value. */ -static inline uint64_t -be_to_native64 (uint64_t be) -{ - return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_64 (be); -} - /* Given VAX, obtained from memory in VAX-endian format, returns its value. */ static inline uint64_t @@ -649,11 +480,7 @@ extract_hex (const char *s, struct fp *fp) if (fp->fraction == 0) fp->class = ZERO; else if (*s == 'p') - { - char *tail; - fp->exponent += strtol (s + 1, &tail, 10); - s = tail; - } + fp->exponent += strtol (s + 1, NULL, 10); } } } @@ -695,7 +522,7 @@ assemble_number (enum float_format type, struct fp *fp, void *number) break; case FLOAT_Z_SHORT: - put_uint64 (native_to_be32 (assemble_z (fp, 7, 24)), number); + put_uint32 (native_to_be32 (assemble_z (fp, 7, 24)), number); break; case FLOAT_Z_LONG: put_uint64 (native_to_be64 (assemble_z (fp, 7, 56)), number);