ごとけんです

In message "[ruby-dev:4337] Re: pack("F") for bigendian float"
    on 99/01/25, GOTO Kentaro <gotoken / math.sci.hokudai.ac.jp> writes:

>                    つまり,前の D,F は放棄して,
>
>  e  リトルエンディアンの単精度浮動小数点数(機種依存)
>  E  リトルエンディアンの倍精度浮動小数点数(機種依存)
>  g  ビッグエンディアンの単精度浮動小数点数(機種依存)
>  G  ビッグエンディアンの倍精度浮動小数点数(機種依存)

これを実装しました.とりこんでもらえますか??

-- gotoken

--- ruby-1.3-orig/pack.c Wed Dec 23 01:22:26 1998 +++ ruby-1.3/pack.c Tue Jan 26 04:40:40 1999 @@ -21,0 +22,18 @@ +#define swapf(x) swapl(x) + +static double +swapd(d) +const double d; +{ + double dtmp = d; + unsigned long utmp[2]; + unsigned long utmp0; + + utmp[0] = 0; utmp[1] = 0; + memcpy(utmp,&dtmp,sizeof(double)); + utmp0 = utmp[0]; + utmp[0] = swapl(utmp[1]); + utmp[1] = swapl(utmp0); + memcpy(&dtmp,utmp,sizeof(double)); + return dtmp; +} @@ -44,0 +63,2 @@ +#define ntohf(x) (endian()?(x):swapf(x)) +#define ntohd(x) (endian()?(x):swapd(x)) @@ -46,0 +67,2 @@ +#define htonf(x) (endian()?(x):swapf(x)) +#define htond(x) (endian()?(x):swapd(x)) @@ -48,0 +71,2 @@ +#define htovf(x) (endian()?swapf(x):(x)) +#define htovd(x) (endian()?swapd(x):(x)) @@ -50,0 +75,2 @@ +#define vtohf(x) (endian()?swapf(x):(x)) +#define vtohd(x) (endian()?swapd(x):(x)) @@ -58,0 +85,4 @@ +#define ntohf(x) (x) +#define ntohd(x) (x) +#define htonf(x) (x) +#define htond(x) (x) @@ -60,0 +91,2 @@ +#define htovf(x) swapf(x) +#define htovd(x) swapd(x) @@ -62,0 +95,2 @@ +#define vtohf(x) swapf(x) +#define vtohd(x) swapd(x) @@ -69,0 +104,4 @@ +#define ntohf(x) swapf(x) +#define ntohd(x) swapd(x) +#define htonf(x) swapf(x) +#define htond(x) swapd(x) @@ -70,0 +109,2 @@ +#define htovf(x) (x) +#define htovd(x) (x) @@ -73,0 +114,2 @@ +#define vtohf(x) (x) +#define vtohd(x) (x) @@ -401,0 +444,43 @@ + case 'e': + while (len-- > 0) { + float f; + unsigned long ultemp; + + from = NEXTFROM; + switch (TYPE(from)) { + case T_FLOAT: + f = RFLOAT(from)->value; + break; + case T_STRING: + f = atof(RSTRING(from)->ptr); + default: + f = (float)NUM2INT(from); + break; + } + memcpy(&ultemp,&f,sizeof(float)); + ultemp = htovf((unsigned long)ultemp); + memcpy(&f,&ultemp,sizeof(float)); + rb_str_cat(res, (char*)&f, sizeof(float)); + } + break; + + case 'E': + while (len-- > 0) { + double d; + + from = NEXTFROM; + switch (TYPE(from)) { + case T_FLOAT: + d = RFLOAT(from)->value; + break; + case T_STRING: + d = atof(RSTRING(from)->ptr); + default: + d = (double)NUM2INT(from); + break; + } + d = htovd(d); + rb_str_cat(res, (char*)&d, sizeof(double)); + } + break; + @@ -421,0 +507,43 @@ + case 'g': + while (len-- > 0) { + float f; + unsigned long ultemp; + + from = NEXTFROM; + switch (TYPE(from)) { + case T_FLOAT: + f = RFLOAT(from)->value; + break; + case T_STRING: + f = atof(RSTRING(from)->ptr); + default: + f = (float)NUM2INT(from); + break; + } + memcpy(&ultemp,&f,sizeof(float)); + ultemp = htonf((unsigned long)ultemp); + memcpy(&f,&ultemp,sizeof(float)); + rb_str_cat(res, (char*)&f, sizeof(float)); + } + break; + + case 'G': + while (len-- > 0) { + double d; + + from = NEXTFROM; + switch (TYPE(from)) { + case T_FLOAT: + d = RFLOAT(from)->value; + break; + case T_STRING: + d = atof(RSTRING(from)->ptr); + default: + d = (double)NUM2INT(from); + break; + } + d = htond(d); + rb_str_cat(res, (char*)&d, sizeof(double)); + } + break; + @@ -914,0 +1043,56 @@ + case 'e': + if (len >= (send - s) / sizeof(float)) + len = (send - s) / sizeof(float); + while (len-- > 0) { + float tmp; + unsigned long ultemp; + + memcpy(&tmp, s, sizeof(float)); + memcpy(&ultemp,&tmp,sizeof(float)); + ultemp = vtohf((unsigned long)ultemp); + memcpy(&tmp,&ultemp,sizeof(float)); + s += sizeof(float); + rb_ary_push(ary, rb_float_new((double)tmp)); + } + break; + + case 'E': + if (len >= (send - s) / sizeof(double)) + len = (send - s) / sizeof(double); + while (len-- > 0) { + double tmp; + memcpy(&tmp, s, sizeof(double)); + tmp = vtohd(tmp); + s += sizeof(double); + rb_ary_push(ary, rb_float_new(tmp)); + } + break; + + case 'g': + if (len >= (send - s) / sizeof(float)) + len = (send - s) / sizeof(float); + while (len-- > 0) { + float tmp; + unsigned long ultemp; + + memcpy(&tmp, s, sizeof(float)); + memcpy(&ultemp,&tmp,sizeof(float)); + ultemp = ntohf((unsigned long)ultemp); + memcpy(&tmp,&ultemp,sizeof(float)); + s += sizeof(float); + rb_ary_push(ary, rb_float_new((double)tmp)); + } + break; + + case 'G': + if (len >= (send - s) / sizeof(double)) + len = (send - s) / sizeof(double); + while (len-- > 0) { + double tmp; + memcpy(&tmp, s, sizeof(double)); + tmp = ntohd(tmp); + s += sizeof(double); + rb_ary_push(ary, rb_float_new(tmp)); + } + break; +