static int DemuxOnce (demux_t *demux, bool master) { demux_sys_t *sys = demux->p_sys; mtime_t pts = date_Get (&sys->date); lldiv_t d; unsigned h, m, s, f; d = lldiv (pts, CLOCK_FREQ); f = d.rem * sys->date.i_divider_num / sys->date.i_divider_den / CLOCK_FREQ; d = lldiv (d.quot, 60); s = d.rem; d = lldiv (d.quot, 60); m = d.rem; h = d.quot; char *str; int len = asprintf (&str, "%02u:%02u:%02u:%02u", h, m, s, f); if (len == -1) return -1; block_t *block = block_heap_Alloc (str, len + 1); if (unlikely(block == NULL)) return -1; block->i_buffer = len; assert(str[len] == '\0'); block->i_pts = block->i_dts = pts; block->i_length = date_Increment (&sys->date, 1) - pts; es_out_Send (demux->out, sys->es, block); if (master) es_out_SetPCR(demux->out, pts); return 1; }
/** * Waits for a condition variable up to a certain date. * This works like vlc_cond_wait(), except for the additional time-out. * * If the variable was initialized with vlc_cond_init(), the timeout has the * same arbitrary origin as mdate(). If the variable was initialized with * vlc_cond_init_daytime(), the timeout is expressed from the Unix epoch. * * @param p_condvar condition variable to wait on * @param p_mutex mutex which is unlocked while waiting, * then locked again when waking up. * @param deadline <b>absolute</b> timeout * * @return 0 if the condition was signaled, an error code in case of timeout. */ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex, mtime_t deadline) { #if defined(__APPLE__) && !defined(__powerpc__) && !defined( __ppc__ ) && !defined( __ppc64__ ) /* mdate() is the monotonic clock, timedwait origin is gettimeofday() which * isn't monotonic. Use imedwait_relative_np() instead */ mtime_t base = mdate(); deadline -= base; if (deadline < 0) deadline = 0; lldiv_t d = lldiv( deadline, CLOCK_FREQ ); struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) }; int val = pthread_cond_timedwait_relative_np(p_condvar, p_mutex, &ts); if (val != ETIMEDOUT) VLC_THREAD_ASSERT ("timed-waiting on condition"); return val; #else lldiv_t d = lldiv( deadline, CLOCK_FREQ ); struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) }; int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts); if (val != ETIMEDOUT) VLC_THREAD_ASSERT ("timed-waiting on condition"); return val; #endif }
void print_time( long long int ticks ) { lldiv_t sec_ticks; lldiv_t min_sec; sec_ticks = lldiv( ticks, 100 ); min_sec = lldiv( sec_ticks.quot, 60 ); printf( "It took %lld minutes and %lld seconds\n", min_sec.quot, min_sec.rem ); }
static void ResetTime( demux_t *p_demux, int64_t i_time ) { demux_sys_t *p_sys = p_demux->p_sys; vlc_tick_t t; if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 ) { t = VLC_TICK_INVALID; } else { #if CLOCK_FREQ == AV_TIME_BASE t = FROM_AV_TS(i_time); #else lldiv_t q = lldiv( i_time, AV_TIME_BASE ); t = vlc_tick_from_sec(q.quot) + FROM_AV_TS(q.rem); #endif if( t == VLC_TICK_INVALID ) t = VLC_TICK_0; } p_sys->i_pcr = t; for( unsigned i = 0; i < p_sys->i_tracks; i++ ) p_sys->tracks[i].i_pcr = VLC_TICK_INVALID; if( t != VLC_TICK_INVALID ) { es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, t ); UpdateSeekPoint( p_demux, t ); } }
static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, u64 offset, void *buf, long *len) { char cmd_buf[DFU_CMD_BUF_SIZE]; u32 blk_start, blk_count; /* * We must ensure that we work in lba_blk_size chunks, so ALIGN * this value. */ *len = ALIGN(*len, dfu->data.mmc.lba_blk_size); blk_start = dfu->data.mmc.lba_start + (u32)lldiv(offset, dfu->data.mmc.lba_blk_size); blk_count = *len / dfu->data.mmc.lba_blk_size; if (blk_start + blk_count > dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) { puts("Request would exceed designated area!\n"); return -EINVAL; } sprintf(cmd_buf, "mmc %s %p %x %x", op == DFU_OP_READ ? "read" : "write", buf, blk_start, blk_count); debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); return run_command(cmd_buf, 0); }
static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, u64 offset, void *buf, long *len) { struct mmc *mmc = find_mmc_device(dfu->dev_num); u32 blk_start, blk_count, n = 0; int ret, part_num_bkp = 0; /* * We must ensure that we work in lba_blk_size chunks, so ALIGN * this value. */ *len = ALIGN(*len, dfu->data.mmc.lba_blk_size); blk_start = dfu->data.mmc.lba_start + (u32)lldiv(offset, dfu->data.mmc.lba_blk_size); blk_count = *len / dfu->data.mmc.lba_blk_size; if (blk_start + blk_count > dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) { puts("Request would exceed designated area!\n"); return -EINVAL; } if (dfu->data.mmc.hw_partition >= 0) { part_num_bkp = mmc->part_num; ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition); if (ret) return ret; } debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__, op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num, blk_start, blk_count, buf); switch (op) { case DFU_OP_READ: n = mmc->block_dev.block_read(dfu->dev_num, blk_start, blk_count, buf); break; case DFU_OP_WRITE: n = mmc->block_dev.block_write(dfu->dev_num, blk_start, blk_count, buf); break; default: error("Operation not supported\n"); } if (n != blk_count) { error("MMC operation failed"); if (dfu->data.mmc.hw_partition >= 0) mmc_access_part(dfu, mmc, part_num_bkp); return -EIO; } if (dfu->data.mmc.hw_partition >= 0) { ret = mmc_access_part(dfu, mmc, part_num_bkp); if (ret) return ret; } return 0; }
/** * Arm or disarm an initialized timer. * This functions overrides any previous call to itself. * * @note A timer can fire later than requested due to system scheduling * limitations. An interval timer can fail to trigger sometimes, either because * the system is busy or suspended, or because a previous iteration of the * timer is still running. See also vlc_timer_getoverrun(). * * @param id initialized timer pointer * @param absolute the timer value origin is the same as mdate() if true, * the timer value is relative to now if false. * @param value zero to disarm the timer, otherwise the initial time to wait * before firing the timer. * @param interval zero to fire the timer just once, otherwise the timer * repetition interval. */ void vlc_timer_schedule (vlc_timer_t *id, bool absolute, mtime_t value, mtime_t interval) { #ifdef HAVE_POSIX_TIMER lldiv_t vad = lldiv (value, CLOCK_FREQ); lldiv_t itd = lldiv (interval, CLOCK_FREQ); struct itimerspec it = { .it_interval = { .tv_sec = itd.quot, .tv_nsec = (1000000000 / CLOCK_FREQ) * itd.rem, }, .it_value = { .tv_sec = vad.quot, .tv_nsec = (1000000000 / CLOCK_FREQ) * vad.rem, }, };
static struct timespec mtime_to_ts (mtime_t date) { lldiv_t d = lldiv (date, CLOCK_FREQ); struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) }; return ts; }
/*** Clock ***/ mtime_t mdate (void) { #if (_WIN32_WINNT >= 0x0601) ULONGLONG ts; if (unlikely(!QueryUnbiasedInterruptTime (&ts))) abort (); /* hundreds of nanoseconds */ static_assert ((10000000 % CLOCK_FREQ) == 0, "Broken frequencies ratio"); return ts / (10000000 / CLOCK_FREQ); #elif (_WIN32_WINNT >= 0x0600) ULONGLONG ts = GetTickCount64 (); /* milliseconds */ static_assert ((CLOCK_FREQ % 1000) == 0, "Broken frequencies ratio"); return ts * (CLOCK_FREQ / 1000); #else /* We don't need the real date, just the value of a high precision timer */ LARGE_INTEGER counter; if (!QueryPerformanceCounter (&counter)) abort (); /* Convert to from (1/freq) to microsecond resolution */ /* We need to split the division to avoid 63-bits overflow */ lldiv_t d = lldiv (counter.QuadPart, freq.QuadPart); return (d.quot * 1000000) + ((d.rem * 1000000) / freq.QuadPart); #endif }
LongNumber LongAdditionPrivate(LongNumber FirstNumber, LongNumber SecondNumber) { int NewSize; unsigned int CurrentBuffer = 0; if (FirstNumber.Length > SecondNumber.Length) NewSize = FirstNumber.Length; else NewSize = SecondNumber.Length; LongNumber Result = createNewLongNumber(NewSize + 1); int i; for (i = 0; i < Result.Length; ++i) { if (i < FirstNumber.Length) CurrentBuffer += FirstNumber.Digits[i]; if (i < SecondNumber.Length) CurrentBuffer += SecondNumber.Digits[i]; lldiv_t FinResult = lldiv(CurrentBuffer, 100000000); Result.Digits[i] = FinResult.rem; CurrentBuffer = FinResult.quot; } if (FirstNumber.Sign) Result.Sign = !Result.Sign; if (Result.Digits[Result.Length - 1] == 0) Result.Length--; return Result; }
LongNumber LongMultiplying(LongNumber FirstNumber, LongNumber SecondNumber) { unsigned long long CurrentBuffer = 0, Multiple; LongNumber Result = createNewLongNumber(FirstNumber.Length + SecondNumber.Length); if (FirstNumber.Sign != SecondNumber.Sign) Result.Sign = 1; int i, j; for (i = 0; i < FirstNumber.Length; ++i) { for (j = 0; j < SecondNumber.Length || CurrentBuffer; ++j) { Multiple = FirstNumber.Digits[i]; if (j < SecondNumber.Length) Multiple *= SecondNumber.Digits[j]; else Multiple = 0; CurrentBuffer += Multiple + Result.Digits[i + j]; lldiv_t FinResult = lldiv(CurrentBuffer, 100000000); Result.Digits[i + j] = FinResult.rem; CurrentBuffer = FinResult.quot; } } return removingLeadNulls(Result); }
static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, const char *part_name, void *buffer, unsigned int download_bytes) { lbaint_t blkcnt; lbaint_t blks; /* determine number of blocks to write */ blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); blkcnt = lldiv(blkcnt, info->blksz); if (blkcnt > info->size) { error("too large for partition: '%s'\n", part_name); fastboot_fail("too large for partition"); return; } puts("Flashing Raw Image\n"); blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); if (blks != blkcnt) { error("failed writing to device %d\n", dev_desc->devnum); fastboot_fail("failed writing to device"); return; } printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, part_name); fastboot_okay(""); }
void write_init(init_cond_t * init_cond) { char s[512]; int len; if (write_stream == NULL) return; memset(s, 0, 512); if (init_cond->param->type == P_TYPE_BOOL) sprintf(s, "%s=%d\n", init_cond->param->name, init_cond->init_val.bool_val); else if (init_cond->param->type == P_TYPE_INT) sprintf(s, "%s=%d\n", init_cond->param->name, init_cond->init_val.int_val); else if (init_cond->param->type == P_TYPE_DOUBLE) { lldiv_t div = lldiv( init_cond->init_val.double_val * 1000000,1000000 ); sprintf(s, "%s="I64Fd".%06u\n", init_cond->param->name, div.quot, (unsigned int) div.rem ); } else { printf("write_init: unknown parameter type!\n"); return; } len = strlen(s); if ((fwrite(s, 1, len, write_stream)) != len) printf("write_init: failed writing to file stream! Out of disk space?\n"); }
ulong get_timer(ulong base) { unsigned long long timer_diff; timer_diff = get_ticks() - gd->timer_reset_value; return lldiv(timer_diff, (gd->timer_rate_hz / CONFIG_SYS_HZ)) - base; }
long long sum_of_digits(long long n) { long long sum = 0; while(n) { lldiv_t d = lldiv(n,10ull); sum += d.rem; n = d.quot; } return sum; }
/* This function determines whether or not we need floating precision when calculating our slope. */ unsigned long precision_needed(unsigned long* dx, unsigned long* dy){ lldiv_t d = lldiv(*dy, *dx); // STONESOUP:TRIGGER_POINT if ( d.rem == 0 ){ return 0; }else{ return 1; } }
unsigned long long reverse2(unsigned long long a) { unsigned long long aux; unsigned long long rev = 0; while(a>0){ lldiv_t longdiv = lldiv(a, 10); rev = rev*10 + longdiv.rem; a = longdiv.quot; } return rev; }
static UINT64 GetQPC(void) { LARGE_INTEGER counter; if (!QueryPerformanceCounter(&counter)) abort(); lldiv_t d = lldiv(counter.QuadPart, freq.QuadPart); return (d.quot * 10000000) + ((d.rem * 10000000) / freq.QuadPart); }
static VALUE t_init_cnpj(int argc, VALUE *argv, VALUE self) { int radix = 0; int filial = 0; int verif = 0; VALUE valid = Qnil; if (argc == 2) { radix = FIX2INT(argv[0]); if( radix >= 100000000 || radix < 0 ) rb_raise(rb_eArgError, "radix should be greater than -1 or lesser than 10_000_000"); filial = NUM2INT(argv[1]); if( filial >= 10000 || filial < 1) rb_raise(rb_eArgError, "filial should be greater than 0 or lesser than 10_000"); verif = calculate_digit(radix,filial); valid = Qtrue; } else if (argc == 1) { lldiv_t v; long long cnpj = 0; if (rb_class_of(argv[0]) == rb_cString) cnpj = NUM2LL(rb_str_to_inum(argv[0], 10, 0)); else if (rb_class_of(argv[0]) == rb_cFixnum || rb_class_of(argv[0]) == rb_cBignum) cnpj = NUM2LL(argv[0]); v = lldiv(cnpj, (long long)100); verif = (int)v.rem; v = lldiv(v.quot, (long long)10000); filial = (int)v.rem; radix = (int)v.quot; valid = (verif == calculate_digit(radix,filial)) ? Qtrue : Qfalse; } rb_iv_set(self, "@radix", INT2FIX(radix)); rb_iv_set(self, "@filial", INT2FIX(filial)); rb_iv_set(self, "@digit", INT2FIX(verif)); rb_iv_set(self, "@valid", valid); return self; }
void brute_factor(unsigned long long int n) { //std::cout << n << "="; lldiv_t junk = lldiv(1, 1); unsigned long long int i = 2; while (true) { if (i > ceil(sqrt(n))) { //std::cout << n << std::endl; break; } junk = lldiv(n, i); if (junk.rem == 0) { //std::cout << n << "/" << i << "=" << junk.quot << " r" << junk.rem << " (" << i << ")" << std::endl; n = junk.quot; //std::cout << i << "*"; i = 1; } i++; } }
/** * reduce the exponent to its smallest possible value, * without losing any precision * * \param mantissa a pointer to the 64 bits mantissa to be reduced * \param exponent a pointer to the exponent to be reduced **/ void gsb_real_raw_minimize_exponent ( gint64 *mantissa, gint *exponent ) { while ( *exponent > 0 ) { lldiv_t d = lldiv ( *mantissa, 10 ); if ( d.rem != 0 ) return; *mantissa = d.quot; --*exponent; } }
void __udelay(unsigned long usec) { unsigned long long endtime; endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz, 1000000UL); endtime += get_ticks(); while (get_ticks() < endtime) ; }
/** * Return the real in a formatted string with an optional currency * symbol, according to the given locale regarding decimal separator, * thousands separator and positive or negative sign. * * \param number Number to format. * \param locale the locale obtained with localeconv(), or built manually * \param currency_symbol the currency symbol * * \return A newly allocated string of the number (this * function will never return NULL) */ gchar *gsb_real_raw_format_string (gsb_real number, struct lconv *locale, const gchar *currency_symbol ) { gchar buffer[G_ASCII_DTOSTR_BUF_SIZE]; gchar format[40]; gchar *result = NULL, *temp = NULL; const gchar *cs_start; const gchar *cs_start_space; const gchar *sign; const gchar *mon_decimal_point; const gchar *cs_end_space; const gchar *cs_end; gint nbre_char; lldiv_t units; /*printf ("currency_symbol = %s\n", currency_symbol);*/ cs_start = (currency_symbol && locale->p_cs_precedes) ? currency_symbol : ""; cs_start_space = (currency_symbol && locale->p_cs_precedes && locale->p_sep_by_space) ? " " : ""; sign = (number.mantissa < 0) ? locale->negative_sign : locale->positive_sign; mon_decimal_point = locale->mon_decimal_point && *locale->mon_decimal_point ? locale->mon_decimal_point : ""; cs_end_space = (currency_symbol && !locale->p_cs_precedes && locale->p_sep_by_space) ? " " : ""; cs_end = (currency_symbol && !locale->p_cs_precedes) ? currency_symbol : ""; units = lldiv ( llabs (number.mantissa), gsb_real_power_10[number.exponent] ); nbre_char = g_sprintf ( buffer, "%.0f", (gdouble) units.quot ); temp = g_strndup ( buffer, nbre_char ); if ( units.quot >= 1000 ) { temp = gsb_real_add_thousands_sep ( temp, locale->mon_thousands_sep ); } g_snprintf ( format, sizeof ( format ), "%s%d%s", "%s%s%s%s%s%0", number.exponent, "lld%s%s" ); result = g_strdup_printf ( format, cs_start, cs_start_space, sign, temp, mon_decimal_point, units.rem, cs_end_space, cs_end ); g_free ( temp ); return result; }
bool isPrimeByFactoring(unsigned long long int n) { lldiv_t junk = lldiv(1, 1); unsigned long long int i = 2; while (true) { if (i > ceil(sqrt(n))) { std::cout << n << " is prime" << std::endl; return true; } junk = lldiv(n, i); if (junk.rem == 0) { //std::cout << n << " is not prime" << std::endl; return false; //std::cout << n << "/" << i << "=" << junk.quot << " r" << junk.rem << " (" << i << ")" << std::endl; //n = junk.quot; //std::cout << i << "*"; //i = 1; } i++; } }
void StandardClock::sleepNS(MWTime time){ #ifdef USE_MACH_MOJO uint64_t now = mach_absolute_time(); //mach_wait_until(now + (uint64_t)((long double)time / (long double)cv)); mach_wait_until(now + (uint64_t)time * tTBI.denom / tTBI.numer); #else long seconds = 0; long nano = 0; if((time - (MWTime)1000000000)){ lldiv_t div = lldiv(time, (MWTime)1000000000); seconds = (long)(div.quot); nano = (long)(div.rem); } else { // mprintf("not bigger %lld", time - (MWTime)1000000000); nano = (long)time; } // mprintf("nanosleeping for %d seconds, %d ns", seconds, nano); struct timespec time_to_sleep; time_to_sleep.tv_sec = seconds; time_to_sleep.tv_nsec = nano; // check every 300 usec int result = nanosleep(&time_to_sleep, NULL); if(result < 0){ char *resultstring = "Unknown error"; switch(errno){ case EINTR: resultstring = "Interrumpted by signal"; break; case EINVAL: resultstring = "Invalid time value"; break; case ENOSYS: resultstring = "Not supported"; break; case EFAULT: resultstring = "Invalid address"; break; } mprintf("Clock sleep error %d: %s (%d seconds, %d nano)", errno, resultstring, (long)seconds, (long)nano); } #endif }
static mtime_t mdate_perf (void) { /* We don't need the real date, just the value of a high precision timer */ LARGE_INTEGER counter; if (!QueryPerformanceCounter (&counter)) abort (); /* Convert to from (1/freq) to microsecond resolution */ /* We need to split the division to avoid 63-bits overflow */ lldiv_t d = lldiv (counter.QuadPart, clk.perf.freq.QuadPart); return (d.quot * 1000000) + ((d.rem * 1000000) / clk.perf.freq.QuadPart); }
//----------------------------------------------------------------------------- std::set<uint64_t> ProperDivisors::divisors_set() const { // NOLINT(build/include_what_you_use) std::set<uint64_t> divs = {1}; // NOLINT(build/include_what_you_use) uint64_t ubound = ceil(sqrt(value)); for (uint64_t x = 2; x <= ubound; ++x) { // get quotient and remainder in one step - actually optimized? std::lldiv_t result = lldiv(value, x); if (result.rem == 0) { divs.insert(x); divs.insert(result.quot); } } return divs; }
string int2str(unsigned int val) { char buffer[64] = {0}; int i=62; lldiv_t d; d.quot = val; do{ d = lldiv(d.quot,10); buffer[i] = _int2str_lookup[d.rem]; }while(--i && d.quot); return string((char*)(buffer+i+1)); }
/*** Clock ***/ mtime_t mdate (void) { /* We don't need the real date, just the value of a high precision timer */ QWORD counter; ULONG freq; if (DosTmrQueryTime(&counter) || DosTmrQueryFreq(&freq)) abort(); /* Convert to from (1/freq) to microsecond resolution */ /* We need to split the division to avoid 63-bits overflow */ lldiv_t d = lldiv (Q2LL(counter), freq); return (d.quot * 1000000) + ((d.rem * 1000000) / freq); }
int main(int argc, char *argv[]) { /* division type */ { int quot = -7 / 4; int rem = -7 % 4; div_t r = div(-7, 4); if (quot != r.quot || rem != r.rem) printf("#error \"Weird. div(3) has different semantics than / and %%.\"\n"); { lldiv_t llr = lldiv(-7LL, 4LL); if (llr.quot != r.quot || llr.rem != r.rem) printf("#error \"Weird. div(3) has different semantics than lldiv(3).\"\n"); } if (quot == -2 && rem == 1) printf("#define MU_DIVISION_FLOORS\n"); else if (quot == -1 && rem == -3) printf("#define MU_DIVISION_IS_SYMMETRIC\n"); else printf("#error \"Wow. Division is broken.\"\n"); } if (sizeof(intptr_t) == 4) printf("#define MU_ADDR_32\n"); else if (sizeof(intptr_t) == 8) printf("#define MU_ADDR_64\n"); else printf("#error. What kind of weird machine is this, anyway?\n"); { int mem; uint8_t *pb = (uint8_t *)&mem; pb[0] = 0x11; pb[1] = 0x22; pb[2] = 0x33; pb[3] = 0x44; if (mem == 0x11223344) printf("#define MU_BIG_ENDIAN\n"); if (mem == 0x44332211) printf("#define MU_LITTLE_ENDIAN\n"); } printf("#define MU_JMPBUF_CELLS %d\n", (int)(sizeof(jmp_buf) / sizeof(intptr_t))); return 0; }