orca-software
libc.h
1 #ifndef _LIBC_H
2 #define _LIBC_H
3 
4 #include <stdarg.h>
5 
6 /*
7 constants, tests and transformations
8 */
9 #define NULL ((void *)0)
10 #define isprint(c) (' '<=(c) && (c)<='~')
11 #define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
12 #define isdigit(c) ('0' <= (c) && (c) <= '9')
13 #define islower(c) ('a' <= (c) && (c) <= 'z')
14 #define isupper(c) ('A' <= (c) && (c) <='Z')
15 #define isalpha(c) (islower(c) || isupper(c))
16 #define isalnum(c) (isalpha(c) || isdigit(c))
17 #define min(a,b) ((a) < (b)?(a):(b))
18 
19 #if BIG_ENDIAN
20 
21 #define htons(n) (n)
22 #define ntohs(n) (n)
23 #define htonl(n) (n)
24 #define ntohl(n) (n)
25 
26 #else
27 
28 #define htons(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8))
29 #define ntohs(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8))
30 
31 #define htonl(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
32  ((((uint32_t)(n) & 0xFF00)) << 8) | \
33  ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
34  ((((uint32_t)(n) & 0xFF000000)) >> 24))
35 
36 #define ntohl(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
37  ((((uint32_t)(n) & 0xFF00)) << 8) | \
38  ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
39  ((((uint32_t)(n) & 0xFF000000)) >> 24))
40 #endif
41 
42 #ifdef CPP
43 extern "C" {
44 #endif
45 
46 
47 /*
48 custom C library
49 */
50 int8_t *strcpy(int8_t *dst, const int8_t *src);
51 int8_t *strncpy(int8_t *s1, int8_t *s2, int32_t n);
52 int8_t *strcat(int8_t *dst, const int8_t *src);
53 int8_t *strncat(int8_t *s1, int8_t *s2, int32_t n);
54 int32_t strcmp(const int8_t *s1, const int8_t *s2);
55 int32_t strncmp(int8_t *s1, int8_t *s2, int32_t n);
56 int8_t *strstr(const int8_t *string, const int8_t *find);
57 int32_t strlen(const int8_t *s);
58 int8_t *strchr(const int8_t *s, int32_t c);
59 int8_t *strpbrk(int8_t *str, int8_t *set);
60 int8_t *strsep(int8_t **pp, int8_t *delim);
61 int8_t *strtok(int8_t *s, const int8_t *delim);
62 void *memcpy(void *dst, const void *src, uint32_t n);
63 void *memmove(void *dst, const void *src, uint32_t n);
64 int32_t memcmp(const void *cs, const void *ct, uint32_t n);
65 void *memset(void *s, int32_t c, uint32_t n);
66 int32_t strtol(const int8_t *s, int8_t **end, int32_t base);
67 int32_t atoi(const int8_t *s);
68 float atof(const int8_t *p);
69 int32_t ftoa(float f, int8_t *outbuf, int32_t precision);
70 int8_t *itoa(int32_t i, int8_t *s, int32_t base);
71 int32_t puts(const int8_t *str);
72 int8_t *gets(int8_t *s);
73 int32_t abs(int32_t n);
74 int32_t random(void);
75 void srand(uint32_t seed);
76 int32_t hexdump(int8_t *buf, uint32_t size);
77 int32_t printf(const int8_t *fmt, ...);
78 int32_t sprintf(int8_t *out, const int8_t *fmt, ...);
79 void *malloc(size_t size);
80 void free(void *ptr);
81 void *calloc(uint32_t qty, uint32_t type_size);
82 void *realloc(void *ptr, uint32_t size);
83 
84 #ifdef CPP
85 }
86 #endif
87 
88 
89 /* IEEE single-precision definitions */
90 #define SNG_EXPBITS 8
91 #define SNG_FRACBITS 23
92 #define SNG_EXP_BIAS 127
93 #define SNG_EXP_INFNAN 255
94 #define EXCESS 126
95 #define SIGNBIT 0x80000000
96 #define HIDDEN (1 << 23)
97 #define SIGN(fp) ((fp) & SIGNBIT)
98 #define EXP(fp) (((fp) >> 23) & 0xFF)
99 #define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
100 #define PACK(s,e,m) ((s) | ((e) << 23) | (m))
101 
102 union float_long{
103  float f;
104  int32_t l;
105  uint32_t u;
106 };
107 
108 /* the following deal with IEEE double-precision numbers */
109 #define EXCESSD 1022
110 #define HIDDEND (1 << 20)
111 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
112 #define SIGND(fp) ((fp.l.upper) & SIGNBIT)
113 #define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | (fp.l.lower >> 22))
114 #define HIDDEND_LL ((long long)1 << 52)
115 #define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
116 #define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
117 
118 union double_long {
119  double d;
120  struct {
121 #ifdef LITTLE_ENDIAN
122  unsigned long lower;
123  long upper;
124 #else
125  long upper;
126  unsigned long lower;
127 #endif
128  } l;
129  long long ll;
130 };
131 
132 #endif
Definition: libc.h:118
Definition: hf-printf.h:160