/** * Determine the 'jerk' between 2 2D vectors and their speeds. The jerk can be * used to obtain an acceptable speed for changing directions between moves. * @param x1 x component of first vector * @param y1 y component of first vector * @param F1 feed rate of first move * @param x2 x component of second vector * @param y2 y component of second vector * @param F2 feed rate of second move */ int dda_jerk_size_2d_real(int32_t x1, int32_t y1, uint32_t F1, int32_t x2, int32_t y2, uint32_t F2) { const int maxlen = 10000; // Normalize vectors so their length will be fixed // Note: approx_distance is not precise enough and may result in violent direction changes //sersendf_P(PSTR("Input vectors: (%ld, %ld) and (%ld, %ld)\r\n"),x1,y1,x2,y2); int32_t len = int_sqrt(x1*x1+y1*y1); x1 = (x1 * maxlen) / len; y1 = (y1 * maxlen) / len; len = int_sqrt(x2*x2+y2*y2); x2 = (x2 * maxlen) / len; y2 = (y2 * maxlen) / len; //sersendf_P(PSTR("Normalized vectors: (%ld, %ld) and (%ld, %ld)\r\n"),x1,y1,x2,y2); // Now scale the normalized vectors by their speeds x1 *= F1; y1 *= F1; x2 *= F2; y2 *= F2; //sersendf_P(PSTR("Speed vectors: (%ld, %ld) and (%ld, %ld)\r\n"),x1,y1,x2,y2); // The difference between the vectors actually depicts the jerk x1 -= x2; y1 -= y2; //sersendf_P(PSTR("Jerk vector: (%ld, %ld)\r\n"),x1,y1); return approx_distance(x1,y1) / maxlen; }
static int badness(struct task_struct *p) { int points, cpu_time, run_time; if (!p->mm) return 0; if (p->flags & PF_MEMDIE) return 0; /* * The memory size of the process is the basis for the badness. */ points = p->mm->total_vm; /* * CPU time is in seconds and run time is in minutes. There is no * particular reason for this other than that it turned out to work * very well in practice. This is not safe against jiffie wraps * but we don't care _that_ much... */ cpu_time = (p->times.tms_utime + p->times.tms_stime) >> (SHIFT_HZ + 3); run_time = (jiffies - p->start_time) >> (SHIFT_HZ + 10); points /= int_sqrt(cpu_time); points /= int_sqrt(int_sqrt(run_time)); /* * Niced processes are most likely less important, so double * their badness points. */ if (p->nice > 0) points *= 2; /* * Superuser processes are usually more important, so we make it * less likely that we kill those. */ if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) || p->uid == 0 || p->euid == 0) points /= 4; /* * We don't want to kill a process with direct hardware access. * Not only could that mess up the hardware, but usually users * tend to only have this flag set on applications they think * of as important. */ if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO)) points /= 4; #ifdef DEBUG printk(KERN_DEBUG "OOMkill: task %d (%s) got %d points\n", p->pid, p->comm, points); #endif return points; }
// We also need the inverse: given a ramp length, determine the expected speed // Note: the calculation is scaled by a factor 10000 to obtain an answer with a smaller // rounding error. // Warning: this is an expensive function as it requires a square root to get the result. // uint32_t dda_steps_to_velocity(uint32_t steps) { // v(t) = a*t, with v in mm/s and a = acceleration in mm/s² // s(t) = 1/2*a*t² with s (displacement) in mm // Rewriting yields v(s) = sqrt(2*a*s) // Rewriting into steps and seperation in constant part and dynamic part: // F_steps = sqrt((2000*a)/STEPS_PER_M_X) * 60 * sqrt(steps) static uint32_t part = 0; if(part == 0) part = int_sqrt((uint32_t)((2000.0f*ACCELERATION*3600.0f*10000.0f)/(float)STEPS_PER_M_X)); uint32_t res = int_sqrt((steps) * 10000) * part; return res / 10000; }
static int default_congestion_kb(void) { int congestion_kb; /* * Copied from NFS * * congestion size, scale with available memory. * * 64MB: 8192k * 128MB: 11585k * 256MB: 16384k * 512MB: 23170k * 1GB: 32768k * 2GB: 46340k * 4GB: 65536k * 8GB: 92681k * 16GB: 131072k * * This allows larger machines to have larger/more transfers. * Limit the default to 256M */ congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); if (congestion_kb > 256*1024) congestion_kb = 256*1024; return congestion_kb; }
static void report_objects(struct f12_data *f12) { int i; int object_count = 0; if (f12->suppress) return; for (i = 0; i < f12->max_objects; i++) { struct rmi_f12_object_data *object = (struct rmi_f12_object_data *) &f12->object_buf[i * 8]; //u16 w_max = object->wx > object->wy ? object->wy : object->wx; u16 w_max = int_sqrt(object->wx * object->wx + object->wy * object->wy); if (f12->suppress_highw > 0 && f12->suppress_highw <= w_max) { dev_info(f12->rmi_input->input_dev->dev.parent, "Suppressing finger (%d) because of high W (%d)\n",i, w_max); return; } } for (i = 0; i < f12->max_objects; i++) { struct rmi_f12_object_data *object = (struct rmi_f12_object_data *) &f12->object_buf[i * 8]; if (object->type) object_count++; report_one_object(f12, object, i); } // TODO update lines for newer kernel rmi_input_report_key(f12->rmi_input, BTN_TOUCH, object_count); input_sync(f12->rmi_input->input_dev); }
/*! Adjusts the richt text document to a reasonable size. \sa setWidth() */ void QSimpleRichText::adjustSize() { QFontMetrics fm( d->font ); int mw = fm.width( 'x' ) * 80; int w = mw; d->doc->doLayout( 0,w ); if ( d->doc->flow()->widthUsed != 0 ) { w = int_sqrt( (5*d->doc->height) / (3*d->doc->flow()->widthUsed ) ); d->doc->doLayout( 0, QMIN( w, mw) ); if ( w*3 < 5*d->doc->flow()->height ) { w = int_sqrt(6*d->doc->flow()->height/3*d->doc->flow()->widthUsed); d->doc->doLayout( 0,QMIN(w, mw ) ); } } }
/* * After a task dirtied this many pages, balance_dirty_pages_ratelimited_nr() * will look to see if it needs to start dirty throttling. * * If ratelimit_pages is too low then big NUMA machines will call the expensive * global_page_state() too often. So scale it adaptively to the safety margin * (the number of pages we may dirty without exceeding the dirty limits). */ static unsigned long ratelimit_pages(struct backing_dev_info *bdi) { unsigned long background_thresh; unsigned long dirty_thresh; unsigned long dirty_pages; global_dirty_limits(&background_thresh, &dirty_thresh); dirty_pages = global_page_state(NR_FILE_DIRTY) + global_page_state(NR_WRITEBACK) + global_page_state(NR_UNSTABLE_NFS); if (dirty_pages <= (dirty_thresh + background_thresh) / 2) goto out; dirty_thresh = bdi_dirty_limit(bdi, dirty_thresh, dirty_pages); dirty_pages = bdi_stat(bdi, BDI_RECLAIMABLE) + bdi_stat(bdi, BDI_WRITEBACK); if (dirty_pages < dirty_thresh) goto out; return 1; out: return 1 + int_sqrt(dirty_thresh - dirty_pages); }
unsigned long rthal_timer_calibrate(void) { unsigned long long start, end, sum = 0, sum_sq = 0; volatile unsigned const_delay = rthal_clockfreq_arg / HZ; unsigned long result, flags, tsc_lat; unsigned int delay = const_delay; long long diff; int i, j; flags = rthal_critical_enter(NULL); /* * Hw interrupts off, other CPUs quiesced, no migration * possible. We can now fiddle with the timer chip (per-cpu * local or global, rthal_timer_program_shot() will handle * this transparently via the I-pipe). */ steal_timer(1); force_oneshot_hw_mode(); rthal_read_tsc(start); barrier(); rthal_read_tsc(end); tsc_lat = end - start; barrier(); for (i = 0; i < RTHAL_CALIBRATE_LOOPS; i++) { flush_cache_all(); for (j = 0; j < RTHAL_CALIBRATE_LOOPS; j++) { rthal_read_tsc(start); barrier(); #if IPIPE_CORE_APIREV < 2 rthal_timer_program_shot( rthal_nodiv_imuldiv_ceil(delay, rthal_tsc_to_timer)); #else rthal_timer_program_shot(delay); #endif barrier(); rthal_read_tsc(end); diff = end - start - tsc_lat; if (diff > 0) { sum += diff; sum_sq += diff * diff; } } } restore_normal_hw_mode(); rthal_critical_exit(flags); /* Use average + standard deviation as timer programming latency. */ do_div(sum, RTHAL_CALIBRATE_LOOPS * RTHAL_CALIBRATE_LOOPS); do_div(sum_sq, RTHAL_CALIBRATE_LOOPS * RTHAL_CALIBRATE_LOOPS); result = sum + int_sqrt(sum_sq - sum * sum) + 1; return result; }
/* * Try detecting repeating patterns by keeping track of the last 8 * intervals, and checking if the standard deviation of that set * of points is below a threshold. If it is... then use the * average of these 8 points as the estimated value. */ static u32 get_typical_interval(struct menu_device *data) { int i = 0, divisor = 0; uint64_t max = 0, avg = 0, stddev = 0; int64_t thresh = LLONG_MAX; /* Discard outliers above this value. */ unsigned int ret = 0; again: /* first calculate average and standard deviation of the past */ max = avg = divisor = stddev = 0; for (i = 0; i < INTERVALS; i++) { int64_t value = data->intervals[i]; if (value <= thresh) { avg += value; divisor++; if (value > max) max = value; } } do_div(avg, divisor); for (i = 0; i < INTERVALS; i++) { int64_t value = data->intervals[i]; if (value <= thresh) { int64_t diff = value - avg; stddev += diff * diff; } } do_div(stddev, divisor); stddev = int_sqrt(stddev); /* * If we have outliers to the upside in our distribution, discard * those by setting the threshold to exclude these outliers, then * calculate the average and standard deviation again. Once we get * down to the bottom 3/4 of our samples, stop excluding samples. * * This can deal with workloads that have long pauses interspersed * with sporadic activity with a bunch of short pauses. * * The typical interval is obtained when standard deviation is small * or standard deviation is small compared to the average interval. */ if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3)) || stddev <= 20) { data->predicted_us = avg; ret = 1; return ret; } else if ((divisor * 4) > INTERVALS * 3) { /* Exclude the max interval */ thresh = max - 1; goto again; } return ret; }
void QSimpleRichText::adjustSize() { QFontMetrics fm( d->font ); int mw = fm.width( 'x' ) * 80; int w = mw; d->doc->doLayout( 0,w ); if ( d->doc->widthUsed() != 0 ) { w = int_sqrt( (5*d->doc->height() ) / (3*d->doc->widthUsed() ) ); d->doc->doLayout( 0, QMIN( w, mw) ); if ( w*3 < 5*d->doc->height() ) { w = int_sqrt(6*d->doc->height()/3*d->doc->widthUsed() ); d->doc->doLayout( 0,QMIN(w, mw ) ); } } d->cachedWidth = d->doc->width(); d->cachedWidthWithPainter = FALSE; d->widthUsed = -1; }
int main(int argc,char* argv[]){ int i, j; uint64_t sse=0; uint64_t dev; FILE *f[2]; uint8_t buf[2][SIZE]; uint64_t psnr; int len= argc<4 ? 1 : atoi(argv[3]); int64_t max= (1<<(8*len))-1; int shift= argc<5 ? 0 : atoi(argv[4]); int skip_bytes = argc<6 ? 0 : atoi(argv[5]); if(argc<3){ printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n"); printf("for wav files use the following:\n"); printf("./tiny_psnr file1.wav file2.wav 2 0 44 to skip the header.\n"); return -1; } f[0]= fopen(argv[1], "rb"); f[1]= fopen(argv[2], "rb"); fseek(f[shift<0], shift < 0 ? -shift : shift, SEEK_SET); fseek(f[0],skip_bytes,SEEK_CUR); fseek(f[1],skip_bytes,SEEK_CUR); for(i=0;;){ if( fread(buf[0], SIZE, 1, f[0]) != 1) break; if( fread(buf[1], SIZE, 1, f[1]) != 1) break; for(j=0; j<SIZE; i++,j++){ int64_t a= buf[0][j]; int64_t b= buf[1][j]; if(len==2){ a= (int16_t)(a | (buf[0][++j]<<8)); b= (int16_t)(b | (buf[1][ j]<<8)); } sse += (a-b) * (a-b); } } if(!i) i=1; dev= int_sqrt( ((sse/i)*F*F) + (((sse%i)*F*F) + i/2)/i ); if(sse) psnr= ((2*log16(max<<16) + log16(i) - log16(sse))*284619LL*F + (1<<31)) / (1LL<<32); else psnr= 100*F-1; //floating point free infinity :) printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n", (int)(dev/F), (int)(dev%F), (int)(psnr/F), (int)(psnr%F), i*len); return 0; }
//============================================================================== void PoissonData::calculateDistribution() { // //Calculate which elements this processor owns. The element domain is a //square, and we can assume that sqrt(numProcs_) divides evenly into //L_. We're working with a (logically) 2D processor arrangement. //Furthermore, the logical processor layout is such that processor 0 is at //the bottom left corner of a 2D grid, and a side of the grid is of length //sqrt(numProcs_). The element domain is numbered such that element 1 is at //the bottom left corner of the square, and element numbers increase from //left to right. i.e., element 1 is in position (1,1), element L is in //position (1,L), element L+1 is in position (2,1). // //Use 1-based numbering for the elements and the x- and y- coordinates in //the element grid, but use 0-based numbering for processor IDs and the //coordinates in the processor grid. // numLocalElements_ = (L_*L_)/numProcs_; elemIDs_ = new GlobalID[numLocalElements_]; if (!elemIDs_) messageAbort("ERROR allocating elemIDs_."); elemIDsAllocated_ = true; //0-based x-coordinate of this processor in the 2D processor grid. procX_ = localProc_%int_sqrt(numProcs_); //0-based maximum processor x-coordinate. maxProcX_ = int_sqrt(numProcs_) - 1; //0-based y-coordinate of this processor in the 2D processor grid. procY_ = localProc_/int_sqrt(numProcs_); //0-based maximum processor y-coordinate. maxProcY_ = int_sqrt(numProcs_) - 1; int sqrtElems = int_sqrt(numLocalElements_); int sqrtProcs = int_sqrt(numProcs_); //1-based first-element-on-this-processor startElement_ = 1 + procY_*sqrtProcs*numLocalElements_ + procX_*sqrtElems; if (outputLevel_>1) { FEI_COUT << localProc_ << ", calcDist.: numLocalElements: " << numLocalElements_ << ", startElement: " << startElement_ << FEI_ENDL; FEI_COUT << localProc_ << ", procX: " << procX_ << ", procY_: " << procY_ << ", maxProcX: " << maxProcX_ << ", maxProcY: " << maxProcY_ << FEI_ENDL; } int offset = 0; for(int i=0; i<sqrtElems; i++) { for(int j=0; j<sqrtElems; j++) { elemIDs_[offset] = (GlobalID)(startElement_ + i*L_ + j); offset++; } } }
//============================================================================== void PoissonData::check1() { // //Private function to be called from the constructor, simply makes sure that //the constructor's input arguments were reasonable. // //If they aren't, a message is printed on standard err, and abort() is called. // if (L_ <= 0) messageAbort("bar length L <= 0."); if (numProcs_ <= 0) messageAbort("numProcs <= 0."); if (L_%int_sqrt(numProcs_)) messageAbort("L must be an integer multiple of sqrt(numProcs)."); if (localProc_ < 0) messageAbort("localProc < 0."); if (localProc_ >= numProcs_) messageAbort("localProc >= numProcs."); if (outputLevel_ < 0) messageAbort("outputLevel < 0."); }
static u32 get_typical_interval(struct menu_device *data) { int i = 0, divisor = 0; uint64_t max = 0, avg = 0, stddev = 0; int64_t thresh = LLONG_MAX; /* Discard outliers above this value. */ unsigned int ret = 0; again: /* first calculate average and standard deviation of the past */ max = avg = divisor = stddev = 0; for (i = 0; i < INTERVALS; i++) { int64_t value = data->intervals[i]; if (value <= thresh) { avg += value; divisor++; if (value > max) max = value; } } do_div(avg, divisor); for (i = 0; i < INTERVALS; i++) { int64_t value = data->intervals[i]; if (value <= thresh) { int64_t diff = value - avg; stddev += diff * diff; } } do_div(stddev, divisor); stddev = int_sqrt(stddev); if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3)) || stddev <= 20) { data->predicted_us = avg; ret = 1; return ret; } else if ((divisor * 4) > INTERVALS * 3) { /* Exclude the max interval */ thresh = max - 1; goto again; } return ret; }
static void silly_thread_func(void) { static unsigned long count; unsigned long res; ktime_t x, y; s64 z; set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ); count++; x = ktime_get(); res = int_sqrt(17); y = ktime_get(); z = ktime_to_ns(ktime_sub(y, x)); trace_me_silly( (unsigned long)z, count); printk("hello! %lu int_sqrt(17)=%lu\n", count, res); }
int main(void) { static unsigned n, x, y; register unsigned i, t; scanf("%u\n", &n); while (n-- > 0) { if (scanf("%u %u", &x, &y) != 2) break; i = (x < y) ? (y - x) : (x - y); if (i < 4) { printf("%u\n", i); } else { t = int_sqrt(i - 1); printf("%u\n", t + t + ((i > (t*t + t)) ? 1 : 0)); } } return 0; }
static u32 noise_magnitude(s16 x[], u32 N, u32 freq_start, u32 freq_end) { int i; u32 sum = 0; u32 freq_step; int samples = 5; if (N > 192) { /* The last 192 samples are enough for noise detection */ x += (N-192); N = 192; } freq_step = (freq_end - freq_start) / (samples - 1); for (i = 0; i < samples; i++) { sum += int_goertzel(x, N, freq_start); freq_start += freq_step; } return (u32)int_sqrt(sum / samples); }
static u32 freq_magnitude(s16 x[], u32 N, u32 freq) { u32 sum = int_goertzel(x, N, freq); return (u32)int_sqrt(sum); }
static int run_psnr(FILE *f[2], int len, int shift, int skip_bytes) { int i, j; uint64_t sse = 0; double sse_d = 0.0; uint8_t buf[2][SIZE]; int64_t max = (1LL << (8 * len)) - 1; int size0 = 0; int size1 = 0; uint64_t maxdist = 0; double maxdist_d = 0.0; int noseek; noseek = fseek(f[0], 0, SEEK_SET) || fseek(f[1], 0, SEEK_SET); if (!noseek) { for (i = 0; i < 2; i++) { uint8_t *p = buf[i]; if (fread(p, 1, 12, f[i]) != 12) return -1; if (!memcmp(p, "RIFF", 4) && !memcmp(p + 8, "WAVE", 4)) { if (fread(p, 1, 8, f[i]) != 8) return -1; while (memcmp(p, "data", 4)) { int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24; fseek(f[i], s, SEEK_CUR); if (fread(p, 1, 8, f[i]) != 8) return -1; } } else { fseek(f[i], -12, SEEK_CUR); } } fseek(f[shift < 0], abs(shift), SEEK_CUR); fseek(f[0], skip_bytes, SEEK_CUR); fseek(f[1], skip_bytes, SEEK_CUR); } for (;;) { int s0 = fread(buf[0], 1, SIZE, f[0]); int s1 = fread(buf[1], 1, SIZE, f[1]); for (j = 0; j < FFMIN(s0, s1); j += len) { switch (len) { case 1: case 2: { int64_t a, b; int dist; if (len == 2) { a = get_s16l(buf[0] + j); b = get_s16l(buf[1] + j); } else { a = buf[0][j]; b = buf[1][j]; } sse += (a - b) * (a - b); dist = llabs(a - b); if (dist > maxdist) maxdist = dist; break; } case 4: case 8: { double dist, a, b; if (len == 8) { a = get_f64l(buf[0] + j); b = get_f64l(buf[1] + j); } else { a = get_f32l(buf[0] + j); b = get_f32l(buf[1] + j); } dist = fabs(a - b); sse_d += (a - b) * (a - b); if (dist > maxdist_d) maxdist_d = dist; break; } } } size0 += s0; size1 += s1; if (s0 + s1 <= 0) break; } i = FFMIN(size0, size1) / len; if (!i) i = 1; switch (len) { case 1: case 2: { uint64_t psnr; uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i); if (sse) psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) * 284619LL * F + (1LL << 31)) / (1LL << 32); else psnr = 1000 * F - 1; // floating point free infinity :) printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9d/%9d\n", (int)(dev / F), (int)(dev % F), (int)(psnr / F), (int)(psnr % F), maxdist, size0, size1); return psnr; } case 4: case 8: { char psnr_str[64]; double psnr = INT_MAX; double dev = sqrt(sse_d / i); uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32); if (sse_d) { psnr = 2 * log(DBL_MAX) - log(i / sse_d); snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr); } else snprintf(psnr_str, sizeof(psnr_str), "inf"); maxdist = maxdist_d * scale; printf("stddev:%10.2f PSNR:%s MAXDIFF:%10"PRIu64" bytes:%9d/%9d\n", dev * scale, psnr_str, maxdist, size0, size1); return psnr; } } return -1; }
int main(int argc, char *argv[]) { uint64_t i, j; uint64_t sse = 0; double sse_d = 0.0; FILE *f[2]; uint8_t buf[2][SIZE]; int len = 1; int64_t max; int shift = argc < 5 ? 0 : atoi(argv[4]); int skip_bytes = argc < 6 ? 0 : atoi(argv[5]); uint64_t size0 = 0; uint64_t size1 = 0; uint64_t maxdist = 0; double maxdist_d = 0.0; if (argc < 3) { printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n"); printf("WAV headers are skipped automatically.\n"); return 1; } if (argc > 3) { if (!strcmp(argv[3], "u8")) { len = 1; } else if (!strcmp(argv[3], "s16")) { len = 2; } else if (!strcmp(argv[3], "f32")) { len = 4; } else if (!strcmp(argv[3], "f64")) { len = 8; } else { char *end; len = strtol(argv[3], &end, 0); if (*end || len < 1 || len > 2) { fprintf(stderr, "Unsupported sample format: %s\n", argv[3]); return 1; } } } max = (1LL << (8 * len)) - 1; f[0] = fopen(argv[1], "rb"); f[1] = fopen(argv[2], "rb"); if (!f[0] || !f[1]) { fprintf(stderr, "Could not open input files.\n"); return 1; } for (i = 0; i < 2; i++) { uint8_t *p = buf[i]; if (fread(p, 1, 12, f[i]) != 12) return 1; if (!memcmp(p, "RIFF", 4) && !memcmp(p + 8, "WAVE", 4)) { if (fread(p, 1, 8, f[i]) != 8) return 1; while (memcmp(p, "data", 4)) { int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24; fseek(f[i], s, SEEK_CUR); if (fread(p, 1, 8, f[i]) != 8) return 1; } } else { fseek(f[i], -12, SEEK_CUR); } } fseek(f[shift < 0], abs(shift), SEEK_CUR); fseek(f[0], skip_bytes, SEEK_CUR); fseek(f[1], skip_bytes, SEEK_CUR); for (;;) { int s0 = fread(buf[0], 1, SIZE, f[0]); int s1 = fread(buf[1], 1, SIZE, f[1]); for (j = 0; j < FFMIN(s0, s1); j += len) { switch (len) { case 1: case 2: { int64_t a = buf[0][j]; int64_t b = buf[1][j]; int dist; if (len == 2) { a = get_s16l(buf[0] + j); b = get_s16l(buf[1] + j); } else { a = buf[0][j]; b = buf[1][j]; } sse += (a - b) * (a - b); dist = llabs(a - b); if (dist > maxdist) maxdist = dist; break; } case 4: case 8: { double dist, a, b; if (len == 8) { a = get_f64l(buf[0] + j); b = get_f64l(buf[1] + j); } else { a = get_f32l(buf[0] + j); b = get_f32l(buf[1] + j); } dist = fabs(a - b); sse_d += (a - b) * (a - b); if (dist > maxdist_d) maxdist_d = dist; break; } } } size0 += s0; size1 += s1; if (s0 + s1 <= 0) break; } i = FFMIN(size0, size1) / len; if (!i) i = 1; switch (len) { case 1: case 2: { uint64_t psnr; uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i); if (sse) psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) * 284619LL * F + (1LL << 31)) / (1LL << 32); else psnr = 1000 * F - 1; // floating point free infinity :) printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9"PRIu64"/%9"PRIu64"\n", (int)(dev / F), (int)(dev % F), (int)(psnr / F), (int)(psnr % F), maxdist, size0, size1); break; } case 4: case 8: { char psnr_str[64]; double dev = sqrt(sse_d / i); uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32); if (sse_d) { double psnr = 2 * log(DBL_MAX) - log(i / sse_d); snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr); } else snprintf(psnr_str, sizeof(psnr_str), "inf"); maxdist = maxdist_d * scale; printf("stddev:%10.2f PSNR:%s MAXDIFF:%10"PRIu64" bytes:%9"PRIu64"/%9"PRIu64"\n", dev * scale, psnr_str, maxdist, size0, size1); break; } } return 0; }
static int run_psnr(FILE *f[2], int len, int shift, int skip_bytes) { int i, j; uint64_t sse = 0; double sse_d = 0.0; uint8_t buf[2][SIZE]; int64_t max = (1LL << (8 * len)) - 1; int size0 = 0; int size1 = 0; uint64_t maxdist = 0; double maxdist_d = 0.0; int noseek; noseek = fseek(f[0], 0, SEEK_SET) || fseek(f[1], 0, SEEK_SET); if (!noseek) { for (i = 0; i < 2; i++) { uint8_t *p = buf[i]; if (fread(p, 1, 12, f[i]) != 12) return 1; if (!memcmp(p, "RIFF", 4) && !memcmp(p + 8, "WAVE", 4)) { if (fread(p, 1, 8, f[i]) != 8) return 1; while (memcmp(p, "data", 4)) { int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24; fseek(f[i], s, SEEK_CUR); if (fread(p, 1, 8, f[i]) != 8) return 1; } } else { fseek(f[i], -12, SEEK_CUR); } } fseek(f[shift < 0], abs(shift), SEEK_CUR); fseek(f[0], skip_bytes, SEEK_CUR); fseek(f[1], skip_bytes, SEEK_CUR); } fflush(stdout); for (;;) { int s0 = fread(buf[0], 1, SIZE, f[0]); int s1 = fread(buf[1], 1, SIZE, f[1]); int tempsize = FFMIN(s0,s1); DECLARE_ALIGNED(32, FFTComplex, fftcomplexa)[SIZE/len]; DECLARE_ALIGNED(32, FFTComplex, fftcomplexb)[SIZE/len]; for (j = 0; j < tempsize; j += len) { switch (len) { case 1: case 2: { int64_t a = buf[0][j]; int64_t b = buf[1][j]; int dist; if (len == 2) { fftcomplexa[j/len].re = get_s16l(buf[0] + j); fftcomplexb[j/len].re = get_s16l(buf[1] + j); fftcomplexa[j/len].im = 0; fftcomplexb[j/len].im = 0; } else { fftcomplexa[j/len].re = buf[0][j]; fftcomplexb[j/len].re = buf[1][j]; fftcomplexa[j/len].im = 0; fftcomplexb[j/len].im = 0; } dist = abs(fftcomplexa[j/len].re-fftcomplexb[j/len].re); if (dist > maxdist) maxdist = dist; break; break; } case 4: case 8: { double dist, a, b; if (len == 8) { fftcomplexa[j/len].re = (float) get_f64l(buf[0] + j); fftcomplexb[j/len].re = (float) get_f64l(buf[1] + j); fftcomplexa[j/len].im = 0; fftcomplexb[j/len].im = 0; } else { fftcomplexa[j/len].re = (float) get_f32l(buf[0] + j); fftcomplexb[j/len].re = (float) get_f32l(buf[1] + j); fftcomplexa[j/len].im = 0; fftcomplexb[j/len].im = 0; } dist = abs(fftcomplexa[j/len].re-fftcomplexb[j/len].re); if (dist > maxdist_d) maxdist_d = dist; break; } } } for(;j<SIZE;j+=len){ fftcomplexa[j/len].re = 0; fftcomplexb[j/len].re = 0; fftcomplexa[j/len].im = 0; fftcomplexb[j/len].im = 0; } size0 += s0; size1 += s1; if (s0 + s1 <= 0) break; FFTContext* fftcontexta = av_fft_init(floor(log2(SIZE/len)),0); av_fft_permute (fftcontexta, fftcomplexa); int temp = 0; av_fft_calc (fftcontexta, fftcomplexa); FFTContext* fftcontextb = av_fft_init(floor(log2(SIZE/len)),0); av_fft_permute (fftcontextb, fftcomplexb); av_fft_calc (fftcontextb, fftcomplexb); float* maskingfunc = get_mask_array(SIZE/len); float* mask = get_mask(fftcomplexa, SIZE/len, maskingfunc); double psysse = get_psy_sse(fftcomplexa,fftcomplexb, mask, SIZE/len); free(maskingfunc); free(mask); sse+=psysse; sse_d+=psysse; } fflush(stdout); i = FFMIN(size0, size1) / len; if (!i) i = 1; switch (len) { case 1: case 2: { uint64_t psnr; uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i); if (sse) psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) * 284619LL * F + (1LL << 31)) / (1LL << 32); else psnr = 1000 * F - 1; // floating point free infinity :) printf("stddev:%5d.%02d PSYSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9d/%9d\n", (int)(dev / F), (int)(dev % F), (int)(psnr / F), (int)(psnr % F), maxdist, size0, size1); return psnr; } case 4: case 8: { char psnr_str[64]; double psnr = INT_MAX; double dev = sqrt(sse_d / i); uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32); if (sse_d) { psnr = 2 * log(DBL_MAX) - log(i / sse_d); snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr); } else snprintf(psnr_str, sizeof(psnr_str), "inf"); maxdist = maxdist_d * scale; printf("stddev:%10.2f PSYSNR:%s MAXDIFF:%10"PRIu64" bytes:%9d/%9d\n", dev * scale, psnr_str, maxdist, size0, size1); return psnr; } } return -1; }
int main(int argc,char* argv[]){ int i, j; uint64_t sse=0; uint64_t dev; FILE *f[2]; uint8_t buf[2][SIZE]; uint64_t psnr; int len= argc<4 ? 1 : atoi(argv[3]); int64_t max= (1<<(8*len))-1; int shift= argc<5 ? 0 : atoi(argv[4]); int skip_bytes = argc<6 ? 0 : atoi(argv[5]); int size0=0; int size1=0; int maxdist = 0; if(argc<3){ printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n"); printf("WAV headers are skipped automatically.\n"); return 1; } f[0]= fopen(argv[1], "rb"); f[1]= fopen(argv[2], "rb"); if(!f[0] || !f[1]){ fprintf(stderr, "Could not open input files.\n"); return 1; } for (i = 0; i < 2; i++) { uint8_t *p = buf[i]; if (fread(p, 1, 12, f[i]) != 12) return 1; if (!memcmp(p, "RIFF", 4) && !memcmp(p+8, "WAVE", 4)) { if (fread(p, 1, 8, f[i]) != 8) return 1; while (memcmp(p, "data", 4)) { int s = p[4] | p[5]<<8 | p[6]<<16 | p[7]<<24; fseek(f[i], s, SEEK_CUR); if (fread(p, 1, 8, f[i]) != 8) return 1; } } else { fseek(f[i], -12, SEEK_CUR); } } fseek(f[shift<0], abs(shift), SEEK_CUR); fseek(f[0],skip_bytes,SEEK_CUR); fseek(f[1],skip_bytes,SEEK_CUR); for(;;){ int s0= fread(buf[0], 1, SIZE, f[0]); int s1= fread(buf[1], 1, SIZE, f[1]); for(j=0; j<FFMIN(s0,s1); j++){ int64_t a= buf[0][j]; int64_t b= buf[1][j]; int dist; if(len==2){ a= (int16_t)(a | (buf[0][++j]<<8)); b= (int16_t)(b | (buf[1][ j]<<8)); } sse += (a-b) * (a-b); dist = abs(a-b); if (dist > maxdist) maxdist = dist; } size0 += s0; size1 += s1; if(s0+s1<=0) break; } i= FFMIN(size0,size1)/len; if(!i) i=1; dev= int_sqrt( ((sse/i)*F*F) + (((sse%i)*F*F) + i/2)/i ); if(sse) psnr= ((2*log16(max<<16) + log16(i) - log16(sse))*284619LL*F + (1LL<<31)) / (1LL<<32); else psnr= 1000*F-1; //floating point free infinity :) printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5d bytes:%9d/%9d\n", (int)(dev/F), (int)(dev%F), (int)(psnr/F), (int)(psnr%F), maxdist, size0, size1); return 0; }
long sieb8(long n){ typedef long vecType; // type of a calcunit #define bits (8*sizeof(vecType)) // bits per calcunit // b is the number of bits per calcblock // the number p is at the index k // p = 2k+3 // the index k is at calcblock x and bit y // bx+y = k // p = 2(bx+y)+3 // to calc the size of array we need to make sure // that the number n (passed to function) is in the array // n = 2(bx+y)+3 // (n-3)/2 = bx+y // // the size in bits we need s in defined as // s = ((n-3)/2)+1 = (n-1)/2 // because the size is the highest index +1 // // the amount of calcblocks we need l is // l = (n-1)/(2b) // // so to use the full memory power pass numbers n that // fullfill the following equation, where l is an int // n = 2bl+1 long len = (n-1)/(2*bits); long lenBits = (n-1)/2; // stores the numberarray // if a bit is 1 in this array means that the number is not not a prime number vecType* prime; // vecType* prime = malloc(len*sizeof(vecType)); if(posix_memalign((void**)&prime,getpagesize(),len*sizeof(vecType)) != 0){ return -1; } memset(prime,0,len*sizeof(vecType)); long sqrt = int_sqrt(len)+1; // numbers only need to be checked up to the sqrt of the len long size = 1; // the amount of primenumbers found long ax; // the calcblock index long ay; // the bit index in the calcblock // decribed on initialization long current; long offset; long axbits2plus3; // we now iterate over the numberarray // ax and ay represent the real number 2*(BITS*ax+ay)+3 // only calculations up to sqrt are nessecary for(ax=0 ; ax<=sqrt ; ++ax){ // calculate a number that is always the same for the same ax // makes computations faster later on axbits2plus3 = ax*bits*2+3; for(ay=0 ; ay<bits ; ++ay){ // test the actual bit, if true it is a prime if((prime[ax] & (vecType)1<<ay) == 0){ // now cross out all multiples of this prime number // the first index we cross out is the current // primenumber squared cause all smaller multiples // of the prime are cross out already, the bit index k of // that sqaure can be calculated as // current prime number is // p = 2*(bits*ax+ay)+3 // first crossout number n is // n = p^2 // bit index k of that number is // n = 2k+3 // k = (n-3)/2 // k = (p^2-3)/2 // to get from one multiple to the next we just // add an offset that we can calculate as // primenumber p is // p = 2*(bits*ax+ay)+3 // second crossed out number n is // n = p^2+p // which is at the index k // n = 2k+3 // k = (n-3)/2 // k = (p^2+p-3)/2 // k = (p^2-3)/2 + p/2 // we see that the second cross out is p/2 after the // first crossout which is our offset. // but wait, we don't really need to check the // even multiples of the prime number since they were // removed already. All checked primenumbers are uneven so their // square is uneven too. so we only need to check every other offset // leading us to an index offset that is exactly the primenumber !!! // (this is pure luck) // 2*(ax*bits+ay)+3 = 2*ax*bits+3 + 2*ay offset = axbits2plus3 + 2*ay; // 2*(bits*ax+ay)+3 // the index after the first index is the number current = (offset*offset-3)/2; // current and offset are messured in bits for( ; current<lenBits ; current+=offset){ prime[current/bits] |= (vecType)1<<(current%bits); } ++size; } } } // now make up for only calculating up to sqrt // and add the rest of primenumbers to the size for( ; ax<len ; ++ax){ for(ay=0 ; ay<bits ; ++ay){ if((prime[ax] & (vecType)1<<ay) == 0){ ++size; } } } free(prime); return size; #undef bits }
static int mx2_camera_try_fmt(struct soc_camera_device *icd, struct v4l2_format *f) { struct v4l2_subdev *sd = soc_camera_to_subdev(icd); const struct soc_camera_format_xlate *xlate; struct v4l2_pix_format *pix = &f->fmt.pix; struct v4l2_mbus_framefmt mf; __u32 pixfmt = pix->pixelformat; unsigned int width_limit; int ret; xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); if (pixfmt && !xlate) { dev_warn(icd->parent, "Format %x not found\n", pixfmt); return -EINVAL; } /* FIXME: implement MX27 limits */ /* limit to MX25 hardware capabilities */ if (cpu_is_mx25()) { if (xlate->host_fmt->bits_per_sample <= 8) width_limit = 0xffff * 4; else width_limit = 0xffff * 2; /* CSIIMAG_PARA limit */ if (pix->width > width_limit) pix->width = width_limit; if (pix->height > 0xffff) pix->height = 0xffff; pix->bytesperline = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); if (pix->bytesperline < 0) return pix->bytesperline; pix->sizeimage = pix->height * pix->bytesperline; /* Check against the CSIRXCNT limit */ if (pix->sizeimage > 4 * 0x3ffff) { /* Adjust geometry, preserve aspect ratio */ unsigned int new_height = int_sqrt(4 * 0x3ffff * pix->height / pix->bytesperline); pix->width = new_height * pix->width / pix->height; pix->height = new_height; pix->bytesperline = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); BUG_ON(pix->bytesperline < 0); } } /* limit to sensor capabilities */ mf.width = pix->width; mf.height = pix->height; mf.field = pix->field; mf.colorspace = pix->colorspace; mf.code = xlate->code; ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf); if (ret < 0) return ret; if (mf.field == V4L2_FIELD_ANY) mf.field = V4L2_FIELD_NONE; if (mf.field != V4L2_FIELD_NONE) { dev_err(icd->parent, "Field type %d unsupported.\n", mf.field); return -EINVAL; } pix->width = mf.width; pix->height = mf.height; pix->field = mf.field; pix->colorspace = mf.colorspace; return 0; }
/* * Test whether a given integer is a perfect square. */ bool is_square(const uint64_t n) { uint64_t sqrt = int_sqrt(n); return sqrt * sqrt == n; }
/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ static int nfs_write_mapping(struct address_space *mapping, int how) { struct writeback_control wbc = { .bdi = mapping->backing_dev_info, .sync_mode = WB_SYNC_ALL, .nr_to_write = LONG_MAX, .range_start = 0, .range_end = LLONG_MAX, }; return __nfs_write_mapping(mapping, &wbc, how); } /* * flush the inode to disk. */ int nfs_wb_all(struct inode *inode) { return nfs_write_mapping(inode->i_mapping, 0); } int nfs_wb_nocommit(struct inode *inode) { return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); } int nfs_wb_page_cancel(struct inode *inode, struct page *page) { struct nfs_page *req; loff_t range_start = page_offset(page); loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); struct writeback_control wbc = { .bdi = page->mapping->backing_dev_info, .sync_mode = WB_SYNC_ALL, .nr_to_write = LONG_MAX, .range_start = range_start, .range_end = range_end, }; int ret = 0; BUG_ON(!PageLocked(page)); for (;;) { req = nfs_page_find_request(page); if (req == NULL) goto out; if (test_bit(PG_CLEAN, &req->wb_flags)) { nfs_release_request(req); break; } if (nfs_lock_request_dontget(req)) { nfs_inode_remove_request(req); /* * In case nfs_inode_remove_request has marked the * page as being dirty */ cancel_dirty_page(page, PAGE_CACHE_SIZE); nfs_unlock_request(req); break; } ret = nfs_wait_on_request(req); if (ret < 0) goto out; } if (!PagePrivate(page)) return 0; ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); out: return ret; } static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how) { loff_t range_start = page_offset(page); loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); struct writeback_control wbc = { .bdi = page->mapping->backing_dev_info, .sync_mode = WB_SYNC_ALL, .nr_to_write = LONG_MAX, .range_start = range_start, .range_end = range_end, }; int ret; do { if (clear_page_dirty_for_io(page)) { ret = nfs_writepage_locked(page, &wbc); if (ret < 0) goto out_error; } else if (!PagePrivate(page)) break; ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); if (ret < 0) goto out_error; } while (PagePrivate(page)); return 0; out_error: __mark_inode_dirty(inode, I_DIRTY_PAGES); return ret; } /* * Write back all requests on one page - we do this before reading it. */ int nfs_wb_page(struct inode *inode, struct page* page) { return nfs_wb_page_priority(inode, page, FLUSH_STABLE); } #ifdef CONFIG_MIGRATION int nfs_migrate_page(struct address_space *mapping, struct page *newpage, struct page *page) { struct nfs_page *req; int ret; if (PageFsCache(page)) nfs_fscache_release_page(page, GFP_KERNEL); req = nfs_find_and_lock_request(page); ret = PTR_ERR(req); if (IS_ERR(req)) goto out; ret = migrate_page(mapping, newpage, page); if (!req) goto out; if (ret) goto out_unlock; page_cache_get(newpage); req->wb_page = newpage; SetPagePrivate(newpage); set_page_private(newpage, page_private(page)); ClearPagePrivate(page); set_page_private(page, 0); page_cache_release(page); out_unlock: nfs_clear_page_tag_locked(req); nfs_release_request(req); out: return ret; } #endif int __init nfs_init_writepagecache(void) { nfs_wdata_cachep = kmem_cache_create("nfs_write_data", sizeof(struct nfs_write_data), 0, SLAB_HWCACHE_ALIGN, NULL); if (nfs_wdata_cachep == NULL) return -ENOMEM; nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, nfs_wdata_cachep); if (nfs_wdata_mempool == NULL) return -ENOMEM; nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, nfs_wdata_cachep); if (nfs_commit_mempool == NULL) return -ENOMEM; /* * NFS congestion size, scale with available memory. * * 64MB: 8192k * 128MB: 11585k * 256MB: 16384k * 512MB: 23170k * 1GB: 32768k * 2GB: 46340k * 4GB: 65536k * 8GB: 92681k * 16GB: 131072k * * This allows larger machines to have larger/more transfers. * Limit the default to 256M */ nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); if (nfs_congestion_kb > 256*1024) nfs_congestion_kb = 256*1024; return 0; } void nfs_destroy_writepagecache(void) { mempool_destroy(nfs_commit_mempool); mempool_destroy(nfs_wdata_mempool); kmem_cache_destroy(nfs_wdata_cachep); }
//============================================================================== void PoissonData::getBottomSharedNodes(int& numShared, GlobalID* sharedNodeIDs, int* numProcsPerSharedNode, int** sharingProcs) { // //This function decides whether any of the nodes along the bottom edge, //including the left node but not the right node, are shared. It also //decides which processors the nodes are shared with. // if (numProcs_ == 1) { numShared = 0; return; } if (procY_ == 0) { //if this proc is on the bottom edge of the square... if (procX_ > 0) { //if this proc is not the bottom left proc... numShared = 1; int elemIndex = 0; elem_->setElemID(elemIDs_[elemIndex]); //now get a pointer to this element's connectivity array and //calculate that connectivity (in place). int size; GlobalID* nodes = elem_->getElemConnPtr(size); if (size == 0) messageAbort(": bad conn ptr."); calculateConnectivity(nodes, size, elemIDs_[elemIndex]); sharedNodeIDs[0] = nodes[0]; //elem's bottom left node is node 0 numProcsPerSharedNode[0] = 2; sharingProcs[0][0] = localProc_; sharingProcs[0][1] = localProc_ - 1; return; } else { //else this proc is the top right proc... numShared = 0; } } else { //else this proc is not on the bottom edge of the square... numShared = int_sqrt(numLocalElements_); int lowerRightElemIndex = int_sqrt(numLocalElements_) - 1; int sqrtElems = int_sqrt(numLocalElements_); int shOffset = 0; for(int i=0; i<sqrtElems; i++){ //stride across the bottom edge of the local elements, from //right to left... int size=0; int elemIndex = lowerRightElemIndex-i; elem_->setElemID(elemIDs_[elemIndex]); //now get a pointer to this element's connectivity array and //calculate that connectivity (in place). GlobalID* nodes = elem_->getElemConnPtr(size); if (size == 0) messageAbort(": bad conn ptr."); calculateConnectivity(nodes, size, elemIDs_[elemIndex]); //now put in the lower left node sharedNodeIDs[shOffset] = nodes[0]; sharingProcs[shOffset][0] = localProc_ - int_sqrt(numProcs_); sharingProcs[shOffset][1] = localProc_; numProcsPerSharedNode[shOffset++] = 2; } if (procX_ > 0) { //if this proc isn't on the left edge, the lower left node (the //last one we put into the shared node list) is shared by 4 procs. shOffset--; numProcsPerSharedNode[shOffset] = 4; sharingProcs[shOffset][2] = localProc_ - 1; sharingProcs[shOffset][3] = sharingProcs[shOffset][0] - 1; } } }
void load_shapes () { unsigned char Grid[33][33]; char *Error1 = "Bad Shape Selected Error"; int Shape; int x, y, z; int Style, Color; int X_Width, Y_Width, Center, S_Width; int Hollow_X, Hollow_Y; for (Shape = 0; Shape < MAX_SHAPES; Shape++) { for (y = 0; y <= 32; y++) { for (x = 0; x <= 32; x++) { Grid[x][y] = c_BLACK; } } Style = random_int (6); Color = 1 + random_int (15); switch (Style) { /* SOLID BOXES */ case 0: { do { X_Width = 3 + random_int(30); Y_Width = 3 + random_int(30); } while ( (X_Width * Y_Width) >= 512); for (x = 1; x <= X_Width; x++) { for (y = 1; y <= Y_Width; y++) { Grid[x][y] = Color; } } break; } /* HOLLOW BOXES */ case 1: { do { X_Width = 6 + random_int(27); Y_Width = 6 + random_int(27); } while ( (X_Width * Y_Width) >= 512); for (y = 1; y <= Y_Width; y++) { for (x = 1; x <= X_Width; x++) { Grid[x][y] = Color; } } Hollow_X = 1 + random_int ((X_Width / 2) -1); Hollow_Y = 1 + random_int ((Y_Width / 2) -1); for (y = Hollow_Y+1; y <= Y_Width-Hollow_Y; y++) { for (x = Hollow_X+1; x <= X_Width-Hollow_X; x++) { Grid[x][y] = c_BLACK; } } break; } /* SOLID DIAMOND */ case 2: { X_Width = 3 + 2 * random_int(10); Y_Width = X_Width; Center = X_Width / 2; for (y = 0; y <= Center; y++) { for (x = 0; x <= y; x++) { Grid [Center-x+1][y+1] = Color; Grid [Center+x+1][y+1] = Color; Grid [Center-x+1][Y_Width-y] = Color; Grid [Center+x+1][Y_Width-y] = Color; } } break; } /* HOLLOW DIAMOND */ case 3: { X_Width = 3 + 2 * random_int(10); Y_Width = X_Width; Center = X_Width / 2; S_Width = random_int (Center); for (y = 0; y <= Center; y++) { for (x = 0; x <= y; x++) { if ( x+(Center-y) >= S_Width ) { Grid [Center-x+1][y+1] = Color; Grid [Center+x+1][y+1] = Color; Grid [Center-x+1][Y_Width-y] = Color; Grid [Center+x+1][Y_Width-y] = Color; } } } break; } /* BALL */ case 4: { X_Width = 7 + 2 * random_int (8); Y_Width = X_Width; Center = 1 + X_Width / 2; for (y = 1; y <= Y_Width; y++) { for (x = 1; x <= X_Width; x++) { z = int_sqrt(Center-x, Center-y); if (z < Center) { Grid[x][y] = 150 + Color * 2 + z * 3; } } } break; } /* HOLLOW BALLS */ case 5: { X_Width = 7 + 2 * random_int (8); Y_Width = X_Width; Center = 1 + X_Width / 2; S_Width = random_int (X_Width); for (y = 1; y <= Y_Width; y++) { for (x = 1; x <= X_Width; x++) { z = int_sqrt(Center-x, Center-y); if ( (z < Center) && (z >= S_Width) ) { Grid[x][y] = 150 + Color * 2 + z * 3; } } } break; } default: { error_out (Error1); break; } } z = 0; for (y = 1; y <= Y_Width; y++) { for (x = 1; x <= X_Width; x++) { Img[Shape].Image[z] = Grid[x][y]; z++; } } Img[Shape].X_Width = X_Width; Img[Shape].Y_Width = Y_Width; } return; }
/* * Try detecting repeating patterns by keeping track of the last 8 * intervals, and checking if the standard deviation of that set * of points is below a threshold. If it is... then use the * average of these 8 points as the estimated value. */ static void get_typical_interval(struct menu_device *data) { int i, divisor; unsigned int max, thresh; uint64_t avg, stddev; thresh = UINT_MAX; /* Discard outliers above this value */ again: /* First calculate the average of past intervals */ max = 0; avg = 0; divisor = 0; for (i = 0; i < INTERVALS; i++) { unsigned int value = data->intervals[i]; if (value <= thresh) { avg += value; divisor++; if (value > max) max = value; } } do_div(avg, divisor); /* Then try to determine standard deviation */ stddev = 0; for (i = 0; i < INTERVALS; i++) { unsigned int value = data->intervals[i]; if (value <= thresh) { int64_t diff = value - avg; stddev += diff * diff; } } do_div(stddev, divisor); /* * The typical interval is obtained when standard deviation is small * or standard deviation is small compared to the average interval. * * int_sqrt() formal parameter type is unsigned long. When the * greatest difference to an outlier exceeds ~65 ms * sqrt(divisor) * the resulting squared standard deviation exceeds the input domain * of int_sqrt on platforms where unsigned long is 32 bits in size. * In such case reject the candidate average. * * Use this result only if there is no timer to wake us up sooner. */ if (likely(stddev <= ULONG_MAX)) { stddev = int_sqrt(stddev); if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3)) || stddev <= 20) { if (data->expected_us > avg) data->predicted_us = avg; return; } } /* * If we have outliers to the upside in our distribution, discard * those by setting the threshold to exclude these outliers, then * calculate the average and standard deviation again. Once we get * down to the bottom 3/4 of our samples, stop excluding samples. * * This can deal with workloads that have long pauses interspersed * with sporadic activity with a bunch of short pauses. */ if ((divisor * 4) <= INTERVALS * 3) return; thresh = max - 1; goto again; }
static int filter_double_point(struct sunxi_ts_data *ts_data, struct ts_sample_data *sample_data) { int ret = 0; static int prev_sample_ds = 0; static int cur_sample_ds = 0; static int delta_ds = 0; if (ZOOM_INIT_STATE == zoom_flag && (0 == zoom_out_count && 0 == zoom_in_count)) { prev_sample_ds = int_sqrt((prev_double_sample_data.dx)*(prev_double_sample_data.dx) \ + (prev_double_sample_data.dy)*(prev_double_sample_data.dy)); } cur_sample_ds = int_sqrt((sample_data->dx)*(sample_data->dx) + (sample_data->dy)*(sample_data->dy)); delta_ds = cur_sample_ds - prev_sample_ds; /* update prev_double_sample_data */ memcpy((void*)&prev_double_sample_data, (void*)sample_data, sizeof(*sample_data)); prev_sample_ds = cur_sample_ds; if(delta_ds > HOLD_DS_LIMIT){ /* zoom in */ if(delta_ds > min(GLIDE_DELTA_DS_MAX_LIMIT, (GLIDE_DELTA_DS_MAX_TIMES*accmulate_zoom_in_ds/zoom_in_count))){ cur_sample_ds = prev_sample_ds; /* discard the noise, and can not be reference. */ ret = TRUE; } else { accmulate_zoom_in_ds += delta_ds; zoom_in_count++; filter_zoom_in_data(&prev_report_samp, sample_data); } zoom_change_cnt = 0; accmulate_zoom_out_ds = 0; zoom_out_count = 0; }else if (ZOOM_INIT_STATE == zoom_flag ||ZOOM_STATIC == zoom_flag) { if(ZOOM_IN == zoom_flag) { /* zoom out when zoom in */ dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "delta_ds = %d, (4*accmulate_zoom_in_ds/zoom_in_count) = %d. \n", \ -delta_ds, (4*accmulate_zoom_in_ds/zoom_in_count)); if (delta_ds > min(GLIDE_DELTA_DS_MAX_LIMIT, (GLIDE_DELTA_DS_MAX_TIMES*accmulate_zoom_in_ds/zoom_in_count))) { /* noise */ cur_sample_ds = prev_sample_ds; /* discard the noise, and can not be reference. */ dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "sunxi-ts: noise, zoom out when zoom in. \n"); ret = TRUE; } else { /* normal zoom out */ zoom_change_cnt++; accmulate_zoom_out_ds += delta_ds; zoom_out_count++; if (zoom_change_cnt > ZOOM_OUT_CNT_LIMIT) { dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "change to ZOOM_OUT from ZOOM_IN. \n"); change_to_zoom_out(ts_data, sample_data); filter_zoom_out_data_init(); filter_zoom_out_data(&prev_report_samp, sample_data); } else { dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "sunxi-ts: normal zoom out, but this will cause twitter. \n"); ret = TRUE; } } }else if(ZOOM_OUT == zoom_flag){ /* zoom out when zoom out */ if (delta_ds > min(GLIDE_DELTA_DS_MAX_LIMIT, (GLIDE_DELTA_DS_MAX_TIMES*accmulate_zoom_out_ds/zoom_out_count))) { cur_sample_ds = prev_sample_ds; ret = TRUE; } else { accmulate_zoom_out_ds += delta_ds; zoom_out_count++; filter_zoom_out_data(&prev_report_samp, sample_data); } zoom_change_cnt = 0; accmulate_zoom_in_ds = 0; zoom_in_count = 0; } else if (ZOOM_INIT_STATE == zoom_flag ||ZOOM_STATIC == zoom_flag) { zoom_out_count ++; if (zoom_out_count > ZOOM_CHANGE_LIMIT_CNT) { accmulate_zoom_out_ds = delta_ds; zoom_out_count = 1; if (ZOOM_INIT_STATE == zoom_flag) { orientation_flag = judge_zoom_orientation(sample_data); report_up_event_implement(ts_data); } filter_zoom_out_data_init(); filter_zoom_out_data(&prev_report_samp, sample_data); dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "change to ZOOM_OUT from ZOOM_INIT_STATE. \n"); change_to_zoom_out(ts_data, sample_data); #if 1 dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "ZOOM_INIT_STATE: delta_ds= %d. \n", delta_ds); #endif } else { /* have not known orientation, discard the point */ ret = TRUE; } } } else { hold_cnt++; cur_sample_ds = prev_sample_ds; if (hold_cnt > 100000) hold_cnt = 100; if (unlikely(ZOOM_INIT_STATE == zoom_flag )) { dprintk(DEBUG_FILTER_DOUBLE_POINT_STATUS_INFO, "ZOOM_INIT_STATE: delta_ds == %d. \n", delta_ds); if(hold_cnt <= ZOOM_CHANGE_LIMIT_CNT) { //discard the first 3 point } return ret; } static void report_double_point(struct sunxi_ts_data *ts_data, struct ts_sample_data *sample_data) { int x1,x2,y1,y2; y1 = 0; y2 = 0; if (TRUE == filter_double_point(ts_data, sample_data)) { //noise return; } /* when report double point, need to clear single_touch_cnt */ ts_data->single_touch_cnt = 0; if (sample_data->dx < X_TURN_POINT) { x1 = X_CENTER_COORDINATE - (sample_data->dx<<2); x2 = X_CENTER_COORDINATE + (sample_data->dx<<2); } else { x1 = X_CENTER_COORDINATE - X_COMPENSATE - ((sample_data->dx) - X_TURN_POINT); x2 = X_CENTER_COORDINATE + X_COMPENSATE + ((sample_data->dx) - X_TURN_POINT); } #ifdef FIX_ORIENTATION orientation_flag = ORIENTATION_DEFAULT_VAL; #endif if(0 == orientation_flag) { dprintk(DEBUG_ORIENTATION_INFO, "orientation_flag: orientation is not supported or have not known, set the default orientation. \n"); orientation_flag = ORIENTATION_DEFAULT_VAL; } if (-1 == orientation_flag) { if (sample_data->dy < Y_TURN_POINT) { y1 = Y_CENTER_COORDINATE - (sample_data->dy<<1); y2 = Y_CENTER_COORDINATE + (sample_data->dy<<1); } else { y1 = Y_CENTER_COORDINATE - Y_COMPENSATE - (sample_data->dy - Y_TURN_POINT); y2 = Y_CENTER_COORDINATE + Y_COMPENSATE + (sample_data->dy - Y_TURN_POINT); } }else if (1 == orientation_flag) { if(sample_data->dy < Y_TURN_POINT){ y2 = Y_CENTER_COORDINATE - (sample_data->dy<<1); y1 = Y_CENTER_COORDINATE + (sample_data->dy<<1); } else { y2 = Y_CENTER_COORDINATE - Y_COMPENSATE - (sample_data->dy - Y_TURN_POINT); y1 = Y_CENTER_COORDINATE + Y_COMPENSATE + (sample_data->dy - Y_TURN_POINT); } } input_report_abs(ts_data->input, ABS_MT_TOUCH_MAJOR,800); input_report_abs(ts_data->input, ABS_MT_POSITION_X, x1); y1 = 4096 - y1; input_report_abs(ts_data->input, ABS_MT_POSITION_Y, y1); input_mt_sync(ts_data->input); input_report_abs(ts_data->input, ABS_MT_TOUCH_MAJOR,800); input_report_abs(ts_data->input, ABS_MT_POSITION_X, x2); y2 = 4096 -y2; input_report_abs(ts_data->input, ABS_MT_POSITION_Y, y2); input_mt_sync(ts_data->input); input_sync(ts_data->input); dprintk(DEBUG_REPORT_STATUS_INFO, "report two point: x1 = %d, y1 = %d; x2 = %d, y2 = %d. \n",x1, y1, x2, y2); dprintk(DEBUG_REPORT_STATUS_INFO, "sample_data->dx = %d, sample_data->dy = %d. \n", sample_data->dx, sample_data->dy); return; } static void report_data(struct sunxi_ts_data *ts_data, struct ts_sample_data *sample_data) { if (TRUE == change_mode) { /* only up event happened, change_mode is allowed. */ printk("err: report_data: never execute. \n "); ts_data->single_touch_cnt++; if (ts_data->single_touch_cnt > UP_TO_SINGLE_CNT_LIMIT) { /* change to single touch mode */ change_to_single_touch_mode(); report_single_point(ts_data, sample_data); dprintk(DEBUG_REPORT_STATUS_INFO, "change touch mode to SINGLE_TOUCH_MODE from UP state. \n"); } } else if (FALSE == change_mode) { /* keep in double touch mode remain in double touch mode */ ts_data->single_touch_cnt++; if (ts_data->single_touch_cnt > SINGLE_CNT_LIMIT) { /* to avoid unconsiously touch */ /* change to single touch mode */ change_to_single_touch_mode(); report_single_point(ts_data, sample_data); dprintk(DEBUG_REPORT_STATUS_INFO, "change touch mode to SINGLE_TOUCH_MODE from double_touch_mode. \n"); } } return; } #ifdef TP_INT_PERIOD_TEST continue; #endif if (TP_UP == sample_data->sample_status || TP_DOWN == sample_data->sample_status) { /* when received up & down event, reinitialize ts_data->double_point_cnt to debounce for DOUBLE_TOUCH_MODE */ ts_data->double_point_cnt = 0; if ((TP_DATA_VA == ts_data->ts_process_status || TP_DOWN == ts_data->ts_process_status) && data_timer_status) { /* delay 20ms , ignore up event & down event */ dprintk(DEBUG_FILTER_INFO, "(prev_sample->sample_time + ts_data->ts_delay_period) == %u, \ (sample_data->sample_time) == %u. \n", \ (prev_sample->sample_time + ts_data->ts_delay_period), \ (sample_data->sample_time)); dprintk(DEBUG_FILTER_INFO, "up or down: sample_data->sample_time = %u.sample_data->sample_status = %d \n", \ sample_data->sample_time, sample_data->sample_status); if (time_after_eq((unsigned long)(prev_sample->sample_time + ts_data->ts_delay_period - DELAY_COMPENSTAE_PEROID), \ (unsigned long)(sample_data->sample_time))) { /* notice: sample_time may overflow */ dprintk(DEBUG_FILTER_INFO, "ignore up event & down event. \n"); mod_timer(&data_timer, jiffies + ts_data->ts_delay_period); prev_sample->sample_time = sample_data->sample_time; continue; } }