~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/c/big_ll.d

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
    big_ll.c -- Bignum emulation with long long.
 
3
 */
 
4
/*
 
5
    Copyright (c) 2005, Maciek Pasternacki.
 
6
 
 
7
    ECL is free software; you can redistribute it and/or
 
8
    modify it under the terms of the GNU Library General Public
 
9
    License as published by the Free Software Foundation; either
 
10
    version 2 of the License, or (at your option) any later version.
 
11
 
 
12
    See file '../Copyright' for full details.
 
13
*/
 
14
 
 
15
#include <ecl/ecl.h>
 
16
#include <ecl/internal.h>
 
17
 
 
18
cl_object
 
19
big_register0_get(void)
 
20
{
 
21
        cl_env.big_register[0]->big.big_num = 0ll;
 
22
        return cl_env.big_register[0];
 
23
}
 
24
 
 
25
cl_object
 
26
big_register1_get(void)
 
27
{
 
28
        cl_env.big_register[1]->big.big_num = 0ll;
 
29
        return cl_env.big_register[1];
 
30
}
 
31
 
 
32
cl_object
 
33
big_register2_get(void)
 
34
{
 
35
        cl_env.big_register[2]->big.big_num = 0ll;
 
36
        return cl_env.big_register[2];
 
37
}
 
38
 
 
39
void
 
40
big_register_free(cl_object x) {}
 
41
 
 
42
cl_object
 
43
big_register_copy(cl_object old)
 
44
{
 
45
        cl_object new_big = cl_alloc_object(t_bignum);
 
46
        new_big->big.big_num = old->big.big_num;
 
47
        return new_big;
 
48
}
 
49
 
 
50
cl_object
 
51
big_register_normalize(cl_object x)
 
52
{
 
53
        if (x->big.big_num == 0ll)
 
54
                return(MAKE_FIXNUM(0));
 
55
        if (x->big.big_num <= MOST_POSITIVE_FIXNUM && x->big.big_num >= MOST_NEGATIVE_FIXNUM)
 
56
                return(MAKE_FIXNUM(x->big.big_num));
 
57
        return big_register_copy(x);
 
58
}
 
59
 
 
60
static cl_object
 
61
big_alloc(int size)
 
62
{
 
63
  volatile cl_object x = cl_alloc_object(t_bignum);
 
64
  if (size <= 0)
 
65
    error("negative or zero size for bignum in big_alloc");
 
66
  x->big.big_num = 0ll;
 
67
  return x;
 
68
}
 
69
 
 
70
 
 
71
cl_object
 
72
bignum1(cl_fixnum val)
 
73
{
 
74
  volatile cl_object z = cl_alloc_object(t_bignum);
 
75
  z->big.big_num = val;
 
76
  return(z);
 
77
}
 
78
 
 
79
cl_object
 
80
bignum2(cl_fixnum hi, cl_fixnum lo)
 
81
{
 
82
  cl_object z;
 
83
 
 
84
  z = big_alloc(2);
 
85
  z->big.big_num = hi<<32 + lo;
 
86
  return(z);
 
87
}
 
88
 
 
89
cl_object
 
90
big_copy(cl_object x)
 
91
{
 
92
        volatile cl_object y = cl_alloc_object(t_bignum);
 
93
        y->big.big_num = x->big.big_num;
 
94
        return(y);
 
95
}
 
96
 
 
97
/*
 
98
        big_minus(x) returns the complement of bignum x.
 
99
*/
 
100
cl_object
 
101
big_minus(cl_object x)
 
102
{
 
103
        volatile cl_object y = big_copy(x);
 
104
        y->big.big_num = -x->big.big_num;
 
105
        return y;
 
106
}
 
107
 
 
108
cl_object
 
109
big_plus(cl_object x, cl_object y)
 
110
{
 
111
        volatile cl_object z = big_register0_get();
 
112
        z->big.big_num = x->big.big_num + y->big.big_num;
 
113
        return(big_register_copy(z));
 
114
}
 
115
 
 
116
cl_object
 
117
big_normalize(cl_object x)
 
118
{
 
119
  if (x->big.big_num == 0ll)
 
120
    return(MAKE_FIXNUM(0));
 
121
  if (x->big.big_num <= MOST_POSITIVE_FIXNUM && x->big.big_num >= MOST_NEGATIVE_FIXNUM)
 
122
    return(MAKE_FIXNUM(x->big.big_num));
 
123
  return(x);
 
124
}
 
125
 
 
126
int big_num_t_sgn(big_num_t x)
 
127
{
 
128
     return ( x == (big_num_t)0 ) ? 0 : (x < (big_num_t)0) ? -1 : 1;
 
129
}
 
130
 
 
131
 
 
132
void init_big_registers(void)
 
133
{
 
134
        int i;
 
135
        for (i = 0; i < 3; i++) {
 
136
                cl_env.big_register[i] = cl_alloc_object(t_bignum);
 
137
                cl_env.big_register[i]->big.big_num = 0ll;
 
138
        }
 
139
}
 
140
 
 
141
void
 
142
init_big(void)
 
143
{
 
144
        init_big_registers();
 
145
}