GNUnet  0.19.4
common_endian.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001, 2002, 2003, 2004, 2006, 2012 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 
32 #define LOG(kind, ...) GNUNET_log_from (kind, "util-common-endian", __VA_ARGS__)
33 
34 
35 #ifndef htonbe64
36 uint64_t
37 GNUNET_htonll (uint64_t n)
38 {
39 #if __BYTE_ORDER == __BIG_ENDIAN
40  return n;
41 #elif __BYTE_ORDER == __LITTLE_ENDIAN
42  return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32);
43 #else
44  #error byteorder undefined
45 #endif
46 }
47 
48 
49 #endif
50 
51 
52 #ifndef be64toh
53 uint64_t
54 GNUNET_ntohll (uint64_t n)
55 {
56 #if __BYTE_ORDER == __BIG_ENDIAN
57  return n;
58 #elif __BYTE_ORDER == __LITTLE_ENDIAN
59  return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32);
60 #else
61  #error byteorder undefined
62 #endif
63 }
64 
65 
66 #endif
67 
68 
69 double
71 {
72  double res;
73  uint64_t *in = (uint64_t *) &d;
74  uint64_t *out = (uint64_t *) &res;
75 
76  out[0] = GNUNET_htonll (in[0]);
77 
78  return res;
79 }
80 
81 
82 double
84 {
85  double res;
86  uint64_t *in = (uint64_t *) &d;
87  uint64_t *out = (uint64_t *) &res;
88 
89  out[0] = GNUNET_ntohll (in[0]);
90 
91  return res;
92 }
93 
94 
95 /* end of common_endian.c */
static int res
double GNUNET_ntoh_double(double d)
Convert double to host byte order.
Definition: common_endian.c:83
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:54
double GNUNET_hton_double(double d)
Convert double to network byte order.
Definition: common_endian.c:70
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37