-
Notifications
You must be signed in to change notification settings - Fork 3
/
number.h
91 lines (73 loc) · 2.13 KB
/
number.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
/*
Copyright 2008-2010 Ostap Cherkashin
Copyright 2008-2010 Julius Chrobak
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
static const unsigned int MAX_INT = 0x7FFFFFFFU;
static const unsigned int MAX_UINT = 0xFFFFFFFFU;
static const unsigned long long MAX_LONG = 0x7FFFFFFFFFFFFFFFULL;
static const unsigned long long MAX_ULONG = 0xFFFFFFFFFFFFFFFFULL;
static void int_enc(void *buf, int val)
{
*((int*) buf) = val;
}
static int int_dec(const void *buf)
{
return *((int*) buf);
}
static int int_overflow(int sign, unsigned int val)
{
if (sign > 0 && val > MAX_INT)
return 1;
else if (sign < 0 && val > MAX_INT + 1)
return 1;
return 0;
}
static void long_enc(void *buf, long long val)
{
*((long long*) buf) = val;
}
static long long long_dec(const void *buf)
{
return *((long long*) buf);
}
static unsigned long long ulong_mul(unsigned long long si1,
unsigned long long si2,
int *error)
{
if (si2 > 0ULL && si1 > MAX_ULONG / si2)
*error = 1;
return si1 * si2;
}
static unsigned long long ulong_add(unsigned long long si1,
unsigned long long si2,
int *error)
{
if (si1 > MAX_ULONG - si2)
*error = 1;
return si1 + si2;
}
static int long_overflow(int sign, unsigned long long val)
{
if (sign > 0 && val > MAX_LONG)
return 1;
else if (sign < 0 && val > MAX_LONG + 1)
return 1;
return 0;
}
static void real_enc(void *buf, double val)
{
*((double*) buf) = val;
}
static double real_dec(const void *buf)
{
return *((double*) buf);
}