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
46280849Scystatic unsigned char get_byte (unsigned char *, offsets_t, int *);
47181834Sroberto#ifdef __not_yet__
48280849Scystatic void put_byte (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{
92280849Scy	char *buf;
93181834Sroberto
94280849Scy	LIB_GETBUF(buf);
95280849Scy	snprintf(buf, LIB_BUFLENGTH, "%c %s %s %s", sign ? '-' : '+',
96280849Scy		 fmt_blong(ch, 11),
97280849Scy		 fmt_blong(mh, 20),
98280849Scy		 fmt_blong(ml, 32));
99280849Scy
100280849Scy	return buf;
101181834Sroberto}
102181834Sroberto
103181834Srobertostatic char *
104181834Srobertofmt_hex(
105181834Sroberto	unsigned char *bufp,
106181834Sroberto	int length
107181834Sroberto	)
108181834Sroberto{
109280849Scy	char *	buf;
110280849Scy	char	hex[4];
111280849Scy	int	i;
112181834Sroberto
113280849Scy	LIB_GETBUF(buf);
114280849Scy	buf[0] = '\0';
115280849Scy	for (i = 0; i < length; i++) {
116280849Scy		snprintf(hex, sizeof(hex), "%02x", bufp[i]);
117280849Scy		strlcat(buf, hex, LIB_BUFLENGTH);
118280849Scy	}
119280849Scy
120280849Scy	return buf;
121181834Sroberto}
122181834Sroberto
123181834Sroberto#endif
124181834Sroberto
125181834Srobertostatic unsigned char
126181834Srobertoget_byte(
127181834Sroberto	 unsigned char *bufp,
128181834Sroberto	 offsets_t offset,
129181834Sroberto	 int *fieldindex
130181834Sroberto	 )
131181834Sroberto{
132181834Sroberto  unsigned char val;
133181834Sroberto
134181834Sroberto  val     = *(bufp + offset[*fieldindex]);
135181834Sroberto#ifdef LIBDEBUG
136181834Sroberto  if (debug > 4)
137181834Sroberto    printf("fetchieee754: getbyte(0x%08x, %d) = 0x%02x\n", (unsigned int)(bufp)+offset[*fieldindex], *fieldindex, val);
138181834Sroberto#endif
139181834Sroberto  (*fieldindex)++;
140181834Sroberto  return val;
141181834Sroberto}
142181834Sroberto
143181834Sroberto#ifdef __not_yet__
144181834Srobertostatic void
145181834Srobertoput_byte(
146181834Sroberto	 unsigned char *bufp,
147181834Sroberto	 offsets_t offsets,
148181834Sroberto	 int *fieldindex,
149181834Sroberto	 unsigned char val
150181834Sroberto	 )
151181834Sroberto{
152181834Sroberto  *(bufp + offsets[*fieldindex]) = val;
153181834Sroberto  (*fieldindex)++;
154181834Sroberto}
155181834Sroberto#endif
156181834Sroberto
157181834Sroberto/*
158181834Sroberto * make conversions to and from external IEEE754 formats and internal
159181834Sroberto * NTP FP format.
160181834Sroberto */
161181834Srobertoint
162181834Srobertofetch_ieee754(
163181834Sroberto	      unsigned char **buffpp,
164181834Sroberto	      int size,
165181834Sroberto	      l_fp *lfpp,
166181834Sroberto	      offsets_t offsets
167181834Sroberto	      )
168181834Sroberto{
169181834Sroberto  unsigned char *bufp = *buffpp;
170181834Sroberto  unsigned int sign;
171181834Sroberto  unsigned int bias;
172181834Sroberto  unsigned int maxexp;
173181834Sroberto  int mbits;
174181834Sroberto  u_long mantissa_low;
175181834Sroberto  u_long mantissa_high;
176181834Sroberto  u_long characteristic;
177181834Sroberto  long exponent;
178181834Sroberto#ifdef LIBDEBUG
179181834Sroberto  int length;
180181834Sroberto#endif
181181834Sroberto  unsigned char val;
182181834Sroberto  int fieldindex = 0;
183181834Sroberto
184181834Sroberto  switch (size)
185181834Sroberto    {
186181834Sroberto    case IEEE_DOUBLE:
187181834Sroberto#ifdef LIBDEBUG
188181834Sroberto      length = 8;
189181834Sroberto#endif
190181834Sroberto      mbits  = 52;
191181834Sroberto      bias   = 1023;
192181834Sroberto      maxexp = 2047;
193181834Sroberto      break;
194181834Sroberto
195181834Sroberto    case IEEE_SINGLE:
196181834Sroberto#ifdef LIBDEBUG
197181834Sroberto      length = 4;
198181834Sroberto#endif
199181834Sroberto      mbits  = 23;
200181834Sroberto      bias   = 127;
201181834Sroberto      maxexp = 255;
202181834Sroberto      break;
203181834Sroberto
204181834Sroberto    default:
205181834Sroberto      return IEEE_BADCALL;
206181834Sroberto    }
207181834Sroberto
208181834Sroberto  val = get_byte(bufp, offsets, &fieldindex); /* fetch sign byte & first part of characteristic */
209181834Sroberto
210181834Sroberto  sign     = (val & 0x80) != 0;
211181834Sroberto  characteristic = (val & 0x7F);
212181834Sroberto
213181834Sroberto  val = get_byte(bufp, offsets, &fieldindex); /* fetch rest of characteristic and start of mantissa */
214181834Sroberto
215181834Sroberto  switch (size)
216181834Sroberto    {
217181834Sroberto    case IEEE_SINGLE:
218181834Sroberto      characteristic <<= 1;
219181834Sroberto      characteristic  |= (val & 0x80) != 0; /* grab last characteristic bit */
220181834Sroberto
221181834Sroberto      mantissa_high  = 0;
222181834Sroberto
223181834Sroberto      mantissa_low   = (val &0x7F) << 16;
224280849Scy      mantissa_low  |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8;
225181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
226181834Sroberto      break;
227181834Sroberto
228181834Sroberto    case IEEE_DOUBLE:
229181834Sroberto      characteristic <<= 4;
230181834Sroberto      characteristic  |= (val & 0xF0) >> 4; /* grab lower characteristic bits */
231181834Sroberto
232181834Sroberto      mantissa_high  = (val & 0x0F) << 16;
233280849Scy      mantissa_high |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8;
234181834Sroberto      mantissa_high |= get_byte(bufp, offsets, &fieldindex);
235181834Sroberto
236280849Scy      mantissa_low   = (u_long)get_byte(bufp, offsets, &fieldindex) << 24;
237280849Scy      mantissa_low  |= (u_long)get_byte(bufp, offsets, &fieldindex) << 16;
238280849Scy      mantissa_low  |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8;
239181834Sroberto      mantissa_low  |= get_byte(bufp, offsets, &fieldindex);
240181834Sroberto      break;
241181834Sroberto
242181834Sroberto    default:
243181834Sroberto      return IEEE_BADCALL;
244181834Sroberto    }
245181834Sroberto#ifdef LIBDEBUG
246181834Sroberto  if (debug > 4)
247181834Sroberto  {
248181834Sroberto    double d;
249181834Sroberto    float f;
250181834Sroberto
251181834Sroberto    if (size == IEEE_SINGLE)
252181834Sroberto      {
253181834Sroberto	int i;
254181834Sroberto
255181834Sroberto	for (i = 0; i < length; i++)
256181834Sroberto	  {
257181834Sroberto	    *((unsigned char *)(&f)+i) = *(*buffpp + offsets[i]);
258181834Sroberto	  }
259181834Sroberto	d = f;
260181834Sroberto      }
261181834Sroberto    else
262181834Sroberto      {
263181834Sroberto	int i;
264181834Sroberto
265181834Sroberto	for (i = 0; i < length; i++)
266181834Sroberto	  {
267181834Sroberto	    *((unsigned char *)(&d)+i) = *(*buffpp + offsets[i]);
268181834Sroberto	  }
269181834Sroberto      }
270181834Sroberto
271181834Sroberto    printf("fetchieee754: FP: %s -> %s -> %e(=%s)\n", fmt_hex(*buffpp, length),
272181834Sroberto	   fmt_flt(sign, mantissa_high, mantissa_low, characteristic),
273181834Sroberto	   d, fmt_hex((unsigned char *)&d, length));
274181834Sroberto  }
275181834Sroberto#endif
276181834Sroberto
277181834Sroberto  *buffpp += fieldindex;
278181834Sroberto
279181834Sroberto  /*
280181834Sroberto   * detect funny numbers
281181834Sroberto   */
282181834Sroberto  if (characteristic == maxexp)
283181834Sroberto    {
284181834Sroberto      /*
285181834Sroberto       * NaN or Infinity
286181834Sroberto       */
287181834Sroberto      if (mantissa_low || mantissa_high)
288181834Sroberto	{
289181834Sroberto	  /*
290181834Sroberto	   * NaN
291181834Sroberto	   */
292181834Sroberto	  return IEEE_NAN;
293181834Sroberto	}
294181834Sroberto      else
295181834Sroberto	{
296181834Sroberto	  /*
297181834Sroberto	   * +Inf or -Inf
298181834Sroberto	   */
299181834Sroberto	  return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY;
300181834Sroberto	}
301181834Sroberto    }
302181834Sroberto  else
303181834Sroberto    {
304181834Sroberto      /*
305181834Sroberto       * collect real numbers
306181834Sroberto       */
307181834Sroberto
308181834Sroberto      L_CLR(lfpp);
309181834Sroberto
310181834Sroberto      /*
311181834Sroberto       * check for overflows
312181834Sroberto       */
313181834Sroberto      exponent = characteristic - bias;
314181834Sroberto
315181834Sroberto      if (exponent > 31)	/* sorry - hardcoded */
316181834Sroberto	{
317181834Sroberto	  /*
318181834Sroberto	   * overflow only in respect to NTP-FP representation
319181834Sroberto	   */
320181834Sroberto	  return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW;
321181834Sroberto	}
322181834Sroberto      else
323181834Sroberto	{
324181834Sroberto	  int frac_offset;	/* where the fraction starts */
325181834Sroberto
326181834Sroberto	  frac_offset = mbits - exponent;
327181834Sroberto
328181834Sroberto	  if (characteristic == 0)
329181834Sroberto	    {
330181834Sroberto	      /*
331181834Sroberto	       * de-normalized or tiny number - fits only as 0
332181834Sroberto	       */
333181834Sroberto	      return IEEE_OK;
334181834Sroberto	    }
335181834Sroberto	  else
336181834Sroberto	    {
337181834Sroberto	      /*
338181834Sroberto	       * adjust for implied 1
339181834Sroberto	       */
340181834Sroberto	      if (mbits > 31)
341181834Sroberto		mantissa_high |= 1 << (mbits - 32);
342181834Sroberto	      else
343181834Sroberto		mantissa_low  |= 1 << mbits;
344181834Sroberto
345181834Sroberto	      /*
346181834Sroberto	       * take mantissa apart - if only all machine would support
347181834Sroberto	       * 64 bit operations 8-(
348181834Sroberto	       */
349181834Sroberto	      if (frac_offset > mbits)
350181834Sroberto		{
351181834Sroberto		  lfpp->l_ui = 0; /* only fractional number */
352181834Sroberto		  frac_offset -= mbits + 1; /* will now contain right shift count - 1*/
353181834Sroberto		  if (mbits > 31)
354181834Sroberto		    {
355181834Sroberto		      lfpp->l_uf   = mantissa_high << (63 - mbits);
356181834Sroberto		      lfpp->l_uf  |= mantissa_low  >> (mbits - 33);
357181834Sroberto		      lfpp->l_uf >>= frac_offset;
358181834Sroberto		    }
359181834Sroberto		  else
360181834Sroberto		    {
361181834Sroberto		      lfpp->l_uf = mantissa_low >> frac_offset;
362181834Sroberto		    }
363181834Sroberto		}
364181834Sroberto	      else
365181834Sroberto		{
366181834Sroberto		  if (frac_offset > 32)
367181834Sroberto		    {
368181834Sroberto		      /*
369181834Sroberto		       * must split in high word
370181834Sroberto		       */
371181834Sroberto		      lfpp->l_ui  =  mantissa_high >> (frac_offset - 32);
372181834Sroberto		      lfpp->l_uf  = (mantissa_high & ((1 << (frac_offset - 32)) - 1)) << (64 - frac_offset);
373181834Sroberto		      lfpp->l_uf |=  mantissa_low  >> (frac_offset - 32);
374181834Sroberto		    }
375181834Sroberto		  else
376181834Sroberto		    {
377181834Sroberto		      /*
378181834Sroberto		       * must split in low word
379181834Sroberto		       */
380181834Sroberto		      lfpp->l_ui  =  mantissa_high << (32 - frac_offset);
381181834Sroberto		      lfpp->l_ui |= (mantissa_low >> frac_offset) & ((1 << (32 - frac_offset)) - 1);
382181834Sroberto		      lfpp->l_uf  = (mantissa_low & ((1 << frac_offset) - 1)) << (32 - frac_offset);
383181834Sroberto		    }
384181834Sroberto		}
385181834Sroberto
386181834Sroberto	      /*
387181834Sroberto	       * adjust for sign
388181834Sroberto	       */
389181834Sroberto	      if (sign)
390181834Sroberto		{
391181834Sroberto		  L_NEG(lfpp);
392181834Sroberto		}
393181834Sroberto
394181834Sroberto	      return IEEE_OK;
395181834Sroberto	    }
396181834Sroberto	}
397181834Sroberto    }
398181834Sroberto}
399181834Sroberto
400181834Srobertoint
401181834Srobertoput_ieee754(
402181834Sroberto	    unsigned char **bufpp,
403181834Sroberto	    int size,
404181834Sroberto	    l_fp *lfpp,
405181834Sroberto	    offsets_t offsets
406181834Sroberto	    )
407181834Sroberto{
408181834Sroberto  l_fp outlfp;
409181834Sroberto#ifdef LIBDEBUG
410181834Sroberto  unsigned int sign;
411181834Sroberto  unsigned int bias;
412181834Sroberto#endif
413181834Sroberto/*unsigned int maxexp;*/
414181834Sroberto  int mbits;
415181834Sroberto  int msb;
416181834Sroberto  u_long mantissa_low = 0;
417181834Sroberto  u_long mantissa_high = 0;
418181834Sroberto#ifdef LIBDEBUG
419181834Sroberto  u_long characteristic = 0;
420181834Sroberto  long exponent;
421181834Sroberto#endif
422181834Sroberto/*int length;*/
423181834Sroberto  unsigned long mask;
424181834Sroberto
425181834Sroberto  outlfp = *lfpp;
426181834Sroberto
427181834Sroberto  switch (size)
428181834Sroberto    {
429181834Sroberto    case IEEE_DOUBLE:
430181834Sroberto    /*length = 8;*/
431181834Sroberto      mbits  = 52;
432181834Sroberto#ifdef LIBDEBUG
433181834Sroberto      bias   = 1023;
434181834Sroberto#endif
435181834Sroberto    /*maxexp = 2047;*/
436181834Sroberto      break;
437181834Sroberto
438181834Sroberto    case IEEE_SINGLE:
439181834Sroberto    /*length = 4;*/
440181834Sroberto      mbits  = 23;
441181834Sroberto#ifdef LIBDEBUG
442181834Sroberto      bias   = 127;
443181834Sroberto#endif
444181834Sroberto    /*maxexp = 255;*/
445181834Sroberto      break;
446181834Sroberto
447181834Sroberto    default:
448181834Sroberto      return IEEE_BADCALL;
449181834Sroberto    }
450181834Sroberto
451181834Sroberto  /*
452181834Sroberto   * find sign
453181834Sroberto   */
454181834Sroberto  if (L_ISNEG(&outlfp))
455181834Sroberto    {
456181834Sroberto      L_NEG(&outlfp);
457181834Sroberto#ifdef LIBDEBUG
458181834Sroberto      sign = 1;
459181834Sroberto#endif
460181834Sroberto    }
461181834Sroberto  else
462181834Sroberto    {
463181834Sroberto#ifdef LIBDEBUG
464181834Sroberto      sign = 0;
465181834Sroberto#endif
466181834Sroberto    }
467181834Sroberto
468181834Sroberto  if (L_ISZERO(&outlfp))
469181834Sroberto    {
470181834Sroberto#ifdef LIBDEBUG
471181834Sroberto      exponent = mantissa_high = mantissa_low = 0; /* true zero */
472181834Sroberto#endif
473181834Sroberto    }
474181834Sroberto  else
475181834Sroberto    {
476181834Sroberto      /*
477181834Sroberto       * find number of significant integer bits
478181834Sroberto       */
479181834Sroberto      mask = 0x80000000;
480181834Sroberto      if (outlfp.l_ui)
481181834Sroberto	{
482181834Sroberto	  msb = 63;
483181834Sroberto	  while (mask && ((outlfp.l_ui & mask) == 0))
484181834Sroberto	    {
485181834Sroberto	      mask >>= 1;
486181834Sroberto	      msb--;
487181834Sroberto	    }
488181834Sroberto	}
489181834Sroberto      else
490181834Sroberto	{
491181834Sroberto	  msb = 31;
492181834Sroberto	  while (mask && ((outlfp.l_uf & mask) == 0))
493181834Sroberto	    {
494181834Sroberto	      mask >>= 1;
495181834Sroberto	      msb--;
496181834Sroberto	    }
497181834Sroberto	}
498181834Sroberto
499181834Sroberto      switch (size)
500181834Sroberto	{
501181834Sroberto	case IEEE_SINGLE:
502181834Sroberto	  mantissa_high = 0;
503181834Sroberto	  if (msb >= 32)
504181834Sroberto	    {
505181834Sroberto	      mantissa_low  = (outlfp.l_ui & ((1 << (msb - 32)) - 1)) << (mbits - (msb - 32));
506181834Sroberto	      mantissa_low |=  outlfp.l_uf >> (mbits - (msb - 32));
507181834Sroberto	    }
508181834Sroberto	  else
509181834Sroberto	    {
510181834Sroberto	      mantissa_low  = (outlfp.l_uf << (mbits - msb)) & ((1 << mbits) - 1);
511181834Sroberto	    }
512181834Sroberto	  break;
513181834Sroberto
514181834Sroberto	case IEEE_DOUBLE:
515181834Sroberto	  if (msb >= 32)
516181834Sroberto	    {
517181834Sroberto	      mantissa_high  = (outlfp.l_ui << (mbits - msb)) & ((1 << (mbits - 32)) - 1);
518181834Sroberto	      mantissa_high |=  outlfp.l_uf >> (32 - (mbits - msb));
519181834Sroberto	      mantissa_low   = (outlfp.l_ui & ((1 << (msb - mbits)) - 1)) << (32 - (msb - mbits));
520181834Sroberto	      mantissa_low  |=  outlfp.l_uf >> (msb - mbits);
521181834Sroberto	    }
522181834Sroberto	  else
523181834Sroberto	    {
524181834Sroberto	      mantissa_high  = outlfp.l_uf << (mbits - 32 - msb);
525181834Sroberto	      mantissa_low   = outlfp.l_uf << (mbits - 32);
526181834Sroberto	    }
527181834Sroberto	}
528181834Sroberto
529181834Sroberto#ifdef LIBDEBUG
530181834Sroberto      exponent = msb - 32;
531181834Sroberto      characteristic = exponent + bias;
532181834Sroberto
533181834Sroberto      if (debug > 4)
534181834Sroberto	printf("FP: %s\n", fmt_flt(sign, mantissa_high, mantissa_low, characteristic));
535181834Sroberto#endif
536181834Sroberto    }
537181834Sroberto  return IEEE_OK;
538181834Sroberto}
539181834Sroberto
540181834Sroberto
541181834Sroberto#if defined(DEBUG) && defined(LIBDEBUG)
542181834Srobertoint main(
543181834Sroberto	 int argc,
544181834Sroberto	 char **argv
545181834Sroberto	 )
546181834Sroberto{
547181834Sroberto  static offsets_t native_off = { 0, 1, 2, 3, 4, 5, 6, 7 };
548181834Sroberto  double f = 1.0;
549181834Sroberto  double *f_p = &f;
550181834Sroberto  l_fp fp;
551181834Sroberto
552181834Sroberto  if (argc == 2)
553181834Sroberto    {
554181834Sroberto      if (sscanf(argv[1], "%lf", &f) != 1)
555181834Sroberto	{
556181834Sroberto	  printf("cannot convert %s to a float\n", argv[1]);
557181834Sroberto	  return 1;
558181834Sroberto	}
559181834Sroberto    }
560181834Sroberto
561181834Sroberto  printf("double: %s %s\n", fmt_blong(*(unsigned long *)&f, 32), fmt_blong(*(unsigned long *)((char *)(&f)+4), 32));
562181834Sroberto  printf("fetch from %f = %d\n", f, fetch_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off));
563181834Sroberto  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));
564181834Sroberto  f_p = &f;
565181834Sroberto  put_ieee754((void *)&f_p, IEEE_DOUBLE, &fp, native_off);
566181834Sroberto
567181834Sroberto  return 0;
568181834Sroberto}
569181834Sroberto
570181834Sroberto#endif
571181834Sroberto/*
572181834Sroberto * History:
573181834Sroberto *
574181834Sroberto * ieee754io.c,v
575181834Sroberto * Revision 4.12  2005/04/16 17:32:10  kardel
576181834Sroberto * update copyright
577181834Sroberto *
578181834Sroberto * Revision 4.11  2004/11/14 15:29:41  kardel
579181834Sroberto * support PPSAPI, upgrade Copyright to Berkeley style
580181834Sroberto *
581181834Sroberto * Revision 4.8  1999/02/21 12:17:36  kardel
582181834Sroberto * 4.91f reconcilation
583181834Sroberto *
584181834Sroberto * Revision 4.7  1999/02/21 11:26:03  kardel
585181834Sroberto * renamed index to fieldindex to avoid index() name clash
586181834Sroberto *
587181834Sroberto * Revision 4.6  1998/11/15 20:27:52  kardel
588181834Sroberto * Release 4.0.73e13 reconcilation
589181834Sroberto *
590181834Sroberto * Revision 4.5  1998/08/16 19:01:51  kardel
591181834Sroberto * debug information only compile for LIBDEBUG case
592181834Sroberto *
593181834Sroberto * Revision 4.4  1998/08/09 09:39:28  kardel
594181834Sroberto * Release 4.0.73e2 reconcilation
595181834Sroberto *
596181834Sroberto * Revision 4.3  1998/06/13 11:56:19  kardel
597181834Sroberto * disabled putbute() for the time being
598181834Sroberto *
599181834Sroberto * Revision 4.2  1998/06/12 15:16:58  kardel
600181834Sroberto * ansi2knr compatibility
601181834Sroberto *
602181834Sroberto * Revision 4.1  1998/05/24 07:59:56  kardel
603181834Sroberto * conditional debug support
604181834Sroberto *
605181834Sroberto * Revision 4.0  1998/04/10 19:46:29  kardel
606181834Sroberto * Start 4.0 release version numbering
607181834Sroberto *
608181834Sroberto * Revision 1.1  1998/04/10 19:27:46  kardel
609181834Sroberto * initial NTP VERSION 4 integration of PARSE with GPS166 binary support
610181834Sroberto *
611181834Sroberto * Revision 1.1  1997/10/06 21:05:45  kardel
612181834Sroberto * new parse structure
613181834Sroberto *
614181834Sroberto */
615