|
62 | 62 |
|
63 | 63 | #include <stdint.h>
|
64 | 64 | #include <stddef.h>
|
| 65 | +#include <limits.h> |
65 | 66 | #define int128_t __int128
|
66 | 67 | #define uint128_t unsigned __int128
|
67 | 68 | ZIG_EXTERN_C void *memcpy (void *ZIG_RESTRICT, const void *ZIG_RESTRICT, size_t);
|
68 | 69 |
|
| 70 | +static inline uint8_t zig_addw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { |
| 71 | + uint8_t thresh = max - rhs; |
| 72 | + if (lhs > thresh) { |
| 73 | + return lhs - thresh - 1; |
| 74 | + } else { |
| 75 | + return lhs + rhs; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +static inline int8_t zig_addw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { |
| 80 | + if ((lhs > 0) && (rhs > 0)) { |
| 81 | + int8_t thresh = max - rhs; |
| 82 | + if (lhs > thresh) { |
| 83 | + return min + lhs - thresh - 1; |
| 84 | + } |
| 85 | + } else if ((lhs < 0) && (rhs < 0)) { |
| 86 | + int8_t thresh = min - rhs; |
| 87 | + if (lhs < thresh) { |
| 88 | + return max + lhs - thresh + 1; |
| 89 | + } |
| 90 | + } |
| 91 | + return lhs + rhs; |
| 92 | +} |
| 93 | + |
| 94 | +static inline uint16_t zig_addw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { |
| 95 | + uint16_t thresh = max - rhs; |
| 96 | + if (lhs > thresh) { |
| 97 | + return lhs - thresh - 1; |
| 98 | + } else { |
| 99 | + return lhs + rhs; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +static inline int16_t zig_addw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { |
| 104 | + if ((lhs > 0) && (rhs > 0)) { |
| 105 | + int16_t thresh = max - rhs; |
| 106 | + if (lhs > thresh) { |
| 107 | + return min + lhs - thresh - 1; |
| 108 | + } |
| 109 | + } else if ((lhs < 0) && (rhs < 0)) { |
| 110 | + int16_t thresh = min - rhs; |
| 111 | + if (lhs < thresh) { |
| 112 | + return max + lhs - thresh + 1; |
| 113 | + } |
| 114 | + } |
| 115 | + return lhs + rhs; |
| 116 | +} |
| 117 | + |
| 118 | +static inline uint32_t zig_addw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { |
| 119 | + uint32_t thresh = max - rhs; |
| 120 | + if (lhs > thresh) { |
| 121 | + return lhs - thresh - 1; |
| 122 | + } else { |
| 123 | + return lhs + rhs; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +static inline int32_t zig_addw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { |
| 128 | + if ((lhs > 0) && (rhs > 0)) { |
| 129 | + int32_t thresh = max - rhs; |
| 130 | + if (lhs > thresh) { |
| 131 | + return min + lhs - thresh - 1; |
| 132 | + } |
| 133 | + } else if ((lhs < 0) && (rhs < 0)) { |
| 134 | + int32_t thresh = min - rhs; |
| 135 | + if (lhs < thresh) { |
| 136 | + return max + lhs - thresh + 1; |
| 137 | + } |
| 138 | + } |
| 139 | + return lhs + rhs; |
| 140 | +} |
| 141 | + |
| 142 | +static inline uint64_t zig_addw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { |
| 143 | + uint64_t thresh = max - rhs; |
| 144 | + if (lhs > thresh) { |
| 145 | + return lhs - thresh - 1; |
| 146 | + } else { |
| 147 | + return lhs + rhs; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +static inline int64_t zig_addw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { |
| 152 | + if ((lhs > 0) && (rhs > 0)) { |
| 153 | + int64_t thresh = max - rhs; |
| 154 | + if (lhs > thresh) { |
| 155 | + return min + lhs - thresh - 1; |
| 156 | + } |
| 157 | + } else if ((lhs < 0) && (rhs < 0)) { |
| 158 | + int64_t thresh = min - rhs; |
| 159 | + if (lhs < thresh) { |
| 160 | + return max + lhs - thresh + 1; |
| 161 | + } |
| 162 | + } |
| 163 | + return lhs + rhs; |
| 164 | +} |
| 165 | + |
| 166 | +static inline intptr_t zig_addw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { |
| 167 | + return (intptr_t)(((uintptr_t)lhs) + ((uintptr_t)rhs)); |
| 168 | +} |
| 169 | + |
| 170 | +static inline short zig_addw_short(short lhs, short rhs, short min, short max) { |
| 171 | + return (short)(((unsigned short)lhs) + ((unsigned short)rhs)); |
| 172 | +} |
| 173 | + |
| 174 | +static inline int zig_addw_int(int lhs, int rhs, int min, int max) { |
| 175 | + return (int)(((unsigned)lhs) + ((unsigned)rhs)); |
| 176 | +} |
| 177 | + |
| 178 | +static inline long zig_addw_long(long lhs, long rhs, long min, long max) { |
| 179 | + return (long)(((unsigned long)lhs) + ((unsigned long)rhs)); |
| 180 | +} |
| 181 | + |
| 182 | +static inline long long zig_addw_longlong(long long lhs, long long rhs, long long min, long long max) { |
| 183 | + return (long long)(((unsigned long long)lhs) + ((unsigned long long)rhs)); |
| 184 | +} |
| 185 | + |
| 186 | +static inline uint8_t zig_subw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { |
| 187 | + if (lhs < rhs) { |
| 188 | + return max - rhs - lhs + 1; |
| 189 | + } else { |
| 190 | + return lhs - rhs; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +static inline int8_t zig_subw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { |
| 195 | + if ((lhs > 0) && (rhs < 0)) { |
| 196 | + int8_t thresh = lhs - max; |
| 197 | + if (rhs < thresh) { |
| 198 | + return min + (thresh - rhs - 1); |
| 199 | + } |
| 200 | + } else if ((lhs < 0) && (rhs > 0)) { |
| 201 | + int8_t thresh = lhs - min; |
| 202 | + if (rhs > thresh) { |
| 203 | + return max - (rhs - thresh - 1); |
| 204 | + } |
| 205 | + } |
| 206 | + return lhs - rhs; |
| 207 | +} |
| 208 | + |
| 209 | +static inline uint16_t zig_subw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { |
| 210 | + if (lhs < rhs) { |
| 211 | + return max - rhs - lhs + 1; |
| 212 | + } else { |
| 213 | + return lhs - rhs; |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +static inline int16_t zig_subw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { |
| 218 | + if ((lhs > 0) && (rhs < 0)) { |
| 219 | + int16_t thresh = lhs - max; |
| 220 | + if (rhs < thresh) { |
| 221 | + return min + (thresh - rhs - 1); |
| 222 | + } |
| 223 | + } else if ((lhs < 0) && (rhs > 0)) { |
| 224 | + int16_t thresh = lhs - min; |
| 225 | + if (rhs > thresh) { |
| 226 | + return max - (rhs - thresh - 1); |
| 227 | + } |
| 228 | + } |
| 229 | + return lhs - rhs; |
| 230 | +} |
| 231 | + |
| 232 | +static inline uint32_t zig_subw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { |
| 233 | + if (lhs < rhs) { |
| 234 | + return max - rhs - lhs + 1; |
| 235 | + } else { |
| 236 | + return lhs - rhs; |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +static inline int32_t zig_subw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { |
| 241 | + if ((lhs > 0) && (rhs < 0)) { |
| 242 | + int32_t thresh = lhs - max; |
| 243 | + if (rhs < thresh) { |
| 244 | + return min + (thresh - rhs - 1); |
| 245 | + } |
| 246 | + } else if ((lhs < 0) && (rhs > 0)) { |
| 247 | + int32_t thresh = lhs - min; |
| 248 | + if (rhs > thresh) { |
| 249 | + return max - (rhs - thresh - 1); |
| 250 | + } |
| 251 | + } |
| 252 | + return lhs - rhs; |
| 253 | +} |
| 254 | + |
| 255 | +static inline uint64_t zig_subw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { |
| 256 | + if (lhs < rhs) { |
| 257 | + return max - rhs - lhs + 1; |
| 258 | + } else { |
| 259 | + return lhs - rhs; |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +static inline int64_t zig_subw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { |
| 264 | + if ((lhs > 0) && (rhs < 0)) { |
| 265 | + int64_t thresh = lhs - max; |
| 266 | + if (rhs < thresh) { |
| 267 | + return min + (thresh - rhs - 1); |
| 268 | + } |
| 269 | + } else if ((lhs < 0) && (rhs > 0)) { |
| 270 | + int64_t thresh = lhs - min; |
| 271 | + if (rhs > thresh) { |
| 272 | + return max - (rhs - thresh - 1); |
| 273 | + } |
| 274 | + } |
| 275 | + return lhs - rhs; |
| 276 | +} |
| 277 | + |
| 278 | +static inline intptr_t zig_subw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { |
| 279 | + return (intptr_t)(((uintptr_t)lhs) - ((uintptr_t)rhs)); |
| 280 | +} |
| 281 | + |
| 282 | +static inline short zig_subw_short(short lhs, short rhs, short min, short max) { |
| 283 | + return (short)(((unsigned short)lhs) - ((unsigned short)rhs)); |
| 284 | +} |
| 285 | + |
| 286 | +static inline int zig_subw_int(int lhs, int rhs, int min, int max) { |
| 287 | + return (int)(((unsigned)lhs) - ((unsigned)rhs)); |
| 288 | +} |
| 289 | + |
| 290 | +static inline long zig_subw_long(long lhs, long rhs, long min, long max) { |
| 291 | + return (long)(((unsigned long)lhs) - ((unsigned long)rhs)); |
| 292 | +} |
| 293 | + |
| 294 | +static inline long long zig_subw_longlong(long long lhs, long long rhs, long long min, long long max) { |
| 295 | + return (long long)(((unsigned long long)lhs) - ((unsigned long long)rhs)); |
| 296 | +} |
| 297 | + |
0 commit comments