MCPcopy Create free account
hub / github.com/F-Stack/f-stack / encode_timeval

Function encode_timeval

freebsd/kern/kern_acct.c:476–520  ·  view source on GitHub ↗

* Convert the elements of a timeval into a 32-bit word holding * the bits of a IEEE-754 float. * The float value represents the timeval's value in microsecond units. */

Source from the content-addressed store, hash-verified

474 * The float value represents the timeval's value in microsecond units.
475 */
476static uint32_t
477encode_timeval(struct timeval tv)
478{
479 int log2_s;
480 int val, exp; /* Unnormalized value and exponent */
481 int norm_exp; /* Normalized exponent */
482 int shift;
483
484 /*
485 * First calculate value and exponent to about CALC_BITS precision.
486 * Note that the following conditionals have been ordered so that
487 * the most common cases appear first.
488 */
489 if (tv.tv_sec == 0) {
490 if (tv.tv_usec == 0)
491 return (0);
492 exp = 0;
493 val = tv.tv_usec;
494 } else {
495 /*
496 * Calculate the value to a precision of approximately
497 * CALC_BITS.
498 */
499 log2_s = fls(tv.tv_sec) - 1;
500 if (log2_s + LOG2_1M < CALC_BITS) {
501 exp = 0;
502 val = 1000000 * tv.tv_sec + tv.tv_usec;
503 } else {
504 exp = log2_s + LOG2_1M - CALC_BITS;
505 val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec +
506 tv.tv_usec) >> exp);
507 }
508 }
509 /* Now normalize and pack the value into an IEEE-754 float. */
510 norm_exp = fls(val) - 1;
511 shift = FLT_MANT_DIG - norm_exp - 1;
512#ifdef ACCT_DEBUG
513 printf("val=%d exp=%d shift=%d log2(val)=%d\n",
514 val, exp, shift, norm_exp);
515 printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
516 ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
517#endif
518 return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
519 ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
520}
521
522/*
523 * Convert a non-negative long value into the bit pattern of

Callers 1

acct_processFunction · 0.85

Calls 2

printfFunction · 0.70
flsFunction · 0.50

Tested by

no test coverage detected