ごとけんです

pack/unpack のテンプレート文字に対して以下のような拡張を
考えました。

  F   bigendian の単精度浮動小数点数(機種依存)
  D   bigendian の倍精度浮動小数点数(機種依存)

効率はちょっと悪いんですが、今度はちゃんと動くみたいです。
[ruby-dev:4327] は破棄します。Ruby-1.3 にあてるものです。

-- gotoken

--- ruby-1.3/pack.c.orig Wed Dec 23 01:22:26 1998 +++ ruby-1.3/pack.c Sat Jan 23 17:14:02 1999 @@ -6 +6 @@ - $Date: 1998/12/16 07:30:32 $ + $Date: 1998/12/25 04:35:41 $ @@ -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; +} @@ -50,0 +69,4 @@ +#define ntohf(x) (endian()?(x):swapl(x)) +#define htonf(x) (endian()?swapl(x):(x)) +#define ntohd(x) (endian()?(x):swapd(x)) +#define htond(x) (endian()?swapd(x):(x)) @@ -58,0 +81,4 @@ +#define ntohf(x) (x) +#define htonf(x) (x) +#define ntohd(x) (x) +#define htond(x) (x) @@ -70 +96,5 @@ -#define htovs(x) (x) +#define ntohf(x) swapl(x) +#define htonf(x) swapf(x) +#define ntohd(x) swapd(x) +#define htond(x) swapd(x) +#define htovs(x) swapd(x) @@ -382,0 +413,18 @@ + while (len-- > 0) { + float f; + + 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; + } + rb_str_cat(res, (char*)&f, sizeof(float)); + } + break; + @@ -385,0 +434 @@ + unsigned long ultemp; @@ -397,0 +447,3 @@ + memcpy(&ultemp,&f,sizeof(float)); + ultemp = htonf((unsigned long)ultemp); + memcpy(&f,&ultemp,sizeof(float)); @@ -402 +453,0 @@ - case 'd': @@ -417,0 +469,20 @@ + d = htond(d); + rb_str_cat(res, (char*)&d, sizeof(double)); + } + break; + + case 'd': + 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; + } @@ -904 +974,0 @@ - case 'F': @@ -914,0 +985,16 @@ + case 'F': + 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; + @@ -915,0 +1002,11 @@ + 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; +