1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* shift.c */ |
4 | /* */ |
5 | /* Safe shift routines */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2003 Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | /* According to the C standard, shifting a data type by the number of bits it |
37 | ** has causes undefined behaviour. So |
38 | ** |
39 | ** unsigned long l = 1; |
40 | ** unsigned u =32; |
41 | ** l <<= u; |
42 | ** |
43 | ** may be illegal. The functions in this module behave safely in that respect, |
44 | ** and they use proper casting to distinguish signed from unsigned shifts. |
45 | ** They are not a general purpose replacement for the shift operator! |
46 | */ |
47 | |
48 | |
49 | |
50 | #include <limits.h> |
51 | |
52 | /* common */ |
53 | #include "shift.h" |
54 | |
55 | |
56 | |
57 | /*****************************************************************************/ |
58 | /* Code */ |
59 | /*****************************************************************************/ |
60 | |
61 | |
62 | |
63 | long asl_l (long l, unsigned count) |
64 | /* Arithmetic shift left l by count. */ |
65 | { |
66 | while (1) { |
67 | if (count >= CHAR_BIT * sizeof (l)) { |
68 | l <<= (CHAR_BIT * sizeof (l) - 1); |
69 | count -= (CHAR_BIT * sizeof (l) - 1); |
70 | } else { |
71 | l <<= count; |
72 | break; |
73 | } |
74 | } |
75 | return l; |
76 | } |
77 | |
78 | |
79 | |
80 | long asr_l (long l, unsigned count) |
81 | /* Arithmetic shift right l by count */ |
82 | { |
83 | while (1) { |
84 | if (count >= CHAR_BIT * sizeof (l)) { |
85 | l >>= (CHAR_BIT * sizeof (l) - 1); |
86 | count -= (CHAR_BIT * sizeof (l) - 1); |
87 | } else { |
88 | l >>= count; |
89 | break; |
90 | } |
91 | } |
92 | return l; |
93 | } |
94 | |
95 | |
96 | |
97 | unsigned long shl_l (unsigned long l, unsigned count) |
98 | /* Logical shift left l by count */ |
99 | { |
100 | while (1) { |
101 | if (count >= CHAR_BIT * sizeof (l)) { |
102 | l <<= (CHAR_BIT * sizeof (l) - 1); |
103 | count -= (CHAR_BIT * sizeof (l) - 1); |
104 | } else { |
105 | l <<= count; |
106 | break; |
107 | } |
108 | } |
109 | return l; |
110 | } |
111 | |
112 | |
113 | |
114 | unsigned long shr_l (unsigned long l, unsigned count) |
115 | /* Logical shift right l by count */ |
116 | { |
117 | while (1) { |
118 | if (count >= CHAR_BIT * sizeof (l)) { |
119 | l >>= (CHAR_BIT * sizeof (l) - 1); |
120 | count -= (CHAR_BIT * sizeof (l) - 1); |
121 | } else { |
122 | l >>= count; |
123 | break; |
124 | } |
125 | } |
126 | return l; |
127 | } |
128 | |