Skip to content
This repository was archived by the owner on Oct 1, 2023. It is now read-only.

Commit eb326bd

Browse files
committed
Use normal include guards
1 parent b3e598f commit eb326bd

File tree

2 files changed

+57
-54
lines changed

2 files changed

+57
-54
lines changed

sar.c

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
/*
22
* Portable C signed arithmetic right shift (sar)
33
*
4-
* The C specification leaves the right shift m >> n
5-
* implementation-defined if m is a negative integer.
6-
* (ISO/IEC 9899:TC2 6.5.7:5, p84-85)
4+
* The C standard makes m >> n implementation-defined if m is negative.
5+
* (ISO C N2176 6.5.7 6.5.7)
76
*
87
* The logical right shift inserts 0s in its high binary bits.
98
*
109
* The arithmetic right shift inserts 1s in the high bits if the target
11-
* m is negative, but inserts 0s otherwise.
12-
*
13-
* This preserves its sign, and makes
14-
* m >> n equal to m/2**n ,
10+
* m is negative, but inserts 0s otherwise. This preserves its sign, so
11+
* m >> n equals m / 2**n ,
1512
* rounded towards -ININITY.
1613
*
1714
* The sar* functions below perform arithmetic right shifts, independent
@@ -27,7 +24,7 @@
2724
* MIT LICENSE Copyright (c) 2020 Rupert Tombs
2825
*/
2926

30-
/* Use external include guards. */
27+
#ifndef SAR_C
3128
#define SAR_C
3229

3330
/*
@@ -47,139 +44,143 @@
4744
* (fix ^ (fix >> n)) twiddles 1s to the n high bits (and 0s
4845
* after) if fix is 1...1, but is 0...0 otherwise.
4946
*
50-
* The mess *(type*)&fixu reinterprets the bits of fixu as signed.
51-
* It is well defined since corresponding (un-)signed types are an
52-
* exception to strict aliasing rules.
47+
* The *(type*) &fixu pun reinterprets the bits of fixu as signed.
48+
* It is well defined since corresponding (un-)signed types are an exception
49+
* to strict-aliasing rules.
5350
*/
54-
#define SARBODY(type, utype) \
55-
const int logical = (((type)-1) >> 1) > 0; \
56-
utype fixu = -(logical & (m < 0)); \
57-
type fix = *(type*)&fixu; \
51+
#define SARBODY(type, utype) \
52+
const int logical = (((type)-1) >> 1) > 0; \
53+
utype fixu = -(logical & (m < 0)); \
54+
type fix = *(type*) &fixu; \
5855
return (m >> n) | (fix ^ (fix >> n))
5956

6057

61-
static signed char
58+
static inline signed char
6259
sarc(signed char m, uint_fast8_t n)
6360
{
6461
SARBODY(signed char, unsigned char);
6562
}
6663

67-
static short int
64+
static inline short int
6865
sars(short int m, uint_fast8_t n)
6966
{
7067
SARBODY(short int, unsigned short int);
7168
}
7269

73-
static int
70+
static inline int
7471
sari(int m, uint_fast8_t n)
7572
{
7673
SARBODY(int, unsigned int);
7774
}
7875

79-
static long int
76+
static inline long int
8077
sarl(long int m, uint_fast8_t n)
8178
{
8279
SARBODY(long int, unsigned long int);
8380
}
8481

85-
static long long int
82+
static inline long long int
8683
sarll(long long int m, uint_fast8_t n)
8784
{
8885
SARBODY(long long int, unsigned long long int);
8986
}
9087

9188
/* included in stdint.h */
92-
static int_fast8_t
89+
#ifdef UINT_FAST8_MAX
90+
static inline int_fast8_t
9391
sarfast8(int_fast8_t m, uint_fast8_t n)
9492
{
9593
SARBODY(int_fast8_t, uint_fast8_t);
9694
}
9795

98-
static int_fast16_t
96+
static inline int_fast16_t
9997
sarfast16(int_fast16_t m, uint_fast8_t n)
10098
{
10199
SARBODY(int_fast16_t, uint_fast16_t);
102100
}
103101

104-
static int_fast32_t
102+
static inline int_fast32_t
105103
sarfast32(int_fast32_t m, uint_fast8_t n)
106104
{
107105
SARBODY(int_fast32_t, uint_fast32_t);
108106
}
109107

110-
static int_fast64_t
108+
static inline int_fast64_t
111109
sarfast64(int_fast64_t m, uint_fast8_t n)
112110
{
113111
SARBODY(int_fast64_t, uint_fast64_t);
114112
}
115113

116-
static int_least8_t
114+
static inline int_least8_t
117115
sarleast8(int_least8_t m, uint_fast8_t n)
118116
{
119117
SARBODY(int_least8_t, uint_least8_t);
120118
}
121119

122-
static int_least16_t
120+
static inline int_least16_t
123121
sarleast16(int_least16_t m, uint_fast8_t n)
124122
{
125123
SARBODY(int_least16_t, uint_least16_t);
126124
}
127125

128-
static int_least32_t
126+
static inline int_least32_t
129127
sarleast32(int_least32_t m, uint_fast8_t n)
130128
{
131129
SARBODY(int_least32_t, uint_least32_t);
132130
}
133131

134-
static int_least64_t
132+
static inline int_least64_t
135133
sarleast64(int_least64_t m, uint_fast8_t n)
136134
{
137135
SARBODY(int_least64_t, uint_least64_t);
138136
}
139137

140-
static intmax_t
138+
static inline intmax_t
141139
sarmax(intmax_t m, uint_fast8_t n)
142140
{
143141
SARBODY(intmax_t, uintmax_t);
144142
}
143+
#endif /* ifdef UINT_FAST8_MAX */
145144

146145
/* optionally included in stdint.h */
147146
#ifdef INT8_MAX
148-
static int8_t
147+
static inline int8_t
149148
sar8(int8_t m, uint_fast8_t n)
150149
{
151150
SARBODY(int8_t, uint8_t);
152151
}
153152
#endif
154153

155154
#ifdef INT16_MAX
156-
static int16_t
155+
static inline int16_t
157156
sar16(int16_t m, uint_fast8_t n)
158157
{
159158
SARBODY(int16_t, uint16_t);
160159
}
161160
#endif
162161

163162
#ifdef INT32_MAX
164-
static int32_t
163+
static inline int32_t
165164
sar32(int32_t m, uint_fast8_t n)
166165
{
167166
SARBODY(int32_t, uint32_t);
168167
}
169168
#endif
170169

171170
#ifdef INT64_MAX
172-
static int64_t
171+
static inline int64_t
173172
sar64(int64_t m, uint_fast8_t n)
174173
{
175174
SARBODY(int64_t, uint64_t);
176175
}
177176
#endif
178177

179178
#ifdef INTPTR_MAX
180-
static intptr_t
179+
static inline intptr_t
181180
sarptr(intptr_t m, uint_fast8_t n)
182181
{
183182
SARBODY(intptr_t, uintptr_t);
184183
}
185184
#endif
185+
186+
#endif /* ifndef SAR_C */

saru.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* MIT LICENSE Copyright (c) 2020 Rupert Tombs
1111
*/
1212

13-
/* Use external include guards. */
13+
#ifndef SARU_C
1414
#define SARU_C
1515

1616
/*
@@ -25,128 +25,130 @@
2525
return *(type*)&saru;
2626

2727

28-
static signed char
28+
static inline signed char
2929
saruc(signed char m, uint_fast8_t n)
3030
{
3131
SARUBODY(signed char, unsigned char);
3232
}
3333

34-
static short int
34+
static inline short int
3535
sarus(short int m, uint_fast8_t n)
3636
{
3737
SARUBODY(short int, unsigned short int);
3838
}
3939

40-
static int
40+
static inline int
4141
sarui(int m, uint_fast8_t n)
4242
{
4343
SARUBODY(int, unsigned int);
4444
}
4545

46-
static long int
46+
static inline long int
4747
sarul(long int m, uint_fast8_t n)
4848
{
4949
SARUBODY(long int, unsigned long int);
5050
}
5151

52-
static long long int
52+
static inline long long int
5353
sarull(long long int m, uint_fast8_t n)
5454
{
5555
SARUBODY(long long int, unsigned long long int);
5656
}
5757

5858
/* included in stdint.h */
59-
static int_fast8_t
59+
static inline int_fast8_t
6060
sarufast8(int_fast8_t m, uint_fast8_t n)
6161
{
6262
SARUBODY(int_fast8_t, uint_fast8_t);
6363
}
6464

65-
static int_fast16_t
65+
static inline int_fast16_t
6666
sarufast16(int_fast16_t m, uint_fast8_t n)
6767
{
6868
SARUBODY(int_fast16_t, uint_fast16_t);
6969
}
7070

71-
static int_fast32_t
71+
static inline int_fast32_t
7272
sarufast32(int_fast32_t m, uint_fast8_t n)
7373
{
7474
SARUBODY(int_fast32_t, uint_fast32_t);
7575
}
7676

77-
static int_fast64_t
77+
static inline int_fast64_t
7878
sarufast64(int_fast64_t m, uint_fast8_t n)
7979
{
8080
SARUBODY(int_fast64_t, uint_fast64_t);
8181
}
8282

83-
static int_least8_t
83+
static inline int_least8_t
8484
saruleast8(int_least8_t m, uint_fast8_t n)
8585
{
8686
SARUBODY(int_least8_t, uint_least8_t);
8787
}
8888

89-
static int_least16_t
89+
static inline int_least16_t
9090
saruleast16(int_least16_t m, uint_fast8_t n)
9191
{
9292
SARUBODY(int_least16_t, uint_least16_t);
9393
}
9494

95-
static int_least32_t
95+
static inline int_least32_t
9696
saruleast32(int_least32_t m, uint_fast8_t n)
9797
{
9898
SARUBODY(int_least32_t, uint_least32_t);
9999
}
100100

101-
static int_least64_t
101+
static inline int_least64_t
102102
saruleast64(int_least64_t m, uint_fast8_t n)
103103
{
104104
SARUBODY(int_least64_t, uint_least64_t);
105105
}
106106

107-
static intmax_t
107+
static inline intmax_t
108108
sarumax(intmax_t m, uint_fast8_t n)
109109
{
110110
SARUBODY(intmax_t, uintmax_t);
111111
}
112112

113113
/* optionally included in stdint.h */
114114
#ifdef INT8_MAX
115-
static int8_t
115+
static inline int8_t
116116
saru8(int8_t m, uint_fast8_t n)
117117
{
118118
SARUBODY(int8_t, uint8_t);
119119
}
120120
#endif
121121

122122
#ifdef INT16_MAX
123-
static int16_t
123+
static inline int16_t
124124
saru16(int16_t m, uint_fast8_t n)
125125
{
126126
SARUBODY(int16_t, uint16_t);
127127
}
128128
#endif
129129

130130
#ifdef INT32_MAX
131-
static int32_t
131+
static inline int32_t
132132
saru32(int32_t m, uint_fast8_t n)
133133
{
134134
SARUBODY(int32_t, uint32_t);
135135
}
136136
#endif
137137

138138
#ifdef INT64_MAX
139-
static int64_t
139+
static inline int64_t
140140
saru64(int64_t m, uint_fast8_t n)
141141
{
142142
SARUBODY(int64_t, uint64_t);
143143
}
144144
#endif
145145

146146
#ifdef INTPTR_MAX
147-
static intptr_t
147+
static inline intptr_t
148148
saruptr(intptr_t m, uint_fast8_t n)
149149
{
150150
SARUBODY(intptr_t, uintptr_t);
151151
}
152152
#endif
153+
154+
#endif /* ifdef SARU_C */

0 commit comments

Comments
 (0)