* eina: Add eina_atofp.

SVN revision: 42694
This commit is contained in:
Cedric BAIL 2009-09-25 10:03:19 +00:00
parent aea9bb4064
commit 6a284f8178
3 changed files with 44 additions and 22 deletions

View File

@ -54,9 +54,12 @@ EAPI extern Eina_Error EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH;
EAPI int eina_convert_itoa(int n, char *s) EINA_ARG_NONNULL(2);
EAPI int eina_convert_xtoa(unsigned int n, char *s) EINA_ARG_NONNULL(2);
EAPI int eina_convert_dtoa(double d, char *des) EINA_ARG_NONNULL(2);
EAPI Eina_Bool eina_convert_atod(const char *src, int length, long long *m, long *e) EINA_ARG_NONNULL(1,3,4);
EAPI int eina_convert_fptoa(Eina_F32p32 fp, char *des) EINA_ARG_NONNULL(2);
EAPI Eina_Bool eina_convert_atofp(const char *src, int length, Eina_F32p32 *fp) EINA_ARG_NONNULL(1,3);
/**
* @}

View File

@ -72,28 +72,6 @@ static inline void reverse(char s[], int length)
}
}
static inline Eina_F32p32 eina_f32p32_mul2(Eina_F32p32 fp)
{
int64_t low;
int64_t high;
low = (fp & 0x00000000ffffffffLL) << 1;
high = (fp >> 32) << 33;
return low + high;
}
static inline Eina_F32p32 eina_f32p32_mul16(Eina_F32p32 fp)
{
int64_t low;
int64_t high;
low = (fp & 0x00000000ffffffffLL) << 4;
high = (fp >> 32) << 36;
return low + high;
}
/**
* @endcond
*/
@ -707,6 +685,26 @@ eina_convert_fptoa(Eina_F32p32 fp, char *des)
return length + eina_convert_itoa(p, des);
}
EAPI Eina_Bool
eina_convert_atofp(const char *src, int length, Eina_F32p32 *fp)
{
long long m;
long e;
if (!eina_convert_atod(src, length, &m, &e))
return EINA_FALSE;
if (fp)
{
e += 32;
if (e > 0) *fp = m << e;
else *fp = m >> e;
}
return EINA_TRUE;
}
/**
* @}
*/

View File

@ -105,15 +105,27 @@ _eina_convert_fp_check(double d, Eina_F32p32 fp, int length)
{
char tmp1[128];
char tmp2[128];
Eina_F32p32 fpc;
double fpd;
int l1;
int l2;
l1 = eina_convert_dtoa(d, tmp1);
l2 = eina_convert_fptoa(fp, tmp2);
/* fprintf(stderr, "[%s](%i) vs [%s](%i)\n", tmp1, l1, tmp2, l2); */
fail_if(l1 != l2);
fail_if(length != l1);
fail_if(strcmp(tmp1, tmp2) != 0);
fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
/* fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
fail_if(fpc != fp);
fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
fpd = eina_f32p32_double_to(fpc);
/* fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
fail_if(fabs(fpd - d) > DBL_MIN);
d = -d;
fp = -fp;
@ -122,6 +134,15 @@ _eina_convert_fp_check(double d, Eina_F32p32 fp, int length)
fail_if(l1 != l2);
fail_if(length + 1 != l1);
fail_if(strcmp(tmp1, tmp2) != 0);
fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
/* fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
fail_if(fpc != fp);
fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
fpd = eina_f32p32_double_to(fpc);
/* fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
fail_if(fabs(fpd - d) > DBL_MIN);
}
START_TEST(eina_convert_fp)