cdcontainers
0.1.1
Library of data containers and collections for C programming language.
cdcontainers
include
cdcontainers
casts.h
Go to the documentation of this file.
1
// The MIT License (MIT)
2
// Copyright (c) 2018 Maksim Andrianov
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
// of this software and associated documentation files (the "Software"), to
6
// deal in the Software without restriction, including without limitation the
7
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
// sell copies of the Software, and to permit persons to whom the Software is
9
// furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
// IN THE SOFTWARE.
21
#ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_CASTS_H
22
#define CDCONTAINERS_INCLUDE_CDCONTAINERS_CASTS_H
23
24
#include <float.h>
25
#include <limits.h>
26
#include <stddef.h>
27
#include <stdint.h>
28
29
#define CDC_TO_CHAR(p) ((char)(intptr_t)(p))
30
#define CDC_FROM_CHAR(s) ((void *)(intptr_t)(char)(s))
31
32
#define CDC_TO_SCHAR(p) ((signed char)(intptr_t)(p))
33
#define CDC_FROM_SCHAR(s) ((void *)(intptr_t)(signed char)(s))
34
35
#define CDC_TO_UCHAR(p) ((unsigned char)(uintptr_t)(p))
36
#define CDC_FROM_UCHAR(s) ((void *)(uintptr_t)(unsigned char)(s))
37
38
#define CDC_TO_SHORT(p) ((short)(intptr_t)(p))
39
#define CDC_FROM_SHORT(s) ((void *)(intptr_t)(short)(s))
40
41
#define CDC_TO_USHORT(p) ((unsigned short)(uintptr_t)(p))
42
#define CDC_FROM_USHORT(s) ((void *)(uintptr_t)(unsigned short)(s))
43
44
#define CDC_TO_INT(p) ((int)(intptr_t)(p))
45
#define CDC_FROM_INT(s) ((void *)(intptr_t)(int)(s))
46
47
#define CDC_TO_UINT(p) ((unsigned int)(uintptr_t)(p))
48
#define CDC_FROM_UINT(s) ((void *)(uintptr_t)(unsigned int)(s))
49
50
#define CDC_TO_SIZE(p) ((size_t)(uintptr_t)(p))
51
#define CDC_FROM_SIZE(s) ((void *)(uintptr_t)(size_t)(s))
52
53
#if UINTPTR_MAX >= ULONG_MAX
54
#define CDC_TO_LONG(p) ((long)(intptr_t)(p))
55
#define CDC_FROM_LONG(s) ((void *)(intptr_t)(long)(s))
56
#define CDC_LONG_CAST
57
58
#define CDC_TO_ULONG(p) ((unsigned long)(uintptr_t)(p))
59
#define CDC_FROM_ULONG(s) ((void *)(uintptr_t)(unsigned long)(s))
60
#define CDC_ULONG_CAST
61
#endif
62
63
#if (defined(__GNUC__)) && defined(__SIZEOF_POINTER__)
64
#if defined(__SIZEOF_FLOAT__) && (__SIZEOF_POINTER__ >= __SIZEOF_FLOAT__)
65
union
cdc_union_float_ptr {
66
void
*ptr;
67
float
flt;
68
};
69
70
static
inline
void
*cdc_float_to_ptr(
float
flt)
71
{
72
union
cdc_union_float_ptr u;
73
u.flt = flt;
74
return
u.ptr;
75
}
76
77
static
inline
float
cdc_ptr_to_float(
void
*ptr)
78
{
79
union
cdc_union_float_ptr u;
80
u.ptr = ptr;
81
return
u.flt;
82
}
83
84
#define CDC_TO_FLOAT(p) cdc_ptr_to_float(p)
85
#define CDC_FROM_FLOAT(s) cdc_float_to_ptr(s)
86
#define CDC_FLOAT_CAST
87
#endif
88
89
#if defined(__SIZEOF_DOUBLE__) && (__SIZEOF_POINTER__ >= __SIZEOF_DOUBLE__)
90
union
cdc_union_double_ptr {
91
void
*ptr;
92
double
dbl;
93
};
94
95
static
inline
void
*cdc_double_to_ptr(
double
dbl)
96
{
97
union
cdc_union_double_ptr u;
98
u.dbl = dbl;
99
return
u.ptr;
100
}
101
102
static
inline
double
cdc_ptr_to_double(
void
*ptr)
103
{
104
union
cdc_union_double_ptr u;
105
u.ptr = ptr;
106
return
u.dbl;
107
}
108
109
#define CDC_TO_DOUBLE(p) cdc_ptr_to_double(p)
110
#define CDC_FROM_DOUBLE(s) cdc_double_to_ptr(s)
111
#define CDC_DOUBLE_CAST
112
#endif
113
#endif
114
115
#endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_CASTS_H
Generated by
1.8.13