]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr_float.c
   2  * Copyright (c) 1999-2018 Apple Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights 
   7  * Reserved.  This file contains Original Code and/or Modifications of 
   8  * Original Code as defined in and that are subject to the Apple Public 
   9  * Source License Version 1.1 (the "License").  You may not use this file 
  10  * except in compliance with the License.  Please obtain a copy of the 
  11  * License at http://www.apple.com/publicsource and read it before using 
  14  * The Original Code and all software distributed under the License are 
  15  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  16  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  17  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  18  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the 
  19  * License for the specific language governing rights and limitations 
  22  * @APPLE_LICENSE_HEADER_END@ 
  25 /*      $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $      */ 
  28  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 
  29  * unrestricted use provided that this legend is included on all tape 
  30  * media and as a part of the software program in whole or part.  Users 
  31  * may copy or modify Sun RPC without charge, but are not authorized 
  32  * to license or distribute it to anyone else except as part of a product or 
  33  * program developed by the user. 
  35  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 
  36  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 
  37  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 
  39  * Sun RPC is provided with no support and without any obligation on the 
  40  * part of Sun Microsystems, Inc. to assist in its use, correction, 
  41  * modification or enhancement. 
  43  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 
  44  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 
  45  * OR ANY PART THEREOF. 
  47  * In no event will Sun Microsystems, Inc. be liable for any lost revenue 
  48  * or profits or other special, indirect and consequential damages, even if 
  49  * Sun has been advised of the possibility of such damages. 
  51  * Sun Microsystems, Inc. 
  53  * Mountain View, California  94043 
  56 #if defined(LIBC_SCCS) && !defined(lint) 
  57 static char *sccsid 
= "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; 
  58 static char *sccsid 
= "@(#)xdr_float.c  2.1 88/07/29 4.0 RPCSRC"; 
  60 #include "libinfo_common.h" 
  62 #include <sys/cdefs.h> 
  65  * xdr_float.c, Generic XDR routines implementation. 
  67  * Copyright (C) 1984, Sun Microsystems, Inc. 
  69  * These are the "floating point" xdr routines used to (de)serialize 
  70  * most common data items.  See xdr.h for more info on the interface to 
  74 #include <sys/types.h> 
  75 #include <sys/param.h> 
  79 #include <rpc/types.h> 
  84  * This routine works on machines with IEEE754 FP and Vaxen. 
  87 #if 1   /* Used to be long list of architectures */ 
  88 #include <machine/endian.h> 
  94 /* What IEEE single precision floating point looks like on a Vax */ 
  96         unsigned int    mantissa
: 23; 
  98         unsigned int    sign    
: 1; 
 101 /* Vax single precision floating point */ 
 103         unsigned int    mantissa1 
: 7; 
 104         unsigned int    exp       
: 8; 
 105         unsigned int    sign      
: 1; 
 106         unsigned int    mantissa2 
: 16; 
 109 #define VAX_SNG_BIAS    0x81 
 110 #define IEEE_SNG_BIAS   0x7f 
 112 static struct sgl_limits 
{ 
 114         struct ieee_single ieee
; 
 116         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */ 
 117         { 0x0, 0xff, 0x0 }},            /* Max IEEE */ 
 118         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */ 
 119         { 0x0, 0x0, 0x0 }}              /* Min IEEE */ 
 130         struct ieee_single is
; 
 131         struct vax_single vs
, *vsp
; 
 132         struct sgl_limits 
*lim
; 
 135         switch (xdrs
->x_op
) { 
 139                 return (XDR_PUTINT32(xdrs
, (int32_t *)fp
)); 
 141                 vs 
= *((struct vax_single 
*)fp
); 
 142                 for (i 
= 0, lim 
= sgl_limits
; 
 143                         i 
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
); 
 145                         if ((vs
.mantissa2 
== lim
->s
.mantissa2
) && 
 146                                 (vs
.exp 
== lim
->s
.exp
) && 
 147                                 (vs
.mantissa1 
== lim
->s
.mantissa1
)) { 
 152                 is
.exp 
= vs
.exp 
- VAX_SNG_BIAS 
+ IEEE_SNG_BIAS
; 
 153                 is
.mantissa 
= (vs
.mantissa1 
<< 16) | vs
.mantissa2
; 
 156                 return (XDR_PUTINT32(xdrs
, (int32_t *)&is
)); 
 161                 return (XDR_GETINT32(xdrs
, (int32_t *)fp
)); 
 163                 vsp 
= (struct vax_single 
*)fp
; 
 164                 if (!XDR_GETINT32(xdrs
, (int32_t *)&is
)) 
 166                 for (i 
= 0, lim 
= sgl_limits
; 
 167                         i 
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
); 
 169                         if ((is
.exp 
== lim
->ieee
.exp
) && 
 170                                 (is
.mantissa 
== lim
->ieee
.mantissa
)) { 
 175                 vsp
->exp 
= is
.exp 
- IEEE_SNG_BIAS 
+ VAX_SNG_BIAS
; 
 176                 vsp
->mantissa2 
= is
.mantissa
; 
 177                 vsp
->mantissa1 
= (is
.mantissa 
>> 16); 
 191 /* What IEEE double precision floating point looks like on a Vax */ 
 193         unsigned int    mantissa1 
: 20; 
 194         unsigned int    exp       
: 11; 
 195         unsigned int    sign      
: 1; 
 196         unsigned int    mantissa2 
: 32; 
 199 /* Vax double precision floating point */ 
 201         unsigned int    mantissa1 
: 7; 
 202         unsigned int    exp       
: 8; 
 203         unsigned int    sign      
: 1; 
 204         unsigned int    mantissa2 
: 16; 
 205         unsigned int    mantissa3 
: 16; 
 206         unsigned int    mantissa4 
: 16; 
 209 #define VAX_DBL_BIAS    0x81 
 210 #define IEEE_DBL_BIAS   0x3ff 
 211 #define MASK(nbits)     ((1 << nbits) - 1) 
 213 static struct dbl_limits 
{ 
 215         struct  ieee_double ieee
; 
 217         {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */ 
 218         { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */ 
 219         {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */ 
 220         { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */ 
 237         struct  ieee_double id
; 
 238         struct  vax_double vd
; 
 239         struct dbl_limits 
*lim
; 
 243         switch (xdrs
->x_op
) { 
 247                 i32p 
= (int32_t *)(void *)dp
; 
 248 #if BYTE_ORDER == BIG_ENDIAN 
 249                 rv 
= XDR_PUTINT32(xdrs
, i32p
); 
 252                 rv 
= XDR_PUTINT32(xdrs
, i32p
+1); 
 254                 rv 
= XDR_PUTINT32(xdrs
, i32p
+1); 
 257                 rv 
= XDR_PUTINT32(xdrs
, i32p
); 
 261                 vd 
= *((struct vax_double 
*)dp
); 
 262                 for (i 
= 0, lim 
= dbl_limits
; 
 263                         i 
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
); 
 265                         if ((vd
.mantissa4 
== lim
->d
.mantissa4
) && 
 266                                 (vd
.mantissa3 
== lim
->d
.mantissa3
) && 
 267                                 (vd
.mantissa2 
== lim
->d
.mantissa2
) && 
 268                                 (vd
.mantissa1 
== lim
->d
.mantissa1
) && 
 269                                 (vd
.exp 
== lim
->d
.exp
)) { 
 274                 id
.exp 
= vd
.exp 
- VAX_DBL_BIAS 
+ IEEE_DBL_BIAS
; 
 275                 id
.mantissa1 
= (vd
.mantissa1 
<< 13) | (vd
.mantissa2 
>> 3); 
 276                 id
.mantissa2 
= ((vd
.mantissa2 
& MASK(3)) << 29) | 
 277                                 (vd
.mantissa3 
<< 13) | 
 278                                 ((vd
.mantissa4 
>> 3) & MASK(13)); 
 282                 return (XDR_PUTINT32(xdrs
, lp
++) && XDR_PUTINT32(xdrs
, lp
)); 
 287                 i32p 
= (int32_t *)(void *)dp
; 
 288 #if BYTE_ORDER == BIG_ENDIAN 
 289                 rv 
= XDR_GETINT32(xdrs
, i32p
); 
 292                 rv 
= XDR_GETINT32(xdrs
, i32p
+1); 
 294                 rv 
= XDR_GETINT32(xdrs
, i32p
+1); 
 297                 rv 
= XDR_GETINT32(xdrs
, i32p
); 
 302                 if (!XDR_GETINT32(xdrs
, lp
++) || !XDR_GETINT32(xdrs
, lp
)) 
 304                 for (i 
= 0, lim 
= dbl_limits
; 
 305                         i 
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
); 
 307                         if ((id
.mantissa2 
== lim
->ieee
.mantissa2
) && 
 308                                 (id
.mantissa1 
== lim
->ieee
.mantissa1
) && 
 309                                 (id
.exp 
== lim
->ieee
.exp
)) { 
 314                 vd
.exp 
= id
.exp 
- IEEE_DBL_BIAS 
+ VAX_DBL_BIAS
; 
 315                 vd
.mantissa1 
= (id
.mantissa1 
>> 13); 
 316                 vd
.mantissa2 
= ((id
.mantissa1 
& MASK(13)) << 3) | 
 317                                 (id
.mantissa2 
>> 29); 
 318                 vd
.mantissa3 
= (id
.mantissa2 
>> 13); 
 319                 vd
.mantissa4 
= (id
.mantissa2 
<< 3); 
 322                 *dp 
= *((double *)&vd
);