зеркало из https://github.com/github/ruby.git
251 строка
5.5 KiB
C
251 строка
5.5 KiB
C
|
/*
|
|||
|
* PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION
|
|||
|
* --------------------------------------------------------------------
|
|||
|
* This file is written by the Project C Library Group, and completely
|
|||
|
* in public domain. You can freely use, copy, modify, and redistribute
|
|||
|
* the whole contents, without this notice.
|
|||
|
* --------------------------------------------------------------------
|
|||
|
* $Id$
|
|||
|
*/
|
|||
|
|
|||
|
/* System headers */
|
|||
|
#include <stdlib.h>
|
|||
|
#include <sys/xstdlib.h>
|
|||
|
|
|||
|
/*
|
|||
|
** <EFBFBD>ܴؿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ư<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD><EFBFBD><EFBFBD>ʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˤ<EFBFBD><EFBFBD>뤿<EFBFBD>ᡢ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ū<EFBFBD>ˤ<EFBFBD>
|
|||
|
** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>Ǽ<EFBFBD>Ǥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ޤǤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȥ<EFBFBD><EFBFBD>Ǥ<EFBFBD><EFBFBD>ʤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>äƺǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
** <EFBFBD><EFBFBD>18<EFBFBD><EFBFBD><EFBFBD>Ǥ<EFBFBD><EFBFBD>롣
|
|||
|
*/
|
|||
|
|
|||
|
/* File scope variables */
|
|||
|
static double _pos1[32] = {
|
|||
|
1.0e+17, /* + 0 */
|
|||
|
1.0e+18, /* + 1 */
|
|||
|
1.0e+19, /* + 2 */
|
|||
|
1.0e+20, /* + 3 */
|
|||
|
1.0e+21, /* + 4 */
|
|||
|
1.0e+22, /* + 5 */
|
|||
|
1.0e+23, /* + 6 */
|
|||
|
1.0e+24, /* + 7 */
|
|||
|
1.0e+25, /* + 8 */
|
|||
|
1.0e+26, /* + 9 */
|
|||
|
1.0e+27, /* +10 */
|
|||
|
1.0e+28, /* +11 */
|
|||
|
1.0e+29, /* +12 */
|
|||
|
1.0e+30, /* +13 */
|
|||
|
1.0e+31, /* +14 */
|
|||
|
1.0e+32, /* +15 */
|
|||
|
1.0e+33, /* +16 */
|
|||
|
1.0e+34, /* +17 */
|
|||
|
1.0e+35, /* +18 */
|
|||
|
1.0e+36, /* +19 */
|
|||
|
1.0e+37, /* +20 */
|
|||
|
1.0e+38, /* +21 */
|
|||
|
1.0e+39, /* +22 */
|
|||
|
1.0e+40, /* +23 */
|
|||
|
1.0e+41, /* +24 */
|
|||
|
1.0e+42, /* +25 */
|
|||
|
1.0e+43, /* +26 */
|
|||
|
1.0e+44, /* +27 */
|
|||
|
1.0e+45, /* +28 */
|
|||
|
1.0e+46, /* +29 */
|
|||
|
1.0e+47, /* +30 */
|
|||
|
1.0e+48, /* +31 */
|
|||
|
};
|
|||
|
|
|||
|
/* File scope variables */
|
|||
|
static double _neg1[32] = {
|
|||
|
1.0e+17, /* - 0 */
|
|||
|
1.0e+16, /* - 1 */
|
|||
|
1.0e+15, /* - 2 */
|
|||
|
1.0e+14, /* - 3 */
|
|||
|
1.0e+13, /* - 4 */
|
|||
|
1.0e+12, /* - 5 */
|
|||
|
1.0e+11, /* - 6 */
|
|||
|
1.0e+10, /* - 7 */
|
|||
|
1.0e+9, /* - 8 */
|
|||
|
1.0e+8, /* - 9 */
|
|||
|
1.0e+7, /* -10 */
|
|||
|
1.0e+6, /* -11 */
|
|||
|
1.0e+5, /* -12 */
|
|||
|
1.0e+4, /* -13 */
|
|||
|
1.0e+3, /* -14 */
|
|||
|
1.0e+2, /* -15 */
|
|||
|
1.0e+1, /* -16 */
|
|||
|
1.0e+0, /* -17 */
|
|||
|
1.0e-1, /* -18 */
|
|||
|
1.0e-2, /* -19 */
|
|||
|
1.0e-3, /* -20 */
|
|||
|
1.0e-4, /* -21 */
|
|||
|
1.0e-5, /* -22 */
|
|||
|
1.0e-6, /* -23 */
|
|||
|
1.0e-7, /* -24 */
|
|||
|
1.0e-8, /* -25 */
|
|||
|
1.0e-9, /* -26 */
|
|||
|
1.0e-10, /* -27 */
|
|||
|
1.0e-11, /* -28 */
|
|||
|
1.0e-12, /* -29 */
|
|||
|
1.0e-13, /* -30 */
|
|||
|
1.0e-14, /* -31 */
|
|||
|
};
|
|||
|
|
|||
|
/* File scope variables */
|
|||
|
static double _pos2[10] = {
|
|||
|
1.0e+0, /* 000 */
|
|||
|
1.0e+32, /* 001 */
|
|||
|
1.0e+64, /* 010 */
|
|||
|
1.0e+96, /* 011 */
|
|||
|
1.0e+128, /* 100 */
|
|||
|
1.0e+160, /* 101 */
|
|||
|
1.0e+192, /* 110 */
|
|||
|
1.0e+224, /* 111 */
|
|||
|
1.0e+256, /* 1000 */
|
|||
|
1.0e+288, /* 1001 */
|
|||
|
};
|
|||
|
|
|||
|
/* File scope variables */
|
|||
|
static double _neg2[10] = {
|
|||
|
1.0e-0, /* 000 */
|
|||
|
1.0e-32, /* 001 */
|
|||
|
1.0e-64, /* 010 */
|
|||
|
1.0e-96, /* 011 */
|
|||
|
1.0e-128, /* 100 */
|
|||
|
1.0e-160, /* 101 */
|
|||
|
1.0e-192, /* 110 */
|
|||
|
1.0e-224, /* 111 */
|
|||
|
1.0e-256, /* 1000 */
|
|||
|
1.0e-288, /* 1001 */
|
|||
|
};
|
|||
|
|
|||
|
/* File scope functions */
|
|||
|
static int _cmpd (double x, double y)
|
|||
|
{
|
|||
|
unsigned long vx, vy, rc;
|
|||
|
unsigned long *x_ptr = (unsigned long *) &x;
|
|||
|
unsigned long *y_ptr = (unsigned long *) &y;
|
|||
|
|
|||
|
/* x<>λؿ<CEBB><D8BF>ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vx = x_ptr[0] & 0x7FF00000;
|
|||
|
|
|||
|
/* y<>λؿ<CEBB><D8BF>ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vy = y_ptr[0] & 0x7FF00000;
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF>ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD>Ǥ<EFBFBD><C7A4><EFBFBD> */
|
|||
|
if ((rc = vy - vx) != 0)
|
|||
|
return rc;
|
|||
|
|
|||
|
/* x<><78>ͭ<EFBFBD><CDAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ξ<EFBFBD><CEBE>̥ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vx = x_ptr[0] & 0x000FFFFF;
|
|||
|
|
|||
|
/* y<><79>ͭ<EFBFBD><CDAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ξ<EFBFBD><CEBE>̥ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vy = y_ptr[0] & 0x000FFFFF;
|
|||
|
|
|||
|
/* <20><><EFBFBD>̥ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD>Ǥ<EFBFBD><C7A4><EFBFBD> */
|
|||
|
if ((rc = vy - vx) != 0)
|
|||
|
return rc;
|
|||
|
|
|||
|
/* x<><78>ͭ<EFBFBD><CDAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>̥ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vx = x_ptr[1];
|
|||
|
|
|||
|
/* y<><79>ͭ<EFBFBD><CDAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>̥ӥåȤ<C3A5><C8A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ф<EFBFBD> */
|
|||
|
vy = y_ptr[1];
|
|||
|
|
|||
|
/* <20>ǽ<EFBFBD>Ƚ<EFBFBD><C8BD> */
|
|||
|
return vy - vx;
|
|||
|
}
|
|||
|
|
|||
|
/* Functions */
|
|||
|
void _dtos18 (double x, int *decpt, int *sign, char *buffer)
|
|||
|
{
|
|||
|
short e2;
|
|||
|
int e, n;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>2<EFBFBD>λؿ<CEBB><D8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>Х<EFBFBD><D0A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʤ<EFBFBD><CAA4>ξ<EFBFBD><CEBE><EFBFBD>) */
|
|||
|
e2 = (((unsigned short *) &x)[0] & 0x7FF0U) >> 4;
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF><EFBFBD>0<EFBFBD>ξ<EFBFBD><CEBE><EFBFBD><EFBFBD>ϡ<EFBFBD>0.0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>å<EFBFBD> */
|
|||
|
if (e2 == 0) {
|
|||
|
|
|||
|
unsigned long hi = ((unsigned long *) &x)[0] & 0xFFFFF;
|
|||
|
unsigned long lo = ((unsigned long *) &x)[1];
|
|||
|
|
|||
|
/* ͭ<><CDAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD>ɤ<EFBFBD><C9A4><EFBFBD> */
|
|||
|
if (hi == 0 && lo == 0) {
|
|||
|
|
|||
|
/* ʸ<><CAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
buffer[0] = '0';
|
|||
|
|
|||
|
/* NUL<55><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
buffer[1] = '\0';
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD> */
|
|||
|
*decpt = 1;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
/* *sign = hi & 0x80000000UL; */
|
|||
|
*sign = 0;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD> */
|
|||
|
return;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/* 2<>λؿ<CEBB><D8BF>˥Х<CBA5><D0A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2A4ABA4>Ƥ<EFBFBD><C6A4><EFBFBD>10<31>λؿ<CEBB><D8BF><EFBFBD> (approx. log10(2)) */
|
|||
|
e = ((int) ((e2 - 1023) * 77)) >> 8;
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ξ<EFBFBD><CEBE><EFBFBD> */
|
|||
|
if (e >= 0) {
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF><EFBFBD>32<33><32><EFBFBD>꾮<EFBFBD><EABEAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϥơ<CFA5><C6A1>֥<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD> */
|
|||
|
if (e < 32)
|
|||
|
x *= _neg1[e];
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF><EFBFBD>32<33><32><EFBFBD><EFBFBD><EFBFBD>礭<EFBFBD><E7A4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϥơ<CFA5><C6A1>֥<EFBFBD>1,2<><32><EFBFBD><EFBFBD> */
|
|||
|
else
|
|||
|
x *= _neg1[e & 31] * _neg2[e >> 5];
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ؿ<EFBFBD><D8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ξ<EFBFBD><CEBE><EFBFBD> */
|
|||
|
else {
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ͤ<EFBFBD><CDA4><EFBFBD> */
|
|||
|
n = -e;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ͤ<EFBFBD>32<33><32><EFBFBD>꾮<EFBFBD><EABEAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϥơ<CFA5><C6A1>֥<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD> */
|
|||
|
if (n < 32)
|
|||
|
x *= _pos1[n];
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ͤ<EFBFBD>32<33><32><EFBFBD><EFBFBD><EFBFBD>礭<EFBFBD><E7A4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϥơ<CFA5><C6A1>֥<EFBFBD>1,2<><32><EFBFBD><EFBFBD> */
|
|||
|
else {
|
|||
|
x *= _pos1[n & 31];
|
|||
|
x *= _pos2[n >> 5];
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A5B0A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>᤹ */
|
|||
|
if (_cmpd (1.0e+18, x) >= 0) {
|
|||
|
e++;
|
|||
|
x *= 1.0e-1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><C2AD><EFBFBD>ʤ<EFBFBD><CAA4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD> */
|
|||
|
else if (_cmpd (1.0e+17, x) < 0) {
|
|||
|
e--;
|
|||
|
x *= 1.0e+1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD> */
|
|||
|
*decpt = e + 1;
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
*sign = ((unsigned char *) &x)[0] & 0x80U;
|
|||
|
|
|||
|
/* ʸ<><CAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD> */
|
|||
|
_ulltoa ((unsigned long long) x, buffer);
|
|||
|
}
|