1 |
dashley |
71 |
//$Header$ |
2 |
|
|
//------------------------------------------------------------------------------------------------- |
3 |
|
|
//This file is part of "David T. Ashley's Shared Source Code", a set of shared components |
4 |
|
|
//integrated into many of David T. Ashley's projects. |
5 |
|
|
//------------------------------------------------------------------------------------------------- |
6 |
|
|
//This source code and any program in which it is compiled/used is provided under the MIT License, |
7 |
|
|
//reproduced below. |
8 |
|
|
//------------------------------------------------------------------------------------------------- |
9 |
|
|
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
10 |
|
|
//this software and associated documentation files(the "Software"), to deal in the |
11 |
|
|
//Software without restriction, including without limitation the rights to use, |
12 |
|
|
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
13 |
|
|
//Software, and to permit persons to whom the Software is furnished to do so, |
14 |
|
|
//subject to the following conditions : |
15 |
|
|
// |
16 |
|
|
//The above copyright notice and this permission notice shall be included in all |
17 |
|
|
//copies or substantial portions of the Software. |
18 |
|
|
// |
19 |
|
|
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
|
|
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
|
|
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
22 |
|
|
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
|
|
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
|
|
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
|
|
//SOFTWARE. |
26 |
|
|
//------------------------------------------------------------------------------------------------- |
27 |
|
|
#define MODULE_ESRG_INT_DEF |
28 |
|
|
|
29 |
|
|
#include <assert.h> |
30 |
|
|
#include <string.h> |
31 |
|
|
|
32 |
|
|
#include "esrg_int_def.h" |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
void ESRG_INT_DEF_Create( ESRG_INT_DEF_INT *arg, int is_signed ) |
36 |
|
|
{ |
37 |
|
|
assert(arg != NULL); |
38 |
|
|
|
39 |
|
|
memset(arg, 0, sizeof(ESRG_INT_DEF_INT)); |
40 |
|
|
|
41 |
|
|
if (is_signed) |
42 |
|
|
{ |
43 |
|
|
arg->flags = ESRG_INT_DEF_FLAG_IS_SIGNED; |
44 |
|
|
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
void ESRG_INT_DEF_AssignU( ESRG_INT_DEF_INT *arg, unsigned int aval) |
49 |
|
|
{ |
50 |
|
|
assert(arg != NULL); |
51 |
|
|
|
52 |
|
|
//Reset any pending error flags. An assignment starts fresh. |
53 |
|
|
arg->flags &= ~(ESRG_INT_DEF_FLAG_NEG_ROLL | ESRG_INT_DEF_FLAG_POS_ROLL | ESRG_INT_DEF_FLAG_NAN); |
54 |
|
|
|
55 |
|
|
arg->limbs[0] = aval; |
56 |
|
|
memset(&(arg->limbs[1]), 0, sizeof(arg->limbs[0]) * (ESRG_INT_DEF_NLIMBS-1)); |
57 |
|
|
|
58 |
|
|
//The only circumstance under which there is an overflow |
59 |
|
|
//is if the value is inherently signed and if the limbs per |
60 |
|
|
//int is 1 and the MSB is set. |
61 |
|
|
if (arg->flags & ESRG_INT_DEF_FLAG_IS_SIGNED) |
62 |
|
|
{ |
63 |
|
|
if (ESRG_INT_DEF_NLIMBS == 1) |
64 |
|
|
{ |
65 |
|
|
if (aval & (1 << (ESRG_INT_DEF_NATIVE_BITSIZE-1))) |
66 |
|
|
{ |
67 |
|
|
arg->flags |= ESRG_INT_DEF_FLAG_POS_ROLL; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
void ESRG_INT_DEF_AssignS( ESRG_INT_DEF_INT *arg, int aval) |
75 |
|
|
{ |
76 |
|
|
assert(arg != NULL); |
77 |
|
|
|
78 |
|
|
//Reset any pending error flags. An assignment starts fresh. |
79 |
|
|
arg->flags &= ~(ESRG_INT_DEF_FLAG_NEG_ROLL | ESRG_INT_DEF_FLAG_POS_ROLL | ESRG_INT_DEF_FLAG_NAN); |
80 |
|
|
|
81 |
|
|
arg->limbs[0] = aval; |
82 |
|
|
//Assign the base limb. |
83 |
|
|
|
84 |
|
|
//Deal with the sign extension. If the number is not signed, |
85 |
|
|
//don't extend at all. If the number is signed, extend. |
86 |
|
|
if (arg->flags & ESRG_INT_DEF_FLAG_IS_SIGNED) |
87 |
|
|
{ |
88 |
|
|
if (aval < 0) |
89 |
|
|
memset(&(arg->limbs[1]), -1, sizeof(arg->limbs[0]) * (ESRG_INT_DEF_NLIMBS-1)); |
90 |
|
|
else |
91 |
|
|
memset(&(arg->limbs[1]), 0, sizeof(arg->limbs[0]) * (ESRG_INT_DEF_NLIMBS-1)); |
92 |
|
|
} |
93 |
|
|
else |
94 |
|
|
{ |
95 |
|
|
memset(&(arg->limbs[1]), 0, sizeof(arg->limbs[0]) * (ESRG_INT_DEF_NLIMBS-1)); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
//The only circumstance under which there is an overflow |
99 |
|
|
//is if the value is inherently unsigned and if the limbs per |
100 |
|
|
//int is 1 and the MSB is set. |
101 |
|
|
if (!(arg->flags & ESRG_INT_DEF_FLAG_IS_SIGNED)) |
102 |
|
|
{ |
103 |
|
|
if (ESRG_INT_DEF_NLIMBS == 1) |
104 |
|
|
{ |
105 |
|
|
if (aval & (1 << (ESRG_INT_DEF_NATIVE_BITSIZE-1))) |
106 |
|
|
{ |
107 |
|
|
arg->flags |= ESRG_INT_DEF_FLAG_POS_ROLL; |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
|
114 |
|
|
int ESRG_INT_DEF_Cmp ( ESRG_INT_DEF_INT *a, ESRG_INT_DEF_INT *b ) |
115 |
|
|
{ |
116 |
|
|
assert(a != NULL); |
117 |
|
|
assert(b != NULL); |
118 |
|
|
|
119 |
|
|
return(0); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
//End of esrg_int_def.c. |