-
Notifications
You must be signed in to change notification settings - Fork 0
/
to16.h
106 lines (104 loc) · 3.08 KB
/
to16.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
static inline int16_t i64_ToI16(int64_t i){
if(i>INT16_MAX || i<INT16_MIN){
//fputs("Could not convert iX to i16.\n",stderr);
//exit(-1);
return 0;
}
return (int16_t)i;
}
static inline int16_t u64_ToI16(uint64_t i){
if(i>INT16_MAX){
//fputs("Could not convert uX to i16.\n",stderr);
//exit(-1);
return 0;
}
return (int16_t)i;
}
static inline int16_t i8_ToI16(int8_t i){ return (int16_t)i; }
static inline int16_t u8_ToI16(uint8_t i){ return (int16_t)i; }
static inline int16_t i16_ToI16(int16_t i){ return i; }
static inline int16_t u16_ToI16(uint16_t i){ return u64_ToI16(i); }
static inline int16_t i32_ToI16(int32_t i){ return i64_ToI16(i); }
static inline int16_t u32_ToI16(uint32_t i){ return u64_ToI16(i); }
static inline int16_t flong_ToI16(long double f){
f = roundl(f);
if(f>INT16_MAX || f<INT16_MIN){
//fputs("Could not convert fX to i16.\n",stderr);
//exit(-1);
return 0;
}
return (int16_t)f;
}
static inline int16_t f32_ToI16(float f){ return flong_ToI16(f); }
static inline int16_t f64_ToI16(double f){ return flong_ToI16(f); }
static inline int16_t invalid_ToI16(){ return (int16_t)0; }
#define ToI16(x)\
_Generic((x),\
int8_t: i8_ToI16,\
uint8_t: u8_ToI16,\
int16_t: i16_ToI16,\
uint16_t: u16_ToI16,\
int32_t: i32_ToI16,\
uint32_t: u32_ToI16,\
int64_t: i64_ToI16,\
uint64_t: u64_ToI16,\
float: f32_ToI16,\
double: f64_ToI16,\
long double: flong_ToI16,\
default: invalid_ToI16\
)((x))
//unsigned
static inline uint16_t i64_ToU16(int64_t i){
if(i>UINT16_MAX || i<0){
//fputs("Could not convert iX to u16.\n",stderr);
//exit(-1);
return 0;
}
return (uint16_t)i;
}
static inline uint16_t u64_ToU16(uint64_t i){
if(i>UINT16_MAX){
//fputs("Could not convert uX to u16.\n",stderr);
//exit(-1);
return 0;
}
return (uint16_t)i;
}
static inline uint16_t i8_ToU16(int8_t i){ return i64_ToU16(i); }
static inline uint16_t u8_ToU16(uint8_t i){ return (uint16_t)i; }
static inline uint16_t i16_ToU16(int16_t i){ return i64_ToU16(i); }
static inline uint16_t u16_ToU16(uint16_t i){ return i; }
static inline uint16_t i32_ToU16(int32_t i){ return i64_ToU16(i); }
static inline uint16_t u32_ToU16(uint32_t i){ return u64_ToU16(i); }
static inline uint16_t flong_ToU16(long double f){
f = roundl(f);
if(f>UINT16_MAX || f<0.0){
//fputs("Could not convert fX to u8.\n",stderr);
//exit(-1);
return 0;
}
return (uint16_t)f;
}
static inline uint16_t f32_ToU16(float f){ return flong_ToU16(f); }
static inline uint16_t f64_ToU16(double f){ return flong_ToU16(f); }
static inline uint16_t invalid_ToU16(){ return (uint16_t)0; }
#define ToU16(x)\
_Generic((x),\
int8_t: i8_ToU16,\
uint8_t: u8_ToU16,\
int16_t: i16_ToU16,\
uint16_t: u16_ToU16,\
int32_t: i32_ToU16,\
uint32_t: u32_ToU16,\
int64_t: i64_ToU16,\
uint64_t: u64_ToU16,\
float: f32_ToU16,\
double: f64_ToU16,\
long double: flong_ToU16,\
default: invalid_ToU16\
)((x))