BRL-CAD
htonf.c
Go to the documentation of this file.
1 /* H T O N F . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #ifdef HAVE_MEMORY_H
24 # include <memory.h>
25 #endif
26 #include <stdio.h>
27 #include <assert.h>
28 
29 #include "bu/cv.h"
30 #include "bu/endian.h"
31 #include "bu/log.h"
32 
33 void
34 bu_cv_htonf(register unsigned char *out, register const unsigned char *in, size_t count)
35 {
36  register size_t i;
37 
38  assert(sizeof(float) == SIZEOF_NETWORK_FLOAT);
39 
40  switch (bu_byteorder()) {
41  case BU_BIG_ENDIAN:
42  /*
43  * First, the case where the system already operates in
44  * IEEE format internally, using big-endian order. These
45  * are the lucky ones.
46  */
47  memcpy(out, in, count*SIZEOF_NETWORK_FLOAT);
48  return;
49  case BU_LITTLE_ENDIAN:
50  /*
51  * This machine uses IEEE, but in little-endian byte order
52  */
53  for (i=count; i > 0; i--) {
54  *out++ = in[3];
55  *out++ = in[2];
56  *out++ = in[1];
57  *out++ = in[0];
59  }
60  return;
61  default:
62  /* nada */
63  break;
64  }
65 
66  bu_bomb("bu_ntohf.c: ERROR, no NtoHD conversion for this machine type\n");
67 }
68 
69 
70 void
71 bu_cv_ntohf(register unsigned char *out, register const unsigned char *in, size_t count)
72 {
73  register size_t i;
74 
75  assert(sizeof(float) == SIZEOF_NETWORK_FLOAT);
76 
77  switch (bu_byteorder()) {
78  case BU_BIG_ENDIAN:
79  /*
80  * First, the case where the system already operates in
81  * IEEE format internally, using big-endian order. These
82  * are the lucky ones.
83  */
84  memcpy(out, in, count*SIZEOF_NETWORK_FLOAT);
85  return;
86  case BU_LITTLE_ENDIAN:
87  /*
88  * This machine uses IEEE, but in little-endian byte order
89  */
90  for (i=count; i > 0; i--) {
91  *out++ = in[3];
92  *out++ = in[2];
93  *out++ = in[1];
94  *out++ = in[0];
96  }
97  return;
98  default:
99  /* nada */
100  break;
101  }
102 
103  bu_bomb("bu_ntohf.c: ERROR, no NtoHD conversion for this machine type\n");
104 }
105 
106 /*
107  * Local Variables:
108  * mode: C
109  * tab-width: 8
110  * indent-tabs-mode: t
111  * c-file-style: "stroustrup"
112  * End:
113  * ex: shiftwidth=4 tabstop=8
114  */
bu_endian_t bu_byteorder(void)
Definition: endian.c:27
Header file for the BRL-CAD common definitions.
void bu_cv_ntohf(register unsigned char *out, register const unsigned char *in, size_t count)
Definition: htonf.c:71
void bu_cv_htonf(register unsigned char *out, register const unsigned char *in, size_t count)
Definition: htonf.c:34
goto out
Definition: nmg_mod.c:3846
#define SIZEOF_NETWORK_FLOAT
Definition: cv.h:47
void bu_bomb(const char *str) _BU_ATTR_NORETURN
Definition: bomb.c:91