ieee754io.c revision 181834
1181834Sroberto/*
2181834Sroberto * /src/NTP/ntp4-dev/libntp/ieee754io.c,v 4.12 2005/04/16 17:32:10 kardel RELEASE_20050508_A
3181834Sroberto *
4181834Sroberto * ieee754io.c,v 4.12 2005/04/16 17:32:10 kardel RELEASE_20050508_A
5181834Sroberto *
6181834Sroberto * $Created: Sun Jul 13 09:12:02 1997 $
7181834Sroberto *
8181834Sroberto * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org>
9181834Sroberto *
10181834Sroberto * Redistribution and use in source and binary forms, with or without
11181834Sroberto * modification, are permitted provided that the following conditions
12181834Sroberto * are met:
13181834Sroberto * 1. Redistributions of source code must retain the above copyright
14181834Sroberto *    notice, this list of conditions and the following disclaimer.
15181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright
16181834Sroberto *    notice, this list of conditions and the following disclaimer in the
17181834Sroberto *    documentation and/or other materials provided with the distribution.
18181834Sroberto * 3. Neither the name of the author nor the names of its contributors
19181834Sroberto *    may be used to endorse or promote products derived from this software
20181834Sroberto *    without specific prior written permission.
21181834Sroberto *
22181834Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23181834Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24181834Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25181834Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26181834Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27181834Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28181834Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30181834Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31181834Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32181834Sroberto * SUCH DAMAGE.
33181834Sroberto *
34181834Sroberto */
35181834Sroberto
36181834Sroberto#ifdef HAVE_CONFIG_H
37181834Sroberto#include "config.h"
38181834Sroberto#endif
39181834Sroberto
40181834Sroberto#include <stdio.h>
41181834Sroberto#include "l_stdlib.h"
42181834Sroberto#include "ntp_stdlib.h"
43181834Sroberto#include "ntp_fp.h"
44181834Sroberto#include "ieee754io.h"
45181834Sroberto
46181834Srobertostatic unsigned char get_byte P((unsigned char *, offsets_t, int *));
47181834Sroberto#ifdef __not_yet__
48181834Srobertostatic void put_byte P((unsigned char *, offsets_t, int *, unsigned char));
49181834Sroberto#endif
50181834Sroberto
51181834Sroberto#ifdef LIBDEBUG
52181834Sroberto
53181834Sroberto#include "lib_strbuf.h"
54181834Sroberto
55181834Srobertostatic char *
56181834Srobertofmt_blong(
57181834Sroberto	  unsigned long val,
58181834Sroberto	  int cnt
59181834Sroberto	  )
60181834Sroberto{
61181834Sroberto  char *buf, *s;
62181834Sroberto  int i = cnt;
63181834Sroberto
64181834Sroberto  val <<= 32 - cnt;
65181834Sroberto  LIB_GETBUF(buf);
66181834Sroberto  s = buf;
67181834Sroberto
68181834Sroberto  while (i--)
69181834Sroberto    {
70181834Sroberto      if (val & 0x80000000)
71181834Sroberto	{
72181834Sroberto	  *s++ = '1';
73181834Sroberto	}
74181834Sroberto      else
75181834Sroberto	{
76181834Sroberto	  *s++ = '0';
77181834Sroberto	}
78181834Sroberto      val <<= 1;
79181834Sroberto    }
80181834Sroberto  *s = '\0';
81181834Sroberto  return buf;
82181834Sroberto}
83181834Sroberto
84181834Srobertostatic char *
85181834Srobertofmt_flt(
86181834Sroberto	unsigned int sign,
87181834Sroberto	unsigned long mh,
88181834Sroberto	unsigned long ml,
89181834Sroberto	unsigned long ch
90181834Sroberto	)
91181834Sroberto{
92181834Sroberto  char *buf;
93181834Sroberto
94181834Sroberto  LIB_GETBUF(buf);
95181834Sroberto  sprintf(buf, "%c %s %s %s", sign ? '-' : '+',
96181834Sroberto	  fmt_blong(ch, 11),
97181834Sroberto	  fmt_blong(mh, 20),
98181834Sroberto	  fmt_blong(ml, 32));
99181834Sroberto  return buf;
100181834Sroberto}
101181834Sroberto
102181834Srobertostatic char *
103181834Srobertofmt_hex(
104181834Sroberto	unsigned char *bufp,
105181834Sroberto	int length
106181834Sroberto	)
107181834Sroberto{
108181834Sroberto  char *buf;
109181834Sroberto  int i;
110181834Sroberto
111181834Sroberto  LIB_GETBUF(buf);
112181834Sroberto  for (i = 0; i < length; i++)
113181834Sroberto    {
114181834Sroberto      sprintf(buf+i*2, "%02x", bufp[i]);
115181834Sroberto    }
116181834Sroberto  return buf;
117181834Sroberto}
118181834Sroberto
119181834Sroberto#endif
120181834Sroberto
121181834Srobertostatic unsigned char
122181834Srobertoget_byte(
123181834Sroberto	 unsigned char *bufp,
124181834Sroberto	 offsets_t offset,
125181834Sroberto	 int *fieldindex
126181834Sroberto	 )
127181834Sroberto{
128181834Sroberto  unsigned char val;
129181834Sroberto
130181834Sroberto  val     = *(bufp + offset[*fieldindex]);
131181834Sroberto#ifdef LIBDEBUG
132181834Sroberto  if (debug > 4)
133181834Sroberto    printf("fetchieee754: getbyte(0x%08x, %d) = 0x%02x\n", (unsigned int)(bufp)+offset[*fieldindex], *fieldindex, val);
134181834Sroberto#endif
135181834Sroberto  (*fieldindex)++;
136181834Sroberto  return val;
137181834Sroberto}
138181834Sroberto
139181834Sroberto#ifdef __not_yet__
140181834Srobertostatic void
141181834Srobertoput_byte(
142181834Sroberto	 unsigned char *bufp,
143181834Sroberto	 offsets_t offsets,
144181834Sroberto	 int *fieldindex,
145181834Sroberto	 unsigned char val
146181834Sroberto	 )
147181834Sroberto{
148181834Sroberto  *(bufp + offsets[*fieldindex]) = val;
149181834Sroberto  (*fieldindex)++;
150181834Sroberto}
151181834Sroberto#endif
152181834Sroberto
153181834Sroberto/*
154181834Sroberto * make conversions to and from external IEEE754 formats and internal
155181834Sroberto * NTP FP format.
156181834Sroberto */
157181834Srobertoint
158181834Srobertofetch_ieee754(
159181834Sroberto	      unsigned char **buffpp,
160181834Sroberto	      int size,
161181834Sroberto	      l_fp *lfpp,
162181834Sroberto	      offsets_t offsets
163181834Sroberto	      )
164181834Sroberto{
165181834Sroberto  unsigned char *bufp = *buffpp;
166181834Sroberto  unsigned int sign;
167181834Sroberto  unsigned int bias;
168181834Sroberto  unsigned int maxexp;
169181834Sroberto  int mbits;
170181834Sroberto  u_long mantissa_low;
171181834Sroberto  u_long mantissa_high;
172181834Sroberto  u_long characteristic;
173181834Sroberto  long exponent;
174181834Sroberto#ifdef LIBDEBUG
175181834Sroberto  int length;
176181834Sroberto#endif
177181834Sroberto  unsigned char val;
178181834Sroberto  int fieldindex = 0;
179181834Sroberto
180181834Sroberto  switch (size)
181181834Sroberto    {
182181834Sroberto    case IEEE_DOUBLE:
183181834Sroberto#ifdef LIBDEBUG
184181834Sroberto      length = 8;
185181834Sroberto#endif
186181834Sroberto      mbits  = 52;
187181834Sroberto      bias   = 1023;
188181834Sroberto      maxexp = 2047;
189181834Sroberto      break;
190181834Sroberto
191181834Sroberto    case IEEE_SINGLE:
192181834Sroberto#ifdef LIBDEBUG
193181834Sroberto      length = 4;
194181834Sroberto#endif
195181834Sroberto      mbits  = 23;
196181834Sroberto      bias   = 127;
197181834Sroberto      maxexp = 255;
198181834Sroberto      break;
199181834Sroberto
200181834Sroberto    default:
201181834Sroberto      return IEEE_BADCALL;
202181834Sroberto    }
203181834Sroberto
204181834Sroberto  val = get_byte(bufp, offsets, &fieldindex); /* fetch sign byte & first part of characteristic */
205181834Sroberto
206181834Sroberto  sign     = (val & 0x80) != 0;
207181834Sroberto  characteristic = (val & 0x7F);
208181834Sroberto
209181834Sroberto  val = get_byte(bufp, offsets, &fieldindex); /* fetch rest of characteristic and start of mantissa */
210181834Sroberto
211181834Sroberto  switch (size)
212181834Sroberto    {
213181834Sroberto    case IEEE_SINGLE:
214181834Sroberto      characteristic <<= 1;
215181834Sroberto      characteristic  |= (val & 0x80) != 0; /* grab last characteristic bit */
216181834Sroberto
217181834Sroberto      mantissa_high  = 0;
218181834Sroberto
219181834Sroberto      mantissa_low   = (val &0x7F) << 16;
220181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 8;
221181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
222181834Sroberto      break;
223181834Sroberto
224181834Sroberto    case IEEE_DOUBLE:
225181834Sroberto      characteristic <<= 4;
226181834Sroberto      characteristic  |= (val & 0xF0) >> 4; /* grab lower characteristic bits */
227181834Sroberto
228181834Sroberto      mantissa_high  = (val & 0x0F) << 16;
229181834Sroberto      mantissa_high |= get_byte(bufp, offsets, &fieldindex) << 8;
230181834Sroberto      mantissa_high |= get_byte(bufp, offsets, &fieldindex);
231181834Sroberto
232181834Sroberto      mantissa_low   = get_byte(bufp, offsets, &fieldindex) << 24;
233181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 16;
234181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex) << 8;
235181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
236181834Sroberto      break;
237181834Sroberto
238181834Sroberto    default:
239181834Sroberto      return IEEE_BADCALL;
240181834Sroberto    }
241181834Sroberto#ifdef LIBDEBUG
242181834Sroberto  if (debug > 4)
243181834Sroberto  {
244181834Sroberto    double d;
245181834Sroberto    float f;
246181834Sroberto
247181834Sroberto    if (size == IEEE_SINGLE)
248181834Sroberto      {
249181834Sroberto	int i;
250181834Sroberto
251181834Sroberto	for (i = 0; i < length; i++)
252181834Sroberto	  {
253181834Sroberto	    *((unsigned char *)(&f)+i) = *(*buffpp + offsets[i]);
254181834Sroberto	  }
255181834Sroberto	d = f;
256181834Sroberto      }
257181834Sroberto    else
258181834Sroberto      {
259181834Sroberto	int i;
260181834Sroberto
261181834Sroberto	for (i = 0; i < length; i++)
262181834Sroberto	  {
263181834Sroberto	    *((unsigned char *)(&d)+i) = *(*buffpp + offsets[i]);
264181834Sroberto	  }
265181834Sroberto      }
266181834Sroberto
267181834Sroberto    printf("fetchieee754: FP: %s -> %s -> %e(=%s)\n", fmt_hex(*buffpp, length),
268181834Sroberto	   fmt_flt(sign, mantissa_high, mantissa_low, characteristic),
269181834Sroberto	   d, fmt_hex((unsigned char *)&d, length));
270181834Sroberto  }
271181834Sroberto#endif
272181834Sroberto
273181834Sroberto  *buffpp += fieldindex;
274181834Sroberto
275181834Sroberto  /*
276181834Sroberto   * detect funny numbers
277181834Sroberto   */
278181834Sroberto  if (characteristic == maxexp)
279181834Sroberto    {
280181834Sroberto      /*
281181834Sroberto       * NaN or Infinity
282181834Sroberto       */
283181834Sroberto      if (mantissa_low || mantissa_high)
284181834Sroberto	{
285181834Sroberto	  /*
286181834Sroberto	   * NaN
287181834Sroberto	   */
288181834Sroberto	  return IEEE_NAN;
289181834Sroberto	}
290181834Sroberto      else
291181834Sroberto	{
292181834Sroberto	  /*
293181834Sroberto	   * +Inf or -Inf
294181834Sroberto	   */
295181834Sroberto	  return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY;
296181834Sroberto	}
297181834Sroberto    }
298181834Sroberto  else
299181834Sroberto    {
300181834Sroberto      /*
301181834Sroberto       * collect real numbers
302181834Sroberto       */
303181834Sroberto
304181834Sroberto      L_CLR(lfpp);
305181834Sroberto
306181834Sroberto      /*
307181834Sroberto       * check for overflows
308181834Sroberto       */
309181834Sroberto      exponent = characteristic - bias;
310181834Sroberto
311181834Sroberto      if (exponent > 31)	/* sorry - hardcoded */
312181834Sroberto	{
313181834Sroberto	  /*
314181834Sroberto	   * overflow only in respect to NTP-FP representation
315181834Sroberto	   */
316181834Sroberto	  return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW;
317181834Sroberto	}
318181834Sroberto      else
319181834Sroberto	{
320181834Sroberto	  int frac_offset;	/* where the fraction starts */
321181834Sroberto
322181834Sroberto	  frac_offset = mbits - exponent;
323181834Sroberto
324181834Sroberto	  if (characteristic == 0)
325181834Sroberto	    {
326181834Sroberto	      /*
327181834Sroberto	       * de-normalized or tiny number - fits only as 0
328181834Sroberto	       */
329181834Sroberto	      return IEEE_OK;
330181834Sroberto	    }
331181834Sroberto	  else
332181834Sroberto	    {
333181834Sroberto	      /*
334181834Sroberto	       * adjust for implied 1
335181834Sroberto	       */
336181834Sroberto	      if (mbits > 31)
337181834Sroberto		mantissa_high |= 1 << (mbits - 32);
338181834Sroberto	      else
339181834Sroberto		mantissa_low  |= 1 << mbits;
340181834Sroberto
341181834Sroberto	      /*
342181834Sroberto	       * take mantissa apart - if only all machine would support
343181834Sroberto	       * 64 bit operations 8-(
344181834Sroberto	       */
345181834Sroberto	      if (frac_offset > mbits)
346181834Sroberto		{
347181834Sroberto		  lfpp->l_ui = 0; /* only fractional number */
348181834Sroberto		  frac_offset -= mbits + 1; /* will now contain right shift count - 1*/
349181834Sroberto		  if (mbits > 31)
350181834Sroberto		    {
351181834Sroberto		      lfpp->l_uf   = mantissa_high << (63 - mbits);
352181834Sroberto		      lfpp->l_uf  |= mantissa_low  >> (mbits - 33);
353181834Sroberto		      lfpp->l_uf >>= frac_offset;
354181834Sroberto		    }
355181834Sroberto		  else
356181834Sroberto		    {
357181834Sroberto		      lfpp->l_uf = mantissa_low >> frac_offset;
358181834Sroberto		    }
359181834Sroberto		}
360181834Sroberto	      else
361181834Sroberto		{
362181834Sroberto		  if (frac_offset > 32)
363181834Sroberto		    {
364181834Sroberto		      /*
365181834Sroberto		       * must split in high word
366181834Sroberto		       */
367181834Sroberto		      lfpp->l_ui  =  mantissa_high >> (frac_offset - 32);
368181834Sroberto		      lfpp->l_uf  = (mantissa_high & ((1 << (frac_offset - 32)) - 1)) << (64 - frac_offset);
369181834Sroberto		      lfpp->l_uf |=  mantissa_low  >> (frac_offset - 32);
370181834Sroberto		    }
371181834Sroberto		  else
372181834Sroberto		    {
373181834Sroberto		      /*
374181834Sroberto		       * must split in low word
375181834Sroberto		       */
376181834Sroberto		      lfpp->l_ui  =  mantissa_high << (32 - frac_offset);
377181834Sroberto		      lfpp->l_ui |= (mantissa_low >> frac_offset) & ((1 << (32 - frac_offset)) - 1);
378181834Sroberto		      lfpp->l_uf  = (mantissa_low & ((1 << frac_offset) - 1)) << (32 - frac_offset);
379181834Sroberto		    }
380181834Sroberto		}
381181834Sroberto
382181834Sroberto	      /*
383181834Sroberto	       * adjust for sign
384181834Sroberto	       */
385181834Sroberto	      if (sign)
386181834Sroberto		{
387181834Sroberto		  L_NEG(lfpp);
388181834Sroberto		}
389181834Sroberto
390181834Sroberto	      return IEEE_OK;
391181834Sroberto	    }
392181834Sroberto	}
393181834Sroberto    }
394181834Sroberto}
395181834Sroberto
396181834Srobertoint
397181834Srobertoput_ieee754(
398181834Sroberto	    unsigned char **bufpp,
399181834Sroberto	    int size,
400181834Sroberto	    l_fp *lfpp,
401181834Sroberto	    offsets_t offsets
402181834Sroberto	    )
403181834Sroberto{
404181834Sroberto  l_fp outlfp;
405181834Sroberto#ifdef LIBDEBUG
406181834Sroberto  unsigned int sign;
407181834Sroberto  unsigned int bias;
408181834Sroberto#endif
409181834Sroberto/*unsigned int maxexp;*/
410181834Sroberto  int mbits;
411181834Sroberto  int msb;
412181834Sroberto  u_long mantissa_low = 0;
413181834Sroberto  u_long mantissa_high = 0;
414181834Sroberto#ifdef LIBDEBUG
415181834Sroberto  u_long characteristic = 0;
416181834Sroberto  long exponent;
417181834Sroberto#endif
418181834Sroberto/*int length;*/
419181834Sroberto  unsigned long mask;
420181834Sroberto
421181834Sroberto  outlfp = *lfpp;
422181834Sroberto
423181834Sroberto  switch (size)
424181834Sroberto    {
425181834Sroberto    case IEEE_DOUBLE:
426181834Sroberto    /*length = 8;*/
427181834Sroberto      mbits  = 52;
428181834Sroberto#ifdef LIBDEBUG
429181834Sroberto      bias   = 1023;
430181834Sroberto#endif
431181834Sroberto    /*maxexp = 2047;*/
432181834Sroberto      break;
433181834Sroberto
434181834Sroberto    case IEEE_SINGLE:
435181834Sroberto    /*length = 4;*/
436181834Sroberto      mbits  = 23;
437181834Sroberto#ifdef LIBDEBUG
438181834Sroberto      bias   = 127;
439181834Sroberto#endif
440181834Sroberto    /*maxexp = 255;*/
441181834Sroberto      break;
442181834Sroberto
443181834Sroberto    default:
444181834Sroberto      return IEEE_BADCALL;
445181834Sroberto    }
446181834Sroberto
447181834Sroberto  /*
448181834Sroberto   * find sign
449181834Sroberto   */
450181834Sroberto  if (L_ISNEG(&outlfp))
451181834Sroberto    {
452181834Sroberto      L_NEG(&outlfp);
453181834Sroberto#ifdef LIBDEBUG
454181834Sroberto      sign = 1;
455181834Sroberto#endif
456181834Sroberto    }
457181834Sroberto  else
458181834Sroberto    {
459181834Sroberto#ifdef LIBDEBUG
460181834Sroberto      sign = 0;
461181834Sroberto#endif
462181834Sroberto    }
463181834Sroberto
464181834Sroberto  if (L_ISZERO(&outlfp))
465181834Sroberto    {
466181834Sroberto#ifdef LIBDEBUG
467181834Sroberto      exponent = mantissa_high = mantissa_low = 0; /* true zero */
468181834Sroberto#endif
469181834Sroberto    }
470181834Sroberto  else
471181834Sroberto    {
472181834Sroberto      /*
473181834Sroberto       * find number of significant integer bits
474181834Sroberto       */
475181834Sroberto      mask = 0x80000000;
476181834Sroberto      if (outlfp.l_ui)
477181834Sroberto	{
478181834Sroberto	  msb = 63;
479181834Sroberto	  while (mask && ((outlfp.l_ui & mask) == 0))
480181834Sroberto	    {
481181834Sroberto	      mask >>= 1;
482181834Sroberto	      msb--;
483181834Sroberto	    }
484181834Sroberto	}
485181834Sroberto      else
486181834Sroberto	{
487181834Sroberto	  msb = 31;
488181834Sroberto	  while (mask && ((outlfp.l_uf & mask) == 0))
489181834Sroberto	    {
490181834Sroberto	      mask >>= 1;
491181834Sroberto	      msb--;
492181834Sroberto	    }
493181834Sroberto	}
494181834Sroberto
495181834Sroberto      switch (size)
496181834Sroberto	{
497181834Sroberto	case IEEE_SINGLE:
498181834Sroberto	  mantissa_high = 0;
499181834Sroberto	  if (msb >= 32)
500181834Sroberto	    {
501181834Sroberto	      mantissa_low  = (outlfp.l_ui & ((1 << (msb - 32)) - 1)) << (mbits - (msb - 32));
502181834Sroberto	      mantissa_low |=  outlfp.l_uf >> (mbits - (msb - 32));
503181834Sroberto	    }
504181834Sroberto	  else
505181834Sroberto	    {
506181834Sroberto	      mantissa_low  = (outlfp.l_uf << (mbits - msb)) & ((1 << mbits) - 1);
507181834Sroberto	    }
508181834Sroberto	  break;
509181834Sroberto
510181834Sroberto	case IEEE_DOUBLE:
511181834Sroberto	  if (msb >= 32)
512181834Sroberto	    {
513181834Sroberto	      mantissa_high  = (outlfp.l_ui << (mbits - msb)) & ((1 << (mbits - 32)) - 1);
514181834Sroberto	      mantissa_high |=  outlfp.l_uf >> (32 - (mbits - msb));
515181834Sroberto	      mantissa_low   = (outlfp.l_ui & ((1 << (msb - mbits)) - 1)) << (32 - (msb - mbits));
516181834Sroberto	      mantissa_low  |=  outlfp.l_uf >> (msb - mbits);
517181834Sroberto	    }
518181834Sroberto	  else
519181834Sroberto	    {
520181834Sroberto	      mantissa_high  = outlfp.l_uf << (mbits - 32 - msb);
521181834Sroberto	      mantissa_low   = outlfp.l_uf << (mbits - 32);
522181834Sroberto	    }
523181834Sroberto	}
524181834Sroberto
525181834Sroberto#ifdef LIBDEBUG
526181834Sroberto      exponent = msb - 32;
527181834Sroberto      characteristic = exponent + bias;
528181834Sroberto
529181834Sroberto      if (debug > 4)
530181834Sroberto	printf("FP: %s\n", fmt_flt(sign, mantissa_high, mantissa_low, characteristic));
531181834Sroberto#endif
532181834Sroberto    }
533181834Sroberto  return IEEE_OK;
534181834Sroberto}
535181834Sroberto
536181834Sroberto
537181834Sroberto#if defined(DEBUG) && defined(LIBDEBUG)
538181834Srobertoint main(
539181834Sroberto	 int argc,
540181834Sroberto	 char **argv
541181834Sroberto	 )
542181834Sroberto{
543181834Sroberto  static offsets_t native_off = { 0, 1, 2, 3, 4, 5, 6, 7 };
544181834Sroberto  double f = 1.0;
545181834Sroberto  double *f_p = &f;
546181834Sroberto  l_fp fp;
547181834Sroberto
548181834Sroberto  if (argc == 2)
549181834Sroberto    {
550181834Sroberto      if (sscanf(argv[1], "%lf", &f) != 1)
551181834Sroberto	{
552181834Sroberto	  printf("cannot convert %s to a float\n", argv[1]);
553181834Sroberto	  return 1;
554181834Sroberto	}
555181834Sroberto    }
556181834Sroberto
557181834Sroberto  printf("double: %s %s\n", fmt_blong(*(unsigned long *)&f, 32), fmt_blong(*(unsigned long *)((char *)(&f)+4), 32));
558181834Sroberto  printf("fetch from %f = %d\n", f, fetch_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off));
559181834Sroberto  printf("fp [%s %s] = %s\n", fmt_blong(fp.l_ui, 32), fmt_blong(fp.l_uf, 32), mfptoa(fp.l_ui, fp.l_uf, 15));
560181834Sroberto  f_p = &f;
561181834Sroberto  put_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off);
562181834Sroberto
563181834Sroberto  return 0;
564181834Sroberto}
565181834Sroberto
566181834Sroberto#endif
567181834Sroberto/*
568181834Sroberto * History:
569181834Sroberto *
570181834Sroberto * ieee754io.c,v
571181834Sroberto * Revision 4.12  2005/04/16 17:32:10  kardel
572181834Sroberto * update copyright
573181834Sroberto *
574181834Sroberto * Revision 4.11  2004/11/14 15:29:41  kardel
575181834Sroberto * support PPSAPI, upgrade Copyright to Berkeley style
576181834Sroberto *
577181834Sroberto * Revision 4.8  1999/02/21 12:17:36  kardel
578181834Sroberto * 4.91f reconcilation
579181834Sroberto *
580181834Sroberto * Revision 4.7  1999/02/21 11:26:03  kardel
581181834Sroberto * renamed index to fieldindex to avoid index() name clash
582181834Sroberto *
583181834Sroberto * Revision 4.6  1998/11/15 20:27:52  kardel
584181834Sroberto * Release 4.0.73e13 reconcilation
585181834Sroberto *
586181834Sroberto * Revision 4.5  1998/08/16 19:01:51  kardel
587181834Sroberto * debug information only compile for LIBDEBUG case
588181834Sroberto *
589181834Sroberto * Revision 4.4  1998/08/09 09:39:28  kardel
590181834Sroberto * Release 4.0.73e2 reconcilation
591181834Sroberto *
592181834Sroberto * Revision 4.3  1998/06/13 11:56:19  kardel
593181834Sroberto * disabled putbute() for the time being
594181834Sroberto *
595181834Sroberto * Revision 4.2  1998/06/12 15:16:58  kardel
596181834Sroberto * ansi2knr compatibility
597181834Sroberto *
598181834Sroberto * Revision 4.1  1998/05/24 07:59:56  kardel
599181834Sroberto * conditional debug support
600181834Sroberto *
601181834Sroberto * Revision 4.0  1998/04/10 19:46:29  kardel
602181834Sroberto * Start 4.0 release version numbering
603181834Sroberto *
604181834Sroberto * Revision 1.1  1998/04/10 19:27:46  kardel
605181834Sroberto * initial NTP VERSION 4 integration of PARSE with GPS166 binary support
606181834Sroberto *
607181834Sroberto * Revision 1.1  1997/10/06 21:05:45  kardel
608181834Sroberto * new parse structure
609181834Sroberto *
610181834Sroberto */
611