Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
c-stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Cowan
c-stuff
Commits
8df8dd36
Commit
8df8dd36
authored
4 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Port to using endian.h
parent
be167130
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
meson.build
+5
-2
5 additions, 2 deletions
meson.build
src/bytedouble.c
+81
-12
81 additions, 12 deletions
src/bytedouble.c
src/meson.build
+4
-1
4 additions, 1 deletion
src/meson.build
with
90 additions
and
15 deletions
meson.build
+
5
−
2
View file @
8df8dd36
# SPDX-FileCopyrightText: 2018, 2019 Bruce Cowan
#
# SPDX-FileCopyrightText: 2018-2020 Bruce Cowan
# SPDX-License-Identifier: CC0-1.0
project
(
'stdlib'
,
'c'
,
...
...
@@ -18,6 +17,10 @@ if cc.has_function('reallocarray', prefix: '#define _GNU_SOURCE\n#include <stdli
conf_data
.
set
(
'HAVE_REALLOCARRAY'
,
1
)
endif
if
cc
.
has_header
(
'endian.h'
)
has_endian_h
=
true
endif
configure_file
(
configuration
:
conf_data
,
output
:
'config.h'
)
config_dep
=
declare_dependency
(
include_directories
:
'.'
)
...
...
This diff is collapsed.
Click to expand it.
src/bytedouble.c
+
81
−
12
View file @
8df8dd36
...
...
@@ -2,31 +2,100 @@
* SPDX-FileCopyrightText: 2020 Bruce Cowan <bruce@bcowan.me.uk>
* SPDX-License-Identifier: Apache-2.0
*/
#include
<endian.h>
#include
<errno.h>
#include
<inttypes.h>
#include
<math.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdio.h>
#include
<string.h>
#define DOUBLE_TO_BYTES(d) ((uint8_t *) &(d))
#define BYTES_TO_DOUBLE(b) (*((double *) (b)))
typedef
union
{
uint8_t
bytes
[
sizeof
(
double
)];
double
d
;
}
DoubleBytes
;
int
main
(
void
)
static
void
bytes_to_be
(
uint8_t
*
bytes
)
{
uint64_t
ret
=
htobe64
((
uint64_t
)
*
(
uint64_t
*
)
bytes
);
memcpy
(
bytes
,
&
ret
,
8
);
}
static
void
bytes_from_be
(
uint8_t
*
bytes
)
{
double
pi
=
M_PI
;
uint64_t
ret
=
be64toh
((
uint64_t
)
*
(
uint64_t
*
)
bytes
);
memcpy
(
bytes
,
&
ret
,
8
);
}
uint8_t
*
bytes
=
DOUBLE_TO_BYTES
(
pi
);
static
void
double_to_be_bytes
(
double
d
,
uint8_t
*
bytes
)
{
DoubleBytes
db
;
db
.
d
=
d
;
bytes_to_be
(
db
.
bytes
);
memcpy
(
bytes
,
db
.
bytes
,
sizeof
(
double
));
}
static
double
be_bytes_to_double
(
uint8_t
*
bytes
)
{
DoubleBytes
db
;
memcpy
(
db
.
bytes
,
bytes
,
sizeof
(
double
));
bytes_from_be
(
db
.
bytes
);
return
db
.
d
;
}
static
void
print_bytes
(
uint8_t
*
bytes
,
size_t
len
)
{
printf
(
"Bytes are ["
);
for
(
in
t
i
=
0
;
i
<
7
;
i
++
)
for
(
size_
t
i
=
0
;
i
<
len
-
1
;
i
++
)
printf
(
"%02"
PRIx8
", "
,
bytes
[
i
]);
printf
(
"%02"
PRIx8
"]
\n
"
,
bytes
[
7
]);
printf
(
"%02"
PRIx8
"]
\n
"
,
bytes
[
len
-
1
]);
}
int
main
(
void
)
{
errno
=
0
;
char
buf
[
32
];
char
*
endptr
;
puts
(
"Please enter a floating point number"
);
fgets
(
buf
,
31
,
stdin
);
double
val
=
strtod
(
buf
,
&
endptr
);
if
(
errno
)
{
perror
(
"strtod"
);
return
EXIT_FAILURE
;
}
if
(
buf
==
endptr
)
{
fputs
(
"No digits found
\n
"
,
stderr
);
return
EXIT_FAILURE
;
}
uint8_t
bytes
[
sizeof
(
double
)];
memcpy
(
bytes
,
&
val
,
sizeof
(
double
));
print_bytes
(
bytes
,
sizeof
(
double
));
double_to_be_bytes
(
val
,
bytes
);
puts
(
"After swapping:"
);
print_bytes
(
bytes
,
sizeof
(
double
));
double
d
=
BYTES_TO_DOUBLE
(
bytes
);
printf
(
"Converting back, the value is %
f
\n
"
,
d
);
double
d
=
be_bytes_to_double
(
bytes
);
printf
(
"Converting back, the value is %
.15g
\n
"
,
d
);
return
0
;
}
This diff is collapsed.
Click to expand it.
src/meson.build
+
4
−
1
View file @
8df8dd36
...
...
@@ -6,7 +6,6 @@ executable('angle', 'angle.c', dependencies: libm)
executable
(
'array-length'
,
'array-length.c'
,
dependencies
:
types_dep
)
executable
(
'binary'
,
'binary.c'
)
executable
(
'binary2'
,
'binary2.c'
,
dependencies
:
utils_dep
)
executable
(
'bytedouble'
,
'bytedouble.c'
)
executable
(
'complex'
,
'complex.c'
,
dependencies
:
libm
)
executable
(
'doubles'
,
'doubles.c'
)
executable
(
'einstein'
,
'einstein.c'
)
...
...
@@ -30,6 +29,10 @@ executable('world', 'world.c')
subdir
(
'sockets'
)
if
has_endian_h
executable
(
'bytedouble'
,
'bytedouble.c'
)
endif
if
openmp_dep
.
found
()
subdir
(
'openmp'
)
endif
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment