1 /*- 2 * Copyright (c) 2018 Netflix, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Data types and APIs for fixed-point math based on the "Q" number format. 31 * 32 * Author: Lawrence Stewart <lstewart@netflix.com> 33 * 34 * The 3 LSBs of all base data types are reserved for embedded control data: 35 * bits 1-2 specify the radix point shift index i.e. 00,01,10,11 == 1,2,3,4 36 * bit 3 specifies the radix point shift index multiplier as 2 (0) or 16 (1) 37 * 38 * This scheme can therefore represent Q numbers with [2,4,6,8,16,32,48,64] bits 39 * of precision after the binary radix point. The number of bits available for 40 * the integral component depends on the underlying storage type chosen. 41 */ 42 43 #ifndef _SYS_QMATH_H_ 44 #define _SYS_QMATH_H_ 45 46 #include <machine/_stdint.h> 47 48 typedef int8_t s8q_t; 49 typedef uint8_t u8q_t; 50 typedef int16_t s16q_t; 51 typedef uint16_t u16q_t; 52 typedef int32_t s32q_t; 53 typedef uint32_t u32q_t; 54 typedef int64_t s64q_t; 55 typedef uint64_t u64q_t; 56 /* typedef int128_t s128q_t; Not yet */ 57 /* typedef uint128_t u128q_t; Not yet */ 58 typedef s64q_t smaxq_t; 59 typedef u64q_t umaxq_t; 60 61 /* The underlying base type of 'q'. */ 62 #define Q_BT(q) __typeof(q) 63 64 /* Type-cast variable 'v' to the same underlying type as 'q'. */ 65 #define Q_TC(q, v) ((Q_BT(q))(v)) 66 67 /* Number of total bits associated with the data type underlying 'q'. */ 68 #define Q_NTBITS(q) ((uint32_t)(sizeof(q) << 3)) 69 70 /* Number of LSBs reserved for control data. */ 71 #define Q_NCBITS ((uint32_t)3) 72 73 /* Number of control-encoded bits reserved for fractional component data. */ 74 #define Q_NFCBITS(q) \ 75 ((uint32_t)(((Q_GCRAW(q) & 0x3) + 1) << ((Q_GCRAW(q) & 0x4) ? 4 : 1))) 76 77 /* Min/max number of bits that can be reserved for fractional component data. */ 78 #define Q_MINNFBITS(q) ((uint32_t)(2)) 79 #define Q_MAXNFBITS(q) ((uint32_t)(Q_NTBITS(q) - Q_SIGNED(q) - Q_NCBITS)) 80 81 /* 82 * Number of bits actually reserved for fractional component data. This can be 83 * less than the value returned by Q_NFCBITS() as we treat any excess 84 * control-encoded number of bits for the underlying data type as meaning all 85 * available bits are reserved for fractional component data i.e. zero int bits. 86 */ 87 #define Q_NFBITS(q) \ 88 (Q_NFCBITS(q) > Q_MAXNFBITS(q) ? Q_MAXNFBITS(q) : Q_NFCBITS(q)) 89 90 /* Number of bits available for integer component data. */ 91 #define Q_NIBITS(q) ((uint32_t)(Q_NTBITS(q) - Q_RPSHFT(q) - Q_SIGNED(q))) 92 93 /* The radix point offset relative to the LSB. */ 94 #define Q_RPSHFT(q) (Q_NCBITS + Q_NFBITS(q)) 95 96 /* The sign bit offset relative to the LSB. */ 97 #define Q_SIGNSHFT(q) (Q_NTBITS(q) - 1) 98 99 /* Set the sign bit to 0 ('isneg' is F) or 1 ('isneg' is T). */ 100 #define Q_SSIGN(q, isneg) \ 101 ((q) = ((Q_SIGNED(q) && (isneg)) ? (q) | (1ULL << Q_SIGNSHFT(q)) : \ 102 (q) & ~(1ULL << Q_SIGNSHFT(q)))) 103 104 /* Manipulate the 'q' bits holding control/sign data. */ 105 #define Q_CRAWMASK(q) 0x7ULL 106 #define Q_SRAWMASK(q) (1ULL << Q_SIGNSHFT(q)) 107 #define Q_GCRAW(q) ((q) & Q_CRAWMASK(q)) 108 #define Q_GCVAL(q) Q_GCRAW(q) 109 #define Q_SCVAL(q, cv) ((q) = ((q) & ~Q_CRAWMASK(q)) | (cv)) 110 111 /* Manipulate the 'q' bits holding combined integer/fractional data. */ 112 #define Q_IFRAWMASK(q) \ 113 Q_TC(q, Q_SIGNED(q) ? ~(Q_SRAWMASK(q) | Q_CRAWMASK(q)) : ~Q_CRAWMASK(q)) 114 #define Q_IFMAXVAL(q) Q_TC(q, Q_IFRAWMASK(q) >> Q_NCBITS) 115 #define Q_IFMINVAL(q) Q_TC(q, Q_SIGNED(q) ? -Q_IFMAXVAL(q) : 0) 116 #define Q_IFVALIMASK(q) Q_TC(q, ~Q_IFVALFMASK(q)) 117 #define Q_IFVALFMASK(q) Q_TC(q, (1ULL << Q_NFBITS(q)) - 1) 118 #define Q_GIFRAW(q) Q_TC(q, (q) & Q_IFRAWMASK(q)) 119 #define Q_GIFABSVAL(q) Q_TC(q, Q_GIFRAW(q) >> Q_NCBITS) 120 #define Q_GIFVAL(q) Q_TC(q, Q_LTZ(q) ? -Q_GIFABSVAL(q) : Q_GIFABSVAL(q)) 121 #define Q_SIFVAL(q, ifv) \ 122 ((q) = ((q) & (~(Q_SRAWMASK(q) | Q_IFRAWMASK(q)))) | \ 123 (Q_TC(q, Q_ABS(ifv)) << Q_NCBITS) | \ 124 (Q_LTZ(ifv) ? 1ULL << Q_SIGNSHFT(q) : 0)) 125 #define Q_SIFVALS(q, iv, fv) \ 126 ((q) = ((q) & (~(Q_SRAWMASK(q) | Q_IFRAWMASK(q)))) | \ 127 (Q_TC(q, Q_ABS(iv)) << Q_RPSHFT(q)) | \ 128 (Q_TC(q, Q_ABS(fv)) << Q_NCBITS) | \ 129 (Q_LTZ(iv) || Q_LTZ(fv) ? 1ULL << Q_SIGNSHFT(q) : 0)) 130 131 /* Manipulate the 'q' bits holding integer data. */ 132 #define Q_IRAWMASK(q) Q_TC(q, Q_IFRAWMASK(q) & ~Q_FRAWMASK(q)) 133 #define Q_IMAXVAL(q) Q_TC(q, Q_IRAWMASK(q) >> Q_RPSHFT(q)) 134 #define Q_IMINVAL(q) Q_TC(q, Q_SIGNED(q) ? -Q_IMAXVAL(q) : 0) 135 #define Q_GIRAW(q) Q_TC(q, (q) & Q_IRAWMASK(q)) 136 #define Q_GIABSVAL(q) Q_TC(q, Q_GIRAW(q) >> Q_RPSHFT(q)) 137 #define Q_GIVAL(q) Q_TC(q, Q_LTZ(q) ? -Q_GIABSVAL(q) : Q_GIABSVAL(q)) 138 #define Q_SIVAL(q, iv) \ 139 ((q) = ((q) & ~(Q_SRAWMASK(q) | Q_IRAWMASK(q))) | \ 140 (Q_TC(q, Q_ABS(iv)) << Q_RPSHFT(q)) | \ 141 (Q_LTZ(iv) ? 1ULL << Q_SIGNSHFT(q) : 0)) 142 143 /* Manipulate the 'q' bits holding fractional data. */ 144 #define Q_FRAWMASK(q) Q_TC(q, ((1ULL << Q_NFBITS(q)) - 1) << Q_NCBITS) 145 #define Q_FMAXVAL(q) Q_TC(q, Q_FRAWMASK(q) >> Q_NCBITS) 146 #define Q_GFRAW(q) Q_TC(q, (q) & Q_FRAWMASK(q)) 147 #define Q_GFABSVAL(q) Q_TC(q, Q_GFRAW(q) >> Q_NCBITS) 148 #define Q_GFVAL(q) Q_TC(q, Q_LTZ(q) ? -Q_GFABSVAL(q) : Q_GFABSVAL(q)) 149 #define Q_SFVAL(q, fv) \ 150 ((q) = ((q) & ~(Q_SRAWMASK(q) | Q_FRAWMASK(q))) | \ 151 (Q_TC(q, Q_ABS(fv)) << Q_NCBITS) | \ 152 (Q_LTZ(fv) ? 1ULL << Q_SIGNSHFT(q) : 0)) 153 154 /* 155 * Calculate the number of bits required per 'base' digit, rounding up or down 156 * for non power-of-two bases. 157 */ 158 #define Q_BITSPERBASEDOWN(base) (flsll(base) - 1) 159 #define Q_BITSPERBASEUP(base) (flsll(base) - (__builtin_popcountll(base) == 1)) 160 #define Q_BITSPERBASE(base, rnd) Q_BITSPERBASE##rnd(base) 161 162 /* 163 * Upper bound number of digits required to render 'nbits' worth of integer 164 * component bits with numeric base 'base'. Overestimates for power-of-two 165 * bases. 166 */ 167 #define Q_NIBITS2NCHARS(nbits, base) \ 168 ({ \ 169 int _bitsperbase = Q_BITSPERBASE(base, DOWN); \ 170 (((nbits) + _bitsperbase - 1) / _bitsperbase); \ 171 }) 172 173 #define Q_NFBITS2NCHARS(nbits, base) (nbits) 174 175 /* 176 * Maximum number of chars required to render 'q' as a C-string of base 'base'. 177 * Includes space for sign, radix point and NUL-terminator. 178 */ 179 #define Q_MAXSTRLEN(q, base) \ 180 (2 + Q_NIBITS2NCHARS(Q_NIBITS(q), base) + \ 181 Q_NFBITS2NCHARS(Q_NFBITS(q), base) + Q_SIGNED(q)) 182 183 /* Yield the next char from integer bits. */ 184 #define Q_IBITS2CH(q, bits, base) \ 185 ({ \ 186 __typeof(bits) _tmp = (bits) / (base); \ 187 int _idx = (bits) - (_tmp * (base)); \ 188 (bits) = _tmp; \ 189 "0123456789abcdef"[_idx]; \ 190 }) 191 192 /* Yield the next char from fractional bits. */ 193 #define Q_FBITS2CH(q, bits, base) \ 194 ({ \ 195 int _carry = 0, _idx, _nfbits = Q_NFBITS(q), _shift = 0; \ 196 /* \ 197 * Normalise enough MSBs to yield the next digit, multiply by the \ 198 * base, and truncate residual fractional bits post multiplication. \ 199 */ \ 200 if (_nfbits > Q_BITSPERBASEUP(base)) { \ 201 /* Break multiplication into two steps to ensure no overflow. */\ 202 _shift = _nfbits >> 1; \ 203 _carry = (((bits) & ((1ULL << _shift) - 1)) * (base)) >> _shift;\ 204 } \ 205 _idx = ((((bits) >> _shift) * (base)) + _carry) >> (_nfbits - _shift);\ 206 (bits) *= (base); /* With _idx computed, no overflow concern. */ \ 207 (bits) &= (1ULL << _nfbits) - 1; /* Exclude residual int bits. */ \ 208 "0123456789abcdef"[_idx]; \ 209 }) 210 211 /* 212 * Render the C-string representation of 'q' into 's'. Returns a pointer to the 213 * final '\0' to allow for easy calculation of the rendered length and easy 214 * appending to the C-string. 215 */ 216 #define Q_TOSTR(q, prec, base, s, slen) \ 217 ({ \ 218 char *_r, *_s = s; \ 219 int _i; \ 220 if (Q_LTZ(q) && ((ptrdiff_t)(slen)) > 0) \ 221 *_s++ = '-'; \ 222 Q_BT(q) _part = Q_GIABSVAL(q); \ 223 _r = _s; \ 224 do { \ 225 /* Render integer chars in reverse order. */ \ 226 if ((_s - (s)) < ((ptrdiff_t)(slen))) \ 227 *_s++ = Q_IBITS2CH(q, _part, base); \ 228 else \ 229 _r = NULL; \ 230 } while (_part > 0 && _r != NULL); \ 231 if (!((_s - (s)) < ((ptrdiff_t)(slen)))) \ 232 _r = NULL; \ 233 _i = (_s - _r) >> 1; /* N digits requires int(N/2) swaps. */ \ 234 while (_i-- > 0 && _r != NULL) { \ 235 /* Work from middle out to reverse integer chars. */ \ 236 *_s = *(_r + _i); /* Stash LHS char temporarily. */ \ 237 *(_r + _i) = *(_s - _i - 1); /* Copy RHS char to LHS. */\ 238 *(_s - _i - 1) = *_s; /* Copy LHS char to RHS. */ \ 239 } \ 240 _i = (prec); \ 241 if (_i != 0 && _r != NULL) { \ 242 if ((_s - (s)) < ((ptrdiff_t)(slen))) \ 243 *_s++ = '.'; \ 244 else \ 245 _r = NULL; \ 246 _part = Q_GFABSVAL(q); \ 247 if (_i < 0 || _i > (int)Q_NFBITS(q)) \ 248 _i = Q_NFBITS(q); \ 249 while (_i-- > 0 && _r != NULL) { \ 250 /* Render fraction chars in correct order. */ \ 251 if ((_s - (s)) < ((ptrdiff_t)(slen))) \ 252 *_s++ = Q_FBITS2CH(q, _part, base); \ 253 else \ 254 _r = NULL; \ 255 } \ 256 } \ 257 if ((_s - (s)) < ((ptrdiff_t)(slen)) && _r != NULL) \ 258 *_s = '\0'; \ 259 else { \ 260 _r = NULL; \ 261 if (((ptrdiff_t)(slen)) > 0) \ 262 *(s) = '\0'; \ 263 } \ 264 /* Return a pointer to the '\0' or NULL on overflow. */ \ 265 (_r != NULL ? _s : _r); \ 266 }) 267 268 /* Left shift an integral value to align with the int bits of 'q'. */ 269 #define Q_SHL(q, iv) \ 270 (Q_LTZ(iv) ? -(Q_ABS(iv) << Q_NFBITS(q)) : \ 271 Q_TC(q, iv) << Q_NFBITS(q)) 272 273 /* Calculate the relative fractional precision between 'a' and 'b' in bits. */ 274 #define Q_RELPREC(a, b) ((int)Q_NFBITS(a) - (int)Q_NFBITS(b)) 275 276 /* 277 * Determine control bits for the desired 'rpshft' radix point shift. Rounds up 278 * to the nearest valid shift supported by the encoding scheme. 279 */ 280 #define Q_CTRLINI(rpshft) \ 281 (((rpshft) <= 8) ? (((rpshft) - 1) >> 1) : (0x4 | (((rpshft) - 1) >> 4))) 282 283 /* 284 * Convert decimal fractional value 'dfv' to its binary-encoded representation 285 * with 'nfbits' of binary precision. 'dfv' must be passed as a preprocessor 286 * literal to preserve leading zeroes. The returned result can be used to set a 287 * Q number's fractional bits e.g. using Q_SFVAL(). 288 */ 289 #define Q_DFV2BFV(dfv, nfbits) \ 290 ({ \ 291 uint64_t _bfv = 0, _thresh = 5, _tmp = dfv; \ 292 int _i = sizeof(""#dfv) - 1; \ 293 /* \ 294 * Compute decimal threshold to determine which \ 295 * conversion rounds will yield a binary 1. \ 296 */ \ 297 while (--_i > 0) {_thresh *= 10;} \ 298 _i = (nfbits) - 1; \ 299 while (_i >= 0) { \ 300 if (_thresh <= _tmp) { \ 301 _bfv |= 1ULL << _i; \ 302 _tmp = _tmp - _thresh; \ 303 } \ 304 _i--; _tmp <<= 1; \ 305 } \ 306 _bfv; \ 307 }) 308 309 /* 310 * Initialise 'q' with raw integer value 'iv', decimal fractional value 'dfv', 311 * and radix point shift 'rpshft'. Must be done in two steps in case 'iv' 312 * depends on control bits being set e.g. when passing Q_INTMAX(q) as 'iv'. 313 */ 314 #define Q_INI(q, iv, dfv, rpshft) \ 315 ({ \ 316 (*(q)) = Q_CTRLINI(rpshft); \ 317 Q_SIFVALS(*(q), iv, Q_DFV2BFV(dfv, Q_NFBITS(*(q)))); \ 318 }) 319 320 /* Test if 'a' and 'b' fractional precision is the same (T) or not (F). */ 321 #define Q_PRECEQ(a, b) (Q_NFBITS(a) == Q_NFBITS(b)) 322 323 /* Test if 'n' is a signed type (T) or not (F). Works with any numeric type. */ 324 #define Q_SIGNED(n) (Q_TC(n, -1) < 0) 325 326 /* 327 * Test if 'n' is negative. Works with any numeric type that uses the MSB as the 328 * sign bit, and also works with Q numbers. 329 */ 330 #define Q_LTZ(n) (Q_SIGNED(n) && ((n) & Q_SRAWMASK(n))) 331 332 /* 333 * Return absolute value of 'n'. Works with any standard numeric type that uses 334 * the MSB as the sign bit, and is signed/unsigned type safe. 335 * Does not work with Q numbers; use Q_QABS() instead. 336 */ 337 #define Q_ABS(n) (Q_LTZ(n) ? -(n) : (n)) 338 339 /* 340 * Return an absolute value interpretation of 'q'. 341 */ 342 #define Q_QABS(q) (Q_SIGNED(q) ? (q) & ~Q_SRAWMASK(q) : (q)) 343 344 /* Convert 'q' to float or double representation. */ 345 #define Q_Q2F(q) ((float)Q_GIFVAL(q) / (float)(1ULL << Q_NFBITS(q))) 346 #define Q_Q2D(q) ((double)Q_GIFVAL(q) / (double)(1ULL << Q_NFBITS(q))) 347 348 /* Numerically compare 'a' and 'b' as whole numbers using provided operators. */ 349 #define Q_QCMPQ(a, b, intcmp, fraccmp) \ 350 ((Q_GIVAL(a) intcmp Q_GIVAL(b)) || \ 351 ((Q_GIVAL(a) == Q_GIVAL(b)) && (Q_GFVAL(a) fraccmp Q_GFVAL(b)))) 352 353 /* Test if 'a' is numerically less than 'b' (T) or not (F). */ 354 #define Q_QLTQ(a, b) Q_QCMPQ(a, b, <, <) 355 356 /* Test if 'a' is numerically less than or equal to 'b' (T) or not (F). */ 357 #define Q_QLEQ(a, b) Q_QCMPQ(a, b, <, <=) 358 359 /* Test if 'a' is numerically greater than 'b' (T) or not (F). */ 360 #define Q_QGTQ(a, b) Q_QCMPQ(a, b, >, >) 361 362 /* Test if 'a' is numerically greater than or equal to 'b' (T) or not (F). */ 363 #define Q_QGEQ(a, b) Q_QCMPQ(a, b, >, >=) 364 365 /* Test if 'a' is numerically equal to 'b' (T) or not (F). */ 366 #define Q_QEQ(a, b) Q_QCMPQ(a, b, ==, ==) 367 368 /* Test if 'a' is numerically not equal to 'b' (T) or not (F). */ 369 #define Q_QNEQ(a, b) Q_QCMPQ(a, b, !=, !=) 370 371 /* Returns the numerically larger of 'a' and 'b'. */ 372 #define Q_QMAXQ(a, b) (Q_GT(a, b) ? (a) : (b)) 373 374 /* Returns the numerically smaller of 'a' and 'b'. */ 375 #define Q_QMINQ(a, b) (Q_LT(a, b) ? (a) : (b)) 376 377 /* 378 * Test if 'a' can be represented by 'b' with full accuracy (T) or not (F). 379 * The type casting has to be done to a's type so that any truncation caused by 380 * the casts will not affect the logic. 381 */ 382 #define Q_QCANREPQ(a, b) \ 383 ((((Q_LTZ(a) && Q_SIGNED(b)) || !Q_LTZ(a)) && \ 384 Q_GIABSVAL(a) <= Q_TC(a, Q_IMAXVAL(b)) && \ 385 Q_GFABSVAL(a) <= Q_TC(a, Q_FMAXVAL(b))) ? \ 386 0 : EOVERFLOW) 387 388 /* Test if raw integer value 'i' can be represented by 'q' (T) or not (F). */ 389 #define Q_QCANREPI(q, i) \ 390 ((((Q_LTZ(i) && Q_SIGNED(q)) || !Q_LTZ(i)) && \ 391 Q_ABS(i) <= Q_TC(i, Q_IMAXVAL(q))) ? 0 : EOVERFLOW) 392 393 /* 394 * Returns a Q variable debug format string with appropriate modifiers and 395 * padding relevant to the underlying Q data type. 396 */ 397 #define Q_DEBUGFMT_(prefmt, postfmt, mod, hexpad) \ 398 prefmt \ 399 /* Var name + address. */ \ 400 "\"%s\"@%p" \ 401 /* Data type. */ \ 402 "\n\ttype=%c%dq_t, " \ 403 /* Qm.n notation; 'm' = # int bits, 'n' = # frac bits. */ \ 404 "Qm.n=Q%d.%d, " \ 405 /* Radix point shift relative to the underlying data type's LSB. */ \ 406 "rpshft=%d, " \ 407 /* Min/max integer values which can be represented. */ \ 408 "imin=0x%0" #mod "x, " \ 409 "imax=0x%0" #mod "x" \ 410 /* Raw hex dump of all bits. */ \ 411 "\n\tqraw=0x%0" #hexpad #mod "x" \ 412 /* Bit masks for int/frac/ctrl bits. */ \ 413 "\n\timask=0x%0" #hexpad #mod "x, " \ 414 "fmask=0x%0" #hexpad #mod "x, " \ 415 "cmask=0x%0" #hexpad #mod "x, " \ 416 "ifmask=0x%0" #hexpad #mod "x" \ 417 /* Hex dump of masked int bits; 'iraw' includes shift */ \ 418 "\n\tiraw=0x%0" #hexpad #mod "x, " \ 419 "iabsval=0x%" #mod "x, " \ 420 "ival=0x%" #mod "x" \ 421 /* Hex dump of masked frac bits; 'fraw' includes shift */ \ 422 "\n\tfraw=0x%0" #hexpad #mod "x, " \ 423 "fabsval=0x%" #mod "x, " \ 424 "fval=0x%" #mod "x" \ 425 "%s" \ 426 postfmt 427 428 #define Q_DEBUGFMT(q, prefmt, postfmt) \ 429 sizeof(q) == 8 ? Q_DEBUGFMT_(prefmt, postfmt, j, 16) : \ 430 sizeof(q) == 4 ? Q_DEBUGFMT_(prefmt, postfmt, , 8) : \ 431 sizeof(q) == 2 ? Q_DEBUGFMT_(prefmt, postfmt, h, 4) : \ 432 sizeof(q) == 1 ? Q_DEBUGFMT_(prefmt, postfmt, hh, 2) : \ 433 prefmt "\"%s\"@%p: invalid" postfmt \ 434 435 /* 436 * Returns a format string and data suitable for printf-like rendering 437 * e.g. Print to console with a trailing newline: printf(Q_DEBUG(q, "", "\n")); 438 */ 439 #define Q_DEBUG(q, prefmt, postfmt, incfmt) \ 440 Q_DEBUGFMT(q, prefmt, postfmt) \ 441 , #q \ 442 , &(q) \ 443 , Q_SIGNED(q) ? 's' : 'u' \ 444 , Q_NTBITS(q) \ 445 , Q_NIBITS(q) \ 446 , Q_NFBITS(q) \ 447 , Q_RPSHFT(q) \ 448 , Q_IMINVAL(q) \ 449 , Q_IMAXVAL(q) \ 450 , (q) \ 451 , Q_IRAWMASK(q) \ 452 , Q_FRAWMASK(q) \ 453 , Q_TC(q, Q_CRAWMASK(q)) \ 454 , Q_IFRAWMASK(q) \ 455 , Q_GIRAW(q) \ 456 , Q_GIABSVAL(q) \ 457 , Q_GIVAL(q) \ 458 , Q_GFRAW(q) \ 459 , Q_GFABSVAL(q) \ 460 , Q_GFVAL(q) \ 461 , (incfmt) ? Q_DEBUGFMT(q, "\nfmt:", "") : "" \ 462 463 /* 464 * If precision differs, attempt to normalise to the greater precision that 465 * preserves the integer component of both 'a' and 'b'. 466 */ 467 #define Q_NORMPREC(a, b) \ 468 ({ \ 469 int _perr = 0, _relprec = Q_RELPREC(*(a), b); \ 470 if (_relprec != 0) \ 471 _perr = ERANGE; /* XXXLAS: Do precision normalisation! */\ 472 _perr; \ 473 }) 474 475 /* Clone r's control bits and int/frac value into 'l'. */ 476 #define Q_QCLONEQ(l, r) \ 477 ({ \ 478 Q_BT(*(l)) _l = Q_GCVAL(r); \ 479 int _err = Q_QCANREPQ(r, _l); \ 480 if (!_err) { \ 481 *(l) = _l; \ 482 Q_SIFVAL(*(l), Q_GIFVAL(r)); \ 483 } \ 484 _err; \ 485 }) 486 487 /* Copy r's int/frac vals into 'l', retaining 'l's precision and signedness. */ 488 #define Q_QCPYVALQ(l, r) \ 489 ({ \ 490 int _err = Q_QCANREPQ(r, *(l)); \ 491 if (!_err) \ 492 Q_SIFVALS(*(l), Q_GIVAL(r), Q_GFVAL(r)); \ 493 _err; \ 494 }) 495 496 #define Q_QADDSUBQ(a, b, eop) \ 497 ({ \ 498 int _aserr; \ 499 if ((_aserr = Q_NORMPREC(a, b))) while(0); /* NOP */ \ 500 else if ((eop) == '+') { \ 501 if (Q_IFMAXVAL(*(a)) - Q_GIFABSVAL(b) < Q_GIFVAL(*(a))) \ 502 _aserr = EOVERFLOW; /* [+/-a + +b] > max(a) */ \ 503 else \ 504 Q_SIFVAL(*(a), Q_GIFVAL(*(a)) + Q_TC(*(a), \ 505 Q_GIFABSVAL(b))); \ 506 } else { /* eop == '-' */ \ 507 if (Q_IFMINVAL(*(a)) + Q_GIFABSVAL(b) > Q_GIFVAL(*(a))) \ 508 _aserr = EOVERFLOW; /* [+/-a - +b] < min(a) */ \ 509 else \ 510 Q_SIFVAL(*(a), Q_GIFVAL(*(a)) - Q_TC(*(a), \ 511 Q_GIFABSVAL(b))); \ 512 } \ 513 _aserr; \ 514 }) 515 #define Q_QADDQ(a, b) Q_QADDSUBQ(a, b, (Q_LTZ(b) ? '-' : '+')) 516 #define Q_QSUBQ(a, b) Q_QADDSUBQ(a, b, (Q_LTZ(b) ? '+' : '-')) 517 518 #define Q_QDIVQ(a, b) \ 519 ({ \ 520 int _err; \ 521 if ((_err = Q_NORMPREC(a, b))) while(0); /* NOP */ \ 522 else if (Q_GIFABSVAL(b) == 0 || (!Q_SIGNED(*(a)) && Q_LTZ(b))) \ 523 _err = EINVAL; /* Divide by zero or cannot represent. */\ 524 /* XXXLAS: Handle overflow. */ \ 525 else if (Q_GIFABSVAL(*(a)) != 0) { /* Result expected. */ \ 526 Q_SIFVAL(*(a), \ 527 ((Q_GIVAL(*(a)) << Q_NFBITS(*(a))) / Q_GIFVAL(b)) + \ 528 (Q_GFVAL(b) == 0 ? 0 : \ 529 ((Q_GFVAL(*(a)) << Q_NFBITS(*(a))) / Q_GFVAL(b)))); \ 530 } \ 531 _err; \ 532 }) 533 534 #define Q_QMULQ(a, b) \ 535 ({ \ 536 int _mulerr; \ 537 if ((_mulerr = Q_NORMPREC(a, b))) while(0); /* NOP */ \ 538 else if (!Q_SIGNED(*(a)) && Q_LTZ(b)) \ 539 _mulerr = EINVAL; \ 540 else if (Q_GIFABSVAL(b) != 0 && \ 541 Q_IFMAXVAL(*(a)) / Q_GIFABSVAL(b) < Q_GIFABSVAL(*(a))) \ 542 _mulerr = EOVERFLOW; \ 543 else \ 544 Q_SIFVAL(*(a), (Q_GIFVAL(*(a)) * Q_GIFVAL(b)) >> \ 545 Q_NFBITS(*(a))); \ 546 _mulerr; \ 547 }) 548 549 #define Q_QCPYVALI(q, i) \ 550 ({ \ 551 int _err = Q_QCANREPI(*(q), i); \ 552 if (!_err) \ 553 Q_SIFVAL(*(q), Q_SHL(*(q), i)); \ 554 _err; \ 555 }) 556 557 #define Q_QADDSUBI(q, i, eop) \ 558 ({ \ 559 int _aserr = 0; \ 560 if (Q_NTBITS(*(q)) < (uint32_t)flsll(Q_ABS(i))) \ 561 _aserr = EOVERFLOW; /* i cannot fit in q's type. */ \ 562 else if ((eop) == '+') { \ 563 if (Q_IMAXVAL(*(q)) - Q_TC(*(q), Q_ABS(i)) < \ 564 Q_GIVAL(*(q))) \ 565 _aserr = EOVERFLOW; /* [+/-q + +i] > max(q) */ \ 566 else \ 567 Q_SIFVAL(*(q), Q_GIFVAL(*(q)) + \ 568 Q_SHL(*(q), Q_ABS(i))); \ 569 } else { /* eop == '-' */ \ 570 if (Q_IMINVAL(*(q)) + Q_ABS(i) > Q_GIVAL(*(q))) \ 571 _aserr = EOVERFLOW; /* [+/-q - +i] < min(q) */ \ 572 else \ 573 Q_SIFVAL(*(q), Q_GIFVAL(*(q)) - \ 574 Q_SHL(*(q), Q_ABS(i))); \ 575 } \ 576 _aserr; \ 577 }) 578 #define Q_QADDI(q, i) Q_QADDSUBI(q, i, (Q_LTZ(i) ? '-' : '+')) 579 #define Q_QSUBI(q, i) Q_QADDSUBI(q, i, (Q_LTZ(i) ? '+' : '-')) 580 581 #define Q_QDIVI(q, i) \ 582 ({ \ 583 int _diverr = 0; \ 584 if ((i) == 0 || (!Q_SIGNED(*(q)) && Q_LTZ(i))) \ 585 _diverr = EINVAL; /* Divide by zero or cannot represent. */\ 586 else if (Q_GIFABSVAL(*(q)) != 0) { /* Result expected. */ \ 587 Q_SIFVAL(*(q), Q_GIFVAL(*(q)) / Q_TC(*(q), i)); \ 588 if (Q_GIFABSVAL(*(q)) == 0) \ 589 _diverr = ERANGE; /* q underflow. */ \ 590 } \ 591 _diverr; \ 592 }) 593 594 #define Q_QMULI(q, i) \ 595 ({ \ 596 int _mulerr = 0; \ 597 if (!Q_SIGNED(*(q)) && Q_LTZ(i)) \ 598 _mulerr = EINVAL; /* Cannot represent. */ \ 599 else if ((i) != 0 && Q_IFMAXVAL(*(q)) / Q_TC(*(q), Q_ABS(i)) < \ 600 Q_GIFABSVAL(*(q))) \ 601 _mulerr = EOVERFLOW; \ 602 else \ 603 Q_SIFVAL(*(q), Q_GIFVAL(*(q)) * Q_TC(*(q), i)); \ 604 _mulerr; \ 605 }) 606 607 #define Q_QFRACI(q, in, id) \ 608 ({ \ 609 uint64_t _tmp; \ 610 int _err = 0; \ 611 if ((id) == 0) \ 612 _err = EINVAL; /* Divide by zero. */ \ 613 else if ((in) == 0) \ 614 Q_SIFVAL(*(q), in); \ 615 else if ((_tmp = Q_ABS(in)) > (UINT64_MAX >> Q_RPSHFT(*(q)))) \ 616 _err = EOVERFLOW; /* _tmp overflow. */ \ 617 else { \ 618 _tmp = Q_SHL(*(q), _tmp) / Q_ABS(id); \ 619 if (Q_QCANREPI(*(q), _tmp & Q_IFVALIMASK(*(q)))) \ 620 _err = EOVERFLOW; /* q overflow. */ \ 621 else { \ 622 Q_SIFVAL(*(q), _tmp); \ 623 Q_SSIGN(*(q), (Q_LTZ(in) && !Q_LTZ(id)) || \ 624 (!Q_LTZ(in) && Q_LTZ(id))); \ 625 if (_tmp == 0) \ 626 _err = ERANGE; /* q underflow. */ \ 627 } \ 628 } \ 629 _err; \ 630 }) 631 632 #endif /* _SYS_QMATH_H_ */ 633