Annotation of embedaddon/libpdel/structs/type/structs_type_float.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: 
                     43: #include <stdio.h>
                     44: #include <stdlib.h>
                     45: #include <stdarg.h>
                     46: #include <string.h>
                     47: #include <ctype.h>
                     48: #include <limits.h>
                     49: #include <assert.h>
                     50: #include <errno.h>
                     51: #include <math.h>
                     52: 
                     53: #include "structs/structs.h"
                     54: #include "structs/type/array.h"
                     55: #include "structs/type/float.h"
                     56: #include "util/typed_mem.h"
                     57: 
                     58: /*
                     59:  * Float type methods
                     60:  *
                     61:  * Type-specific arguments:
                     62:  *     [int]
                     63:  *             0 = float
                     64:  *             1 = double
                     65:  */
                     66: 
                     67: static structs_equal_t structs_float_equal;
                     68: static structs_ascify_t        structs_float_ascify;
                     69: static structs_binify_t        structs_float_binify;
                     70: 
                     71: #define FTYPE_FLOAT            0
                     72: #define FTYPE_DOUBLE           1
                     73: 
                     74: #define STRUCTS_TYPE_FLOAT(type, ftype)                                        \
                     75: const struct structs_type structs_type_ ## type = {                    \
                     76:        sizeof(type),                                                   \
                     77:        #type,                                                          \
                     78:        STRUCTS_TYPE_PRIMITIVE,                                         \
                     79:        structs_region_init,                                            \
                     80:        structs_region_copy,                                            \
                     81:        structs_float_equal,                                            \
                     82:        structs_float_ascify,                                           \
                     83:        structs_float_binify,                                           \
                     84:        structs_region_encode_netorder,                                 \
                     85:        structs_region_decode_netorder,                                 \
                     86:        structs_nothing_free,                                           \
                     87:        { { (void *)(ftype) } }                                         \
                     88: }                                                                      \
                     89: 
                     90: /* Define the types */
                     91: STRUCTS_TYPE_FLOAT(float, FTYPE_FLOAT);
                     92: STRUCTS_TYPE_FLOAT(double, FTYPE_DOUBLE);
                     93: 
                     94: /*********************************************************************
                     95:                        FLOAT TYPE METHODS
                     96: *********************************************************************/
                     97: 
                     98: int
                     99: structs_float_equal(const struct structs_type *type,
                    100:        const void *v1, const void *v2)
                    101: {
                    102:        const int ftype = type->args[0].i;
                    103: 
                    104:        switch (ftype) {
                    105:        case FTYPE_FLOAT:
                    106:            {
                    107:                const float *f1 = (const float *)v1;
                    108:                const float *f2 = (const float *)v2;
                    109: 
                    110:                return (*f1 == *f2);
                    111:            }
                    112:        case FTYPE_DOUBLE:
                    113:            {
                    114:                const double *d1 = (const double *)v1;
                    115:                const double *d2 = (const double *)v2;
                    116: 
                    117:                return (*d1 == *d2);
                    118:            }
                    119:        default:
                    120:                assert(0);
                    121:                return (-1);
                    122:        }
                    123: }
                    124: 
                    125: char *
                    126: structs_float_ascify(const struct structs_type *type,
                    127:        const char *mtype, const void *data)
                    128: {
                    129:        const int ftype = type->args[0].i;
                    130:        char buf[32];
                    131: 
                    132:        switch (ftype) {
                    133:        case FTYPE_FLOAT:
                    134:            {
                    135:                const float *f = (const float *)data;
                    136: 
                    137:                snprintf(buf, sizeof(buf), "%.16g", *f);
                    138:                break;
                    139:            }
                    140:        case FTYPE_DOUBLE:
                    141:            {
                    142:                const double *d = (const double *)data;
                    143: 
                    144:                snprintf(buf, sizeof(buf), "%.16g", *d);
                    145:                break;
                    146:            }
                    147:        default:
                    148:                assert(0);
                    149:                return (NULL);
                    150:        }
                    151:        return (STRDUP(mtype, buf));
                    152: }
                    153: 
                    154: int
                    155: structs_float_binify(const struct structs_type *type,
                    156:        const char *ascii, void *data, char *ebuf, size_t emax)
                    157: {
                    158:        const int ftype = type->args[0].i;
                    159:        double value;
                    160:        double temp;
                    161:        char *eptr;
                    162: 
                    163:        /* Parse value */
                    164:        errno = 0;
                    165:        value = strtod(ascii, &eptr);
                    166:        if (eptr == ascii)
                    167:                goto invalid;
                    168:        while (isspace(*eptr))
                    169:                eptr++;
                    170:        if (*eptr != '\0') {
                    171: invalid:       strlcpy(ebuf, "invalid floating point value", emax);
                    172:                errno = EINVAL;
                    173:                return (-1);
                    174:        }
                    175: 
                    176:        /* Check for overflow/underflow */
                    177:        if (errno == ERANGE) {
                    178:                if (value == 0.0) {
                    179: underflow:             strlcpy(ebuf, "floating point value underflow", emax);
                    180:                        errno = EINVAL;
                    181:                        return (-1);
                    182:                }
                    183:                if (value == HUGE_VAL || value == -HUGE_VAL) {
                    184: overflow:              strlcpy(ebuf, "floating point value overflow", emax);
                    185:                        errno = EINVAL;
                    186:                        return (-1);
                    187:                }
                    188:        }
                    189:        switch (ftype) {
                    190:        case FTYPE_FLOAT:
                    191:                if (value == 0.0)
                    192:                        break;
                    193:                temp = (value < 0) ? -value : value;
                    194:                if (temp < 5.877473155409902e-39)
                    195:                        goto underflow;
                    196:                if (temp > 3.4028234663852886e+38)
                    197:                        goto overflow;
                    198:                *(float *)data = (float)value;
                    199:                break;
                    200:        case FTYPE_DOUBLE:
                    201:                *(double *)data = value;
                    202:                break;
                    203:        }
                    204: 
                    205:        /* Done */
                    206:        return (0);
                    207: }
                    208: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>