Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
3d937beb
Commit
3d937beb
authored
Jul 18, 2009
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Short types
parent
4e5111fb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
0 deletions
+149
-0
ccan/short_types/_info
ccan/short_types/_info
+80
-0
ccan/short_types/short_types.h
ccan/short_types/short_types.h
+31
-0
ccan/short_types/test/run.c
ccan/short_types/test/run.c
+38
-0
No files found.
ccan/short_types/_info
0 → 100644
View file @
3d937beb
#include <stdio.h>
#include <string.h>
#include "config.h"
/**
* short_types - shorter names for standard integer types
*
* "C is a Spartan language, and so should your naming be."
* -- Linus Torvalds
*
* The short_types header provides for convenient abbreviations for the
* posixly-damned uint32_t types. It also provides be32/le32 for explicitly
* annotating types of specific endian.
*
* Include this header, if only to stop people using these identifiers
* for other things!
*
* Example:
* #include <stdint.h>
* #include <string.h>
* #include <stdio.h>
* #include <ccan/short_types/short_types.h>
*
* // Print nonsensical numerical comparison of POSIX vs. short_types.
* #define stringify_1(x) #x
* #define stringify(x) stringify_1(x)
*
* static void evaluate(size_t size, const char *posix, const char *sht,
* unsigned int *posix_total, unsigned int *sht_total,
* unsigned int *size_total)
* {
* printf("\t%ssigned %s: POSIX %i%%, short %i%%\n",
* sht[0] == 'u' ? "un" : "",
* sht+1,
* strlen(posix)*100 / size,
* strlen(sht)*100 / size);
* *posix_total += strlen(posix);
* *sht_total += strlen(sht);
* *size_total += size;
* }
*
* #define EVALUATE(psx, short, pt, st, t) \
* evaluate(sizeof(psx), stringify(psx), stringify(sht), pt, st, t)
*
* int main(void)
* {
* unsigned int posix_total = 0, sht_total = 0, size_total = 0;
*
* printf("Comparing size of type vs size of name:\n");
*
* EVALUATE(uint8_t, u8, &posix_total, &sht_total, &size_total);
* EVALUATE(int8_t, s8, &posix_total, &sht_total, &size_total);
* EVALUATE(uint16_t, u16, &posix_total, &sht_total, &size_total);
* EVALUATE(int16_t, s16, &posix_total, &sht_total, &size_total);
* EVALUATE(uint32_t, u32, &posix_total, &sht_total, &size_total);
* EVALUATE(int32_t, s32, &posix_total, &sht_total, &size_total);
* EVALUATE(uint64_t, u64, &posix_total, &sht_total, &size_total);
* EVALUATE(int64_t, s64, &posix_total, &sht_total, &size_total);
*
* printf("Conclusion:\n"
* "\tPOSIX is %u%% LESS efficient than binary.\n"
* "\tshort_types.h is %u%% MORE efficient than binary.\n",
* (posix_total - size_total) * 100 / size_total,
* (size_total - sht_total) * 100 / size_total);
* return 0;
* }
*
* Licence: LGPL (2 or any later version)
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
return 0;
}
return 1;
}
ccan/short_types/short_types.h
0 → 100644
View file @
3d937beb
#ifndef CCAN_SHORT_TYPES_H
#define CCAN_SHORT_TYPES_H
#include <stdint.h>
/**
* u64/s64/u32/s32/u16/s16/u8/s8 - short names for explicitly-sized types.
*/
typedef
uint64_t
u64
;
typedef
int64_t
s64
;
typedef
uint32_t
u32
;
typedef
int32_t
s32
;
typedef
uint16_t
u16
;
typedef
int16_t
s16
;
typedef
uint8_t
u8
;
typedef
int8_t
s8
;
/**
* be64/be32/be16 - 64/32/16 bit big-endian representation.
*/
typedef
uint64_t
be64
;
typedef
uint32_t
be32
;
typedef
uint16_t
be16
;
/**
* le64/le32/le16 - 64/32/16 bit little-endian representation.
*/
typedef
uint64_t
le64
;
typedef
uint32_t
le32
;
typedef
uint16_t
le16
;
#endif
/* CCAN_SHORT_TYPES_H */
ccan/short_types/test/run.c
0 → 100644
View file @
3d937beb
#include "short_types/short_types.h"
#include "tap/tap.h"
#include <stdlib.h>
#include <err.h>
int
main
(
int
argc
,
char
*
argv
[])
{
plan_tests
(
22
);
ok1
(
sizeof
(
u64
)
==
8
);
ok1
(
sizeof
(
s64
)
==
8
);
ok1
(
sizeof
(
u32
)
==
4
);
ok1
(
sizeof
(
s32
)
==
4
);
ok1
(
sizeof
(
u16
)
==
2
);
ok1
(
sizeof
(
s16
)
==
2
);
ok1
(
sizeof
(
u8
)
==
1
);
ok1
(
sizeof
(
s8
)
==
1
);
ok1
(
sizeof
(
be64
)
==
8
);
ok1
(
sizeof
(
be32
)
==
4
);
ok1
(
sizeof
(
be16
)
==
2
);
ok1
(
sizeof
(
le64
)
==
8
);
ok1
(
sizeof
(
le32
)
==
4
);
ok1
(
sizeof
(
le16
)
==
2
);
/* Signedness tests. */
ok1
((
u64
)
-
1
>
0
);
ok1
((
u32
)
-
1
>
0
);
ok1
((
u16
)
-
1
>
0
);
ok1
((
u8
)
-
1
>
0
);
ok1
((
s64
)
-
1
<
0
);
ok1
((
s32
)
-
1
<
0
);
ok1
((
s16
)
-
1
<
0
);
ok1
((
s8
)
-
1
<
0
);
return
exit_status
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment