int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st, int val1, int val2) { s32 value; int ret; if (val1 < 0 || val2 < 0) return -EINVAL; value = val1 * pow_10(6) + val2; if (value) { if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND) value = pow_10(9)/value; else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND) value = pow_10(6)/value; else value = 0; } ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id, st->poll.index, sizeof(value), &value); if (ret < 0 || value < 0) return -EINVAL; ret = sensor_hub_get_feature(st->hsdev, st->poll.report_id, st->poll.index, sizeof(value), &value); if (ret < 0 || value < 0) return -EINVAL; st->poll_interval = value; return 0; }
//This function checks if the digits of a number are different. boolean is_differ(int num){ int i, j, temp; for(j = 0 ; j < DIGITS_NUM-1; ++j){ temp = num; for( i = j; i < DIGITS_NUM-1; ++i){ if(i==j) temp/=pow_10(j+1); if ( num / pow_10(j) % 10 == temp % 10) return false; temp/=10; } } return true; }
int myatoi(const char *p) { const char *tmp = NULL; int status = 0;//0代表正数,1代表负数 if (*p == '-') { status = 1; tmp = p + 1;//会从第二个字符开始转化 } else if (*p == '+') { tmp = p + 1; } else { tmp = p; } int len = mystrlen(tmp); int value = 0; int i; for(i = 0; i < len; i++) { value += (tmp[i] - '0') * pow_10(len - i - 1); } if (status == 0) return value; else return -value; }
static u32 convert_to_vtf_format(int size, int exp, int val1, int val2) { u32 value; int sign = 1; if (val1 < 0 || val2 < 0) sign = -1; exp = hid_sensor_convert_exponent(exp); if (exp < 0) { value = abs(val1) * pow_10(-exp); value += abs(val2) / pow_10(6+exp); } else value = abs(val1) / pow_10(exp); if (sign < 0) value = ((1LL << (size * 8)) - value); return value; }
char *ft_itoa(int n) { int pow; int i; int negatif; char *alpha; if (n == -2147483648) return (strffjoin(ft_itoa(n / 10), ft_itoa(-(n % 10)))); negatif = n < 0; n = negatif ? -n : n; pow = 1; while (n / pow_10(pow - 1) >= 10) pow++; if (!(alpha = ft_strnew(pow + negatif))) return (NULL); i = 0 - 1; while (++i < pow) alpha[i + negatif] = (n / pow_10(pow - i - 1)) % 10 + '0'; if (negatif) alpha[0] = '-'; return (alpha); }
int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st, int val1, int val2) { s32 value; int ret; s32 current_value = 0; if (val1 < 0 || val2 < 0) return -EINVAL; value = val1 * pow_10(6) + val2; if (value) { if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND) value = pow_10(9)/value; else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND) value = pow_10(6)/value; else value = 0; } ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id, st->poll.index, value); if (ret < 0 || value < 0) return -EINVAL; ret = sensor_hub_get_feature(st->hsdev, st->poll.report_id, st->poll.index, ¤t_value); if (ret < 0) { printk(KERN_ERR "sensor_hub_get_feature failed\n"); return ret; } if (current_value != value) { printk(KERN_ERR "sensor_hub_set_feature_failed\n"); return -EINVAL; } return 0; }
static void simple_div(int dividend, int divisor, int *whole, int *micro_frac) { int rem; int exp = 0; *micro_frac = 0; if (divisor == 0) { *whole = 0; return; } *whole = dividend/divisor; rem = dividend % divisor; if (rem) { while (rem <= divisor) { rem *= 10; exp++; } *micro_frac = (rem / divisor) * pow_10(6-exp); } }
/* VTF format uses exponent and variable size format. For example if the size is 2 bytes 0x0067 with VTF16E14 format -> +1.03 To convert just change to 0x67 to decimal and use two decimal as E14 stands for 10^-2. Negative numbers are 2's complement */ static void convert_from_vtf_format(u32 value, int size, int exp, int *val1, int *val2) { int sign = 1; if (value & BIT(size*8 - 1)) { value = ((1LL << (size * 8)) - value); sign = -1; } exp = hid_sensor_convert_exponent(exp); if (exp >= 0) { *val1 = sign * value * pow_10(exp); *val2 = 0; } else { split_micro_fraction(value, -exp, val1, val2); if (*val1) *val1 = sign * (*val1); else *val2 = sign * (*val2); } }
/* * This function return the fraction part of a double * and set in ip the integral part. * In many ways it resemble the modf() found on most Un*x */ static double integral(double real, double *ip) { int j; double i, s, p; double real_integral = 0.; /* take care of the obvious */ /* equal to zero ? */ if (real == 0.) { *ip = 0.; return (0.); } /* negative number ? */ if (real < 0.) real = -real; /* a fraction ? */ if ( real < 1.) { *ip = 0.; return real; } /* the real work :-) */ for (j = log_10(real); j >= 0; j--) { p = pow_10(j); s = (real - real_integral)/p; i = 0.; while (i + 1. <= s) i++; real_integral += i*p; } *ip = real_integral; return (real - real_integral); }
/* * This fuction applies the unit exponent to the scale. * For example: * 9.806650000 ->exp:2-> val0[980]val1[665000000] * 9.000806000 ->exp:2-> val0[900]val1[80600000] * 0.174535293 ->exp:2-> val0[17]val1[453529300] * 1.001745329 ->exp:0-> val0[1]val1[1745329] * 1.001745329 ->exp:2-> val0[100]val1[174532900] * 1.001745329 ->exp:4-> val0[10017]val1[453290000] * 9.806650000 ->exp:-2-> val0[0]val1[98066500] */ static void adjust_exponent_nano(int *val0, int *val1, int scale0, int scale1, int exp) { int i; int x; int res; int rem; if (exp > 0) { *val0 = scale0 * pow_10(exp); res = 0; if (exp > 9) { *val1 = 0; return; } for (i = 0; i < exp; ++i) { x = scale1 / pow_10(8 - i); res += (pow_10(exp - 1 - i) * x); scale1 = scale1 % pow_10(8 - i); } *val0 += res; *val1 = scale1 * pow_10(exp); } else if (exp < 0) { exp = abs(exp); if (exp > 9) { *val0 = *val1 = 0; return; } *val0 = scale0 / pow_10(exp); rem = scale0 % pow_10(exp); res = 0; for (i = 0; i < (9 - exp); ++i) { x = scale1 / pow_10(8 - i); res += (pow_10(8 - exp - i) * x); scale1 = scale1 % pow_10(8 - i); } *val1 = rem * pow_10(9 - exp) + res; } else { *val0 = scale0; *val1 = scale1; } }
static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2) { *val1 = no/pow_10(exp); *val2 = no%pow_10(exp) * pow_10(6-exp); }
/* %e %E %g exponent representation */ static void exponent(struct DATA *p, double d) { CWT_CHAR *tmp, *tmp2; int j, i; DEF_PREC(p); j = log_10(d); d = d / pow_10(j); /* get the Mantissa */ d = ROUND(d, p); tmp = dtoa(d, p->precision, &tmp2); /* 1 for unit, 1 for the '.', 1 for 'e|E', * 1 for '+|-', 3 for 'exp' */ /* calculate how much padding need */ p->width = p->width - ((d > 0. && p->justify == RIGHT) ? 1:0) - ((p->space == FOUND) ? 1:0) - p->precision - 7; PAD_RIGHT(p); PUT_PLUS(d, p); PUT_SPACE(d, p); while (*tmp) {/* the integral */ PUT_CHAR(*tmp, p); tmp++; } if (p->precision != 0 || p->square == FOUND) PUT_CHAR(_T('.'), p); /* the '.' */ if (*p->pf == _T('g') || *p->pf == _T('G')) /* smash the trailing zeros */ for (i = ((int)cwt_str_ns()->strLen(tmp2)) - 1; i >= 0 && tmp2[i] == _T('0'); i--) tmp2[i] = _T('\0'); for (; *tmp2; tmp2++) PUT_CHAR(*tmp2, p); /* the fraction */ if (*p->pf == _T('g') || *p->pf == _T('e')) { /* the exponent put the 'e|E' */ PUT_CHAR(_T('e'), p); } else { PUT_CHAR(_T('E'), p); } if (j > 0) { /* the sign of the exp */ PUT_CHAR(_T('+'), p); } else { PUT_CHAR(_T('-'), p); j = -j; } tmp = itoa((double)j); if (j < 9) { /* need to pad the exponent with 0 '000' */ PUT_CHAR(_T('0'), p); PUT_CHAR(_T('0'), p); } else if (j < 99) { PUT_CHAR(_T('0'), p); } while (*tmp) { /* the exponent */ PUT_CHAR(*tmp, p); tmp++; } PAD_LEFT(p); }