1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*-----------------------------------------------------------------*-C-*---
* File: handc/runtime/basemath.c
*
* Copyright (C)1997 Donovan Kolbly <d.kolbly@rscheme.org>
* as part of the RScheme project, licensed for free use.
* See <http://www.rscheme.org/> for the latest information.
*
* File version: 1.7
* File mod date: 1997.11.29 23:10:50
* System build: v0.7.2, 97.12.21
*
* Purpose: Arithmetic for basic (built-in) number system
*------------------------------------------------------------------------*/
#include <rscheme/scheme.h>
#include <rscheme/smemory.h>
#if !FULL_NUMERIC_TOWER
void init_math( void )
{
}
#include "numsimpl.ci"
#else
#include <gmp.h>
typedef UINT_32 UWtype; /* define types that longlong.h needs */
typedef UINT_16 UHWtype;
typedef INT_32 SItype;
typedef UINT_32 USItype;
#define W_TYPE_SIZE (32)
#include <longlong.h> /* from the GMP distribution */
/***********************************************************************
Note: There is a bug in longlong.h from the 2.0.2 version of GMP.
Consider applying the following patch:
*** longlong.h Sat Oct 25 22:47:20 1997
--- /tmp/gmp-2.0.2.orig/longlong.h Fri May 24 01:34:24 1996
***************
*** 1295 ****
! #if !defined (smul_ppmm)
--- 1295 ----
! #if !defined (umul_ppmm)
***********************************************************************/
#define BIGNUM_P(x) OBJ_ISA_PTR_OF_CLASS(x,bignum_class)
#define RATIONAL_P(x) OBJ_ISA_PTR_OF_CLASS(x,mp_rational_class)
#define COMPLEX_P(x) OBJ_ISA_PTR_OF_CLASS(x,rect_complex_class)
static void *mp_alloc( size_t n )
{
return PTR_TO_DATAPTR(alloc( n, mp_data_class ));
}
static void *mp_realloc( void *old_data, size_t old_size, size_t new_size )
{
void *new_data = PTR_TO_DATAPTR(alloc( new_size, mp_data_class ));
memcpy( new_data, old_data, old_size );
return new_data;
}
static void mp_free( void *ptr, size_t n )
{
/* do nothing -- data will get GC'd */
}
void init_math( void )
{
mp_set_memory_functions( mp_alloc, mp_realloc, mp_free );
}
#define DATAPTR_TO_PTR(ptr) OBJ(POINTER_TAG+(UINT_32)ptr)
static obj mpz_to_bignum( mpz_t n )
{
return make3( bignum_class,
int2fx( n[0]._mp_alloc ),
int2fx( n[0]._mp_size ),
DATAPTR_TO_PTR( n[0]._mp_d ) );
}
static obj fx_to_bignum( obj x )
{
mpz_t n;
mpz_init_set_si( n, fx2int(x) );
return mpz_to_bignum( n );
}
#define longint_to_float(l) int_64_to_float(extract_int_64(l))
#define longint_to_rational(l) bignum_to_rational(longint_to_bignum(l))
static obj longint_to_bignum( obj lng )
{
return fx_to_bignum(ZERO);
}
static obj bignum_to_rational( obj b )
{
return make2( mp_rational_class, b, fx_to_bignum( int2fx(1) ) );
}
static obj fx_to_rational( obj fx )
{
return bignum_to_rational( fx_to_bignum(fx) );
}
#define STUBBED_OUT(r) scheme_error( "basemath.c:~d: function stubbed out", 1, int2fx(__LINE__) ); return r
/************************ INT_32 OPERATIONS ************************/
#define HIGHBIT (1<<29)
static _rs_inline obj int_plus( INT_32 a, INT_32 b )
{
INT_32 c = a + b;
if ((~(a ^ b) & (c ^ a)) & HIGHBIT)
{
INT_64 a2 = int_32_to_int_64(a);
INT_64 b2 = int_32_to_int_64(b);
return int_64_compact( int_64_add( a2, b2 ) );
}
return int2fx( c );
}
static _rs_inline obj int_minus( INT_32 a, INT_32 b )
{
INT_32 c = a - b;
if ((~(a ^ b) & (c ^ a)) & HIGHBIT)
{
INT_64 a2 = int_32_to_int_64(a);
INT_64 b2 = int_32_to_int_64(b);
return int_64_compact( int_64_sub( a2, b2 ) );
}
return int2fx( a-b );
}
static _rs_inline obj int_mul( INT_32 a, INT_32 b )
{
INT_32 p_hi, p_lo;
INT_64 a2, b2;
smul_ppmm( p_hi, p_lo, a, b );
if (p_hi == 0)
{
if (p_lo < HIGHBIT)
{
return int2fx( p_lo );
}
}
else if (p_hi == -1)
{
if (p_lo >= -HIGHBIT)
{
return int2fx( p_lo );
}
}
a2 = int_32_to_int_64(a);
b2 = int_32_to_int_64(b);
return int_64_compact( int_64_mul( a2, b2 ) );
}
static _rs_inline obj int_div( INT_32 a, INT_32 b )
{
return make_float( (IEEE_64)a/(IEEE_64)b );
}
static _rs_inline int int_cmp( INT_32 a, INT_32 b )
{
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
/************************ INT_64 OPERATIONS ************************/
static _rs_inline obj long_plus( INT_64 a, INT_64 b )
{
return int_64_compact( int_64_add(a,b) );
}
static _rs_inline obj long_minus( INT_64 a, INT_64 b )
{
return int_64_compact( int_64_sub(a,b) );
}
static _rs_inline obj long_mul( INT_64 a, INT_64 b )
{
return int_64_compact( int_64_mul(a,b) );
}
static _rs_inline obj long_div( INT_64 a, INT_64 b )
{
return make_float( int_64_to_float(a) / int_64_to_float(b) );
}
#define long_cmp(a,b) int_64_cmp(a,b)
/************************ IEEE_64 OPERATIONS ************************/
static _rs_inline obj fl_plus( IEEE_64 a, IEEE_64 b )
{
return make_float( a + b );
}
static _rs_inline obj fl_minus( IEEE_64 a, IEEE_64 b )
{
return make_float( a - b );
}
static _rs_inline obj fl_mul( IEEE_64 a, IEEE_64 b )
{
return make_float( a * b );
}
static _rs_inline obj fl_div( IEEE_64 a, IEEE_64 b )
{
return make_float( a / b );
}
static _rs_inline int fl_cmp( IEEE_64 a, IEEE_64 b )
{
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
/************************ BIGNUM OPERATIONS ************************/
static _rs_inline obj bignum_plus( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj bignum_minus( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj bignum_mul( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj bignum_div( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static int bignum_cmp( obj a, obj b )
{
STUBBED_OUT(0);
}
/************************ RATIONAL OPERATIONS ************************/
static _rs_inline obj rational_plus( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj rational_minus( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj rational_mul( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static _rs_inline obj rational_div( obj a, obj b )
{
STUBBED_OUT(ZERO);
}
static int rational_cmp( obj a, obj b )
{
STUBBED_OUT(0);
}
/************************ COMPLEX OPERATIONS ************************/
typedef struct _cmplx {
obj re;
obj im;
} cmplx;
static obj make_complex( obj re, obj im )
{
return make2( rect_complex_class, re, im );
}
static _rs_inline cmplx real_to_complex( obj r )
{
cmplx c;
c.re = r;
c.im = ZERO;
return c;
}
static _rs_inline cmplx extract_complex( obj ch )
{
cmplx c;
c.re = gvec_ref(ch,SLOT(0));
c.im = gvec_ref(ch,SLOT(1));
return c;
}
#define RETURN_COMPLEX(re,im) if (EQ(im,ZERO)) return re; else return make_complex( re, im )
static _rs_inline obj complex_plus( cmplx a, cmplx b )
{
obj re = basic_plus( a.re, b.re );
obj im = basic_plus( a.im, b.im );
RETURN_COMPLEX(re,im);
}
static _rs_inline obj complex_minus( cmplx a, cmplx b )
{
obj re = basic_minus( a.re, b.re );
obj im = basic_minus( a.im, b.im );
RETURN_COMPLEX(re,im);
}
/* (a + ib) * (c + id) = (ac - bd) + (ad + cb)i */
static obj complex_mul( cmplx a_r, cmplx b_r )
{
obj a = a_r.re;
obj b = a_r.im;
obj c = b_r.re;
obj d = b_r.im;
obj re = basic_minus( basic_mul( a, c ), basic_mul( b, d ) );
obj im = basic_plus( basic_mul( a, d ), basic_mul( c, b ) );
RETURN_COMPLEX(re,im);
}
/* ??? */
static _rs_inline obj complex_div( cmplx a, cmplx b )
{
obj re = basic_div( a.re, b.re );
obj im = basic_div( a.im, b.im );
RETURN_COMPLEX(re,im);
}
static _rs_inline obj complex_abs( cmplx a )
{
return basic_plus( basic_mul( a.re, a.re ), basic_mul( a.im, a.im ) );
}
static int complex_cmp( cmplx a, cmplx b )
{
return basic_cmp( complex_abs(a), complex_abs(b) );
}
/********************************************************************/
#include "numtower.ci"
#endif /* FULL_NUMERIC_TOWER */
|