LargeInteger.st
author Claus Gittinger <cg@exept.de>
Sat, 06 Jun 2015 13:42:28 +0200
changeset 18449 48c31fe80359
parent 18381 7614c149c77e
child 18383 3a40da3624b7
child 18621 78b4a4b916a5
permissions -rw-r--r--
class: Timestamp changed: #readFrom:format:language:onError: fix in Y format reading
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17648
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
     1
"{ Encoding: utf8 }"
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
     4
 COPYRIGHT (c) 1994 by Claus Gittinger
345
claus
parents: 250
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5382
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
    14
"{ Package: 'stx:libbasic' }"
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
    15
17648
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
    16
"{ NameSpace: Smalltalk }"
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
Integer subclass:#LargeInteger
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    19
	instanceVariableNames:'sign digitByteArray'
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    20
	classVariableNames:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    21
	poolDictionaries:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    22
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
    25
!LargeInteger class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
 COPYRIGHT (c) 1994 by Claus Gittinger
345
claus
parents: 250
diff changeset
    30
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    41
documentation
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    42
"
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    43
    This class provides arbitrary precision integers.
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    44
    These are represented as:
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    45
      sign (-1/0/+1) and, if sign ~~ 0
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    46
      the absolute value in a ByteArray of digits with 8 bits per element;
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    47
      least significant 8 bits at index 1...
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    48
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
    49
    The implementation is not completely tuned for high performance
4264
9fd0385c26d0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4263
diff changeset
    50
    (more key-methods should be rewritten as primitives), although the
9fd0385c26d0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4263
diff changeset
    51
    common operations have been pretty tuned for some architectures.
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    52
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    53
    LargeIntegers are usually not created explicitely, but result from
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
    54
    SmallInteger arithmetic (overflowing the SmallInteger range).
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    55
    Also, results of LargeInteger operations are converted to back to
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
    56
    SmallIntegers, when possible (see #compressed).
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    57
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    58
    In contrast to ST-80, there is only one class for LargeIntegers,
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    59
    which keeps the sign as an instance variable
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    60
    (ST-80 has LargePositiveInteger and LargeNegativeInteger).
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    61
    Another possible change is to use 2's complement instead of sign-magnitude
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    62
    representation, and to use the underlying CPU's native byte order (instead of LSB).
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    63
    This would allow us to use modern CPU vector/longword operations, at least for 64 and
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    64
    128 bit numbers (which make up almost all instances in practice).
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    65
    As all of these are transparent to the outside world, any of it may or may not
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    66
    change in the future.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    67
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    68
    [author:]
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    69
	Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1504
diff changeset
    70
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1504
diff changeset
    71
    [see also:]
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    72
	Number
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    73
	Float Fraction FixedPoint
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    74
	SmallInteger
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    75
"
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    76
!
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    77
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    78
testing
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    79
"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    80
   test largeInt multiplication & division
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    81
   (at least, test if they have not both errors which anull each other ;-)
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    82
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    83
   |last v|
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    84
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    85
   v := last := 1.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    86
   2 to:3000 do:[:i |
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    87
       i printCR.
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    88
       v := v * i.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    89
       (v / i) ~= last ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
    90
	   self halt
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    91
       ].
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    92
       last := v.
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    93
   ]
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    94
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    95
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    96
   test addition, subtraction:
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    97
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    98
   SmallInteger maxVal                -> 1073741823
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    99
   SmallInteger maxVal + 1            -> 1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   100
   SmallInteger maxVal + 2            -> 1073741825
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   101
   (SmallInteger maxVal + 2) - 2      -> 1073741823
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   102
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   103
   SmallInteger minVal                -> -1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   104
   SmallInteger minVal - 1            -> -1073741825
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   105
   SmallInteger minVal - 2            -> -1073741826
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   106
   (SmallInteger minVal - 2) + 2      -> -1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   107
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   108
   1234567890 + 10                    -> 1234567900
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   109
   1111111111 + 1111111111            -> 2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   110
   1111111111 - 1111111111            -> 0
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   111
   1111111111 - 2222222222            -> -1111111111
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   112
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   113
   1111111111 * 2                     -> 2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   114
   1111111111 * -2                    -> -2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   115
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   116
   1111111111 * 1111111111            -> 1234567900987654321
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   117
   1234567900987654321 // 1111111111  -> 1111111111
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   118
   1234567900987654321 \\ 1111111111  -> 0
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   119
   1234567900987654322 \\ 1111111111  -> 1
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   120
   1234567900987654421 \\ 1111111111  -> 100
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   121
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   122
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   123
   addition:
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   124
   (16rFFFFFFF0 + 1          ) hexPrintString -> 'FFFFFFF1'
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   125
   (16rFFFFFFFF + 1          ) hexPrintString -> '100000000'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   126
   (16rFFFFFFFF + 2          ) hexPrintString -> '100000001'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   127
   (16rFFFFFFFF + 16rFFFFFF  ) hexPrintString -> '100FFFFFE'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   128
   (16rFFFFFFFF + 16rFFFFFFFF) hexPrintString -> '1FFFFFFFE'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   129
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   130
   20 factorial         -> 2432902008176640000
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   131
   20 factorial + 10000 -> 2432902008176650000
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   132
   20 factorial               hexPrintString -> '21C3677C82B40000'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   133
   (20 factorial + 16rFFFF)   hexPrintString -> '21C3677C82B4FFFF'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   134
   (20 factorial + 16rFFFFFF) hexPrintString -> '21C3677C83B3FFFF'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   135
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   136
   test comparison:
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   137
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   138
   -1234567890 >  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   139
   -1234567890 >= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   140
   -1234567890 <  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   141
   -1234567890 <= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   142
   -1234567890 =  -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   143
   -1234567890 ~= -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   144
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   145
   -1234567890 >  -1234567891         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   146
   -1234567890 >= -1234567891         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   147
   -1234567890 <  -1234567891         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   148
   -1234567890 <= -1234567891         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   149
   -1234567890 =  -1234567891         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   150
   -1234567890 ~= -1234567891         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   151
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   152
   -1234567891 >  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   153
   -1234567891 >= -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   154
   -1234567891 <  -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   155
   -1234567891 <= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   156
   -1234567891 =  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   157
   -1234567891 ~= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   158
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   159
    1234567890 >  -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   160
    1234567890 >= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   161
    1234567890 <  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   162
    1234567890 <= -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   163
    1234567890 =  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   164
    1234567890 ~= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   165
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   166
   -1234567890 >  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   167
   -1234567890 >= 1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   168
   -1234567890 <  1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   169
   -1234567890 <= 1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   170
   -1234567890 =  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   171
   -1234567890 ~= 1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   172
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   173
    1234567890 >  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   174
    1234567890 >= 1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   175
    1234567890 <  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   176
    1234567890 <= 1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   177
    1234567890 =  1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   178
    1234567890 ~= 1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   179
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   180
   -1234567890 >  -10                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   181
   -1234567890 >= -10                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   182
   -1234567890 <  -10                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   183
   -1234567890 <= -10                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   184
   -1234567890 =  -10                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   185
   -1234567890 ~= -10                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   186
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   187
   -1234567890 >  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   188
   -1234567890 >= 10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   189
   -1234567890 <  10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   190
   -1234567890 <= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   191
   -1234567890 =  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   192
   -1234567890 ~= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   193
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   194
   -10 >  -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   195
   -10 >= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   196
   -10 <  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   197
   -10 <= -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   198
   -10 =  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   199
   -10 ~= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   200
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   201
    10 >  -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   202
    10 >= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   203
    10 <  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   204
    10 <= -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   205
    10 =  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   206
    10 ~= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   207
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   208
    1234567890 >  -10                 true
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   209
    1234567890 >= -10                 true
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   210
    1234567890 <  -10                 false
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   211
    1234567890 <= -10                 false
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   212
    1234567890 =  -10                 false
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   213
    1234567890 ~= -10                 true
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   214
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   215
    1234567890 >  10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   216
    1234567890 >= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   217
    1234567890 <  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   218
    1234567890 <= 10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   219
    1234567890 =  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   220
    1234567890 ~= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   221
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   222
   -10 >   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   223
   -10 >=  1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   224
   -10 <   1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   225
   -10 <=  1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   226
   -10 =   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   227
   -10 ~=  1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   228
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   229
    10 >   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   230
    10 >=  1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   231
    10 <   1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   232
    10 <=  1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   233
    10 =   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   234
    10 ~=  1234567890                 true
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
   235
"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   236
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   237
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   238
!LargeInteger class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   239
4794
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   240
digitBytes:aByteArrayOfDigits
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   241
    "create and return a new LargeInteger with digits (lsb-first)
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   242
     from the argument, aByteArray.
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   243
     Experimental interface - May change/be removed without notice."
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   244
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   245
    ^ self basicNew setDigits:aByteArrayOfDigits
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   246
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   247
    "
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   248
     LargeInteger digitBytes:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
4806
1b48e7420cfe comment
Claus Gittinger <cg@exept.de>
parents: 4794
diff changeset
   249
     (LargeInteger digitBytes:#[16r12 16r34 16r56 16r78 16r90]) hexPrintString
4794
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   250
    "
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   251
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   252
    "Modified: / 8.5.1998 / 21:40:41 / cg"
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   253
!
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   254
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   255
digitBytes:aByteArrayOfDigits MSB:msb
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   256
    "create and return a new LargeInteger with digits (which may be in either msb/lsb order)
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   257
     from the argument, aByteArray."
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   258
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   259
    |digits|
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   260
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   261
    msb == false ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   262
	digits := aByteArrayOfDigits
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   263
    ] ifFalse:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   264
	digits := aByteArrayOfDigits reversed
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   265
    ].
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   266
    ^ self basicNew setDigits:digits
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   267
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   268
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   269
     (LargeInteger digitBytes:#[16r10 16r20 16r30 16r00] MSB:false) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   270
     (LargeInteger digitBytes:#[16r10 16r20 16r30 16r00] MSB:true) hexPrintString
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   271
    "
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   272
!
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   273
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   274
digitBytes:aByteArrayOfDigits sign:sign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   275
    "create and return a new LargeInteger with digits (lsb-first)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   276
     from the argument, aByteArray.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   277
     Experimental interface - May change/be removed without notice."
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   278
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   279
    ^ self basicNew setDigits:aByteArrayOfDigits sign:sign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   280
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   281
    "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   282
     LargeInteger digitBytes:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   283
     (LargeInteger digitBytes:#[16r12 16r34 16r56 16r78 16r90]) hexPrintString
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   284
    "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   285
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   286
    "Modified: / 8.5.1998 / 21:40:41 / cg"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   287
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   288
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
new
4299
2028db93d182 comment
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
   290
    "catch creation message.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   291
     LargeIntegers are only created by system code, which
4299
2028db93d182 comment
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
   292
     uses basicNew and cares for correct initialization."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   293
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
    self error:'LargeIntegers cannot be created with new'
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   297
new:numberOfDigits
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
    "catch creation message"
2
claus
parents: 1
diff changeset
   299
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   300
    self error:'LargeIntegers cannot be created with new'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   301
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   302
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   303
value:aSmallInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   304
    "create and return a new LargeInteger with value taken from
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   305
     the argument, aSmallInteger.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   306
     Notice:
4162
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   307
	this should be only used internally, since such small
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   308
	largeIntegers do not normally occur in the system.
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   309
	(They are used by myself)
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
   310
     May change/be removed without notice."
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
   311
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
    ^ self basicNew value:aSmallInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   313
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   314
    "LargeInteger value:3689"
3438
2e64ef848871 oops - last change was not good
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   315
2e64ef848871 oops - last change was not good
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   316
    "Modified: / 8.5.1998 / 21:40:41 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   317
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   319
!LargeInteger class methodsFor:'queries'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   320
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   321
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   322
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   323
     Here, true is returned."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   324
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
    ^ true
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   326
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   327
    "Modified: 23.4.1996 / 15:59:21 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   328
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   329
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   330
!LargeInteger methodsFor:'arithmetic'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
* aNumber
11731
f3adbab61060 comment
Claus Gittinger <cg@exept.de>
parents: 11723
diff changeset
   333
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   335
    |otherSign numberClass|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
    (sign == 0) ifTrue:[^ 0].  "cannot happen if correctly normalized"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
     this is the common case, multiplying with SmallInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   341
     Use a special method for this case ...
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   342
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   343
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   344
        ^ self productFromInteger:aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   346
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
     if the argument is not a largeInteger, coerce
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   349
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   350
    (numberClass == self class) ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   351
        ^ self retry:#* coercing:aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   352
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
    otherSign := aNumber sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   355
    (sign == otherSign) ifTrue:[^ self absMul:aNumber].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   356
    (otherSign == 0) ifTrue:[^ 0].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   357
    ^ (self absMul:aNumber) setSign:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   358
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   359
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   360
+ aNumber
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   361
    "return the sum of the receiver and the argument, aNumber"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   362
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   363
    |otherSign numberClass|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   364
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   365
    (sign == 0) ifTrue:[^ aNumber].  "cannot happen if correctly normalized"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   366
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   367
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   368
     this is the common case, adding a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   369
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   370
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   371
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   372
	^ self sumFromInteger:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   373
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   374
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   375
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   376
     if the argument is not a largeInteger, coerce
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   377
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   378
    (numberClass == self class) ifFalse:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   379
	^ self retry:#+ coercing:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   380
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   381
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   382
    otherSign := aNumber sign.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   383
    (sign > 0) ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   384
	"I am positive"
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   385
	(otherSign > 0) ifTrue:[^ self absPlus:aNumber sign:1].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   386
	(otherSign < 0) ifTrue:[^ self absMinus:aNumber sign:1].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   387
	^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   388
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   389
    "I am negative"
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
   390
    (otherSign > 0) ifTrue:[^ aNumber absMinus:self sign:1].
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
   391
    (otherSign < 0) ifTrue:[^ self absPlus:aNumber sign:-1].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   392
    ^ self
357
claus
parents: 345
diff changeset
   393
claus
parents: 345
diff changeset
   394
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   395
     SmallInteger maxVal
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   396
     SmallInteger maxVal + 1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   397
     SmallInteger maxVal + 2
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   398
     SmallInteger minVal
357
claus
parents: 345
diff changeset
   399
     SmallInteger minVal - 1
claus
parents: 345
diff changeset
   400
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   401
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   402
    "Modified: / 9.1.1998 / 13:26:28 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   403
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   404
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   405
- aNumber
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   406
    "return the difference of the receiver and the argument, aNumber"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   407
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   408
    |otherSign numberClass|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   409
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   410
    (sign == 0) ifTrue:[^ aNumber negated].     "cannot happen if correctly normalized"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   411
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   412
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   413
     this is the common case, subtracting a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   414
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   415
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   416
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   417
	sign > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   418
	    aNumber > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   419
		^ self absFastMinus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   420
	    ].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   421
	    ^ self absFastPlus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   422
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   423
	aNumber > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   424
	    ^ self absFastPlus:aNumber sign:-1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   425
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   426
	^ self absFastMinus:aNumber sign:-1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   427
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   428
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   429
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   430
     if the argument is not a largeInteger, coerce
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   431
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   432
    (numberClass == self class) ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   433
	^ self retry:#- coercing:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   434
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   435
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   436
    otherSign := aNumber sign.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   437
    (sign > 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   438
	"I am positive"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   439
	(otherSign > 0) ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   440
	    "+large - +large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   441
	    ^ self absMinus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   442
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   443
	(otherSign < 0) ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   444
	    "+large - -large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   445
	    ^ self absPlus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   446
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   447
	"should not happen"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   448
	^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   449
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   450
    "I am negative"
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   451
    (otherSign > 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   452
	"-large - +large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   453
	^ self absPlus:aNumber sign:-1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   454
    ].
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   455
    (otherSign < 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   456
	"-large - -large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   457
	^ self absMinus:aNumber sign:-1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   458
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   459
    ^ self
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   460
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   461
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   462
     12345678901234567890 - 0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   463
     12345678901234567890 - 1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   464
     12345678901234567890 - -1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   465
     -12345678901234567890 - 1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   466
     -12345678901234567890 - -1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   467
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   468
     12345678901234567890 - 12345678901234567880
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   469
     12345678901234567890 - 12345000000000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   470
     12345678901234567890 - -87654321098765432110
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   471
     -12345678901234567890 - 87654321098765432110
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   472
     -12345678901234567890 - -12345678901234567880
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   473
     -12345678901234567890 - -12345678901234567980
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   474
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   475
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   476
    "Modified: 20.10.1996 / 18:42:16 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   477
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   478
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   479
/ aNumber
1065
77b25fb9f9d7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
   480
    "return the quotient of the receiver and the argument, aNumber"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   481
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   482
    aNumber isInteger ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   483
	^ Fraction numerator:self denominator:aNumber
14766
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
   484
    ].
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
   485
    aNumber isFraction ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   486
	^ Fraction numerator:(self * aNumber denominator) denominator:(aNumber numerator)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   487
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   488
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   489
    "this is a q&d hack - we loose lots of precision here ..."
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   490
    ^ (self asFloat / aNumber asFloat)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2788
diff changeset
   491
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2788
diff changeset
   492
    "Modified: 28.7.1997 / 19:07:55 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   493
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   494
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   495
// aNumber
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   496
    "return the integer part of the quotient of the receivers value
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   497
     and the arguments value.
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   498
     The result is truncated toward negative infinity
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   499
     and will be negative, if the operands signs differ.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   500
     The following is always true:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   501
        (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   502
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   503
     Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3.
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   504
     Especially surprising:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   505
        -1 // 10 -> -1 (because -(1/10) is truncated towards next smaller integer, which is -1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   506
        -10 // 3 -> -4 (because -(10/3) is truncated towards next smaller integer, which is -4.
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   507
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   508
     See #quo: which returns -2 in the above case and #rem: which is the corresponding remainder."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   509
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   510
    |cls divMod quo abs "{ Class: SmallInteger }" n|
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   511
5382
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
   512
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   513
    cls := aNumber class.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   514
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   515
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   516
     this is the common case, dividing by a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   517
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   518
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   519
    (cls == SmallInteger) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   520
        abs := aNumber.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   521
        abs := abs abs.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   522
        (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   523
            divMod := self absFastDivMod:abs.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   524
        ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   525
            n := abs asLargeInteger.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   526
        ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   527
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   528
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   529
         if the argument is not a largeInteger, coerce
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   530
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   531
        (cls == self class) ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   532
            ^ self retry:#// coercing:aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   533
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   534
        n := aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   535
    ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   536
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   537
    divMod isNil ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   538
        divMod := self absDivMod:n.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   539
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   540
    quo := divMod at:1.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   541
    (sign == aNumber sign) ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   542
        "/ adjust for truncation if negative and there is a remainder ...
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   543
        "/ be careful: there is one special case to care for here:
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   544
        "/ if quo is maxInt+1, the negation can be represented as a smallInt.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   545
        quo := quo setSign:-1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   546
        (divMod at:2) == 0 ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   547
            ^ quo - 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   548
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   549
        quo digitLength == SmallInteger maxBytes ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   550
            ^ quo compressed
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   551
        ].
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   552
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   553
    ^ quo
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   554
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   555
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   556
     (9000000000 // 4000000000)   =   (900 // 400)   ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   557
     (-9000000000 // 4000000000)  =   (-900 // 400)  ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   558
     (9000000000 // -4000000000)  =   (900 // -400)  ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   559
     (-9000000000 // -4000000000) =   (-900 // -400) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   560
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   561
     16rfffffffff // 16r01ffffff  =   2048 ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   562
     16rfffffffff // 16r00ffffff  =   4096 ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   563
     16rfffffffff // 16r001fffff  =  32768 ifFalse:[self halt].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   564
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   565
     900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   566
     -900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   567
     900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   568
     -900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   569
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   570
     9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   571
     -9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   572
     9000000000 quo: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   573
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   574
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   575
     0 // 40000000000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   576
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   577
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   578
    "Modified: / 5.11.1996 / 16:39:36 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   579
    "Modified: / 27.4.1999 / 19:50:26 / stefan"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   580
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   581
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   582
\\ aNumber
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   583
    "Answer the integer remainder m defined by division with truncation toward
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   584
     negative infinity.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   585
     m < |aNumber| AND there is an integer k with (k * aNumber + m) = self
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   586
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   587
     The returned remainder has the same sign as aNumber.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   588
     The following is always true:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   589
        (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   590
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   591
     Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3.
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   592
     Especially surprising:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   593
        -1 \\ 10 -> 9  (because -(1/10) is truncated towards next smaller integer, which is -1,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   594
                        and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   595
        -10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   596
                        and -4 * 4 gives -12, so we need to add 2 to get the original -10.
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   597
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   598
     See #rem: which is the corresponding remainder for division via #quo:.
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   599
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   600
     Redefined here for speed."
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   601
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   602
    |abs rem negativeDivisor|
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   603
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   604
    aNumber negative ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   605
        negativeDivisor := true.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   606
        abs := aNumber negated.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   607
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   608
        negativeDivisor := false.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   609
        abs := aNumber.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   610
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   611
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   612
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   613
     this is the common case, dividing by a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   614
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   615
    "
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   616
    (aNumber class == SmallInteger) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   617
        (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   618
            rem := (self absFastDivMod:abs) at:2.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   619
        ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   620
            rem := self absMod:abs asLargeInteger
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   621
        ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   622
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   623
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   624
         if the argument is not a largeInteger, coerce
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   625
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   626
        (aNumber class == self class) ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   627
            ^ self retry:#\\ coercing:aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   628
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   629
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   630
        rem := self absMod:abs.
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   631
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   632
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   633
    rem = 0 ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   634
        negativeDivisor ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   635
            rem := rem setSign:-1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   636
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   637
        (self negative ~~ negativeDivisor) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   638
            "different sign, so remainder would have been negative.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   639
             rem has been rounded toward zero, this code will simulate
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   640
             rounding to negative infinity."
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   641
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   642
            rem := aNumber - rem.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   643
        ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   644
    ].
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   645
    ^ rem
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   646
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   647
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   648
     (9000000000 \\ 4000000000)   = (900 \\ 400 * 10000000)  ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   649
     (-9000000000 \\ 4000000000)  = (-900 \\ 400 * 10000000) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   650
     (9000000000 \\ -4000000000)  = (900 \\ -400 * 10000000) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   651
     (-9000000000 \\ -4000000000) = (-900 \\ -400 * 10000000)ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   652
     (16000000000 \\ 4000000000)  = (1600 \\ 400 * 10000000) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   653
     (-16000000000 \\ 4000000000)  = (-1600 \\ 400 * 10000000) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   654
     (16000000000 \\ -4000000000)  = (1600 \\ -400 * 10000000) ifFalse:[self halt].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   655
     (-16000000000 \\ -4000000000)  = (-1600 \\ -400 * 10000000) ifFalse:[self halt].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   656
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   657
     9000000000 \\ 7
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   658
     -9000000000 \\ 7
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   659
     9000000000 \\ -7
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   660
     -9000000000 \\ -7
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   661
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   662
     900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   663
     -900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   664
     900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   665
     -900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   666
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   667
     9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   668
     -9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   669
     9000000000 rem: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   670
     -9000000000 rem: -4000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   671
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   672
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   673
    "Modified: / 5.11.1996 / 17:10:10 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   674
    "Modified: / 27.4.1999 / 20:03:40 / stefan"
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   675
!
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   676
4946
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   677
abs
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   678
    "return my absolute value. redefined for speed."
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   679
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   680
    sign >= 0 ifTrue:[^ self].
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   681
    ^ self negated
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   682
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   683
    "Created: / 26.10.1999 / 21:28:06 / stefan"
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   684
!
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   685
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   686
divMod:aNumber
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   687
    "return an array filled with
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   688
	(self // aNumber) and (self \\ aNumber).
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   689
     The returned remainder has the same sign as aNumber.
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   690
     The following is always true:
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   691
	(receiver // something) * something + (receiver \\ something) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   692
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   693
     Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3.
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   694
     Especially surprising:
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   695
	-1 \\ 10 -> 9  (because -(1/10) is truncated towards next smaller integer, which is -1,
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   696
			and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1).
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   697
	-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4,
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   698
			and -4 * 4 gives -12, so we need to add 2 to get the original -10.
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   699
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   700
     This is redefined here for more performance
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   701
     (as the remainder is generated as a side effect of division)"
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   702
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   703
    |cls n|
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   704
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   705
    ((self < 0) or:[aNumber <= 0]) ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   706
	^ super divMod:aNumber
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   707
    ].
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   708
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   709
    "/ only handle the common case here
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   710
    cls := aNumber class.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   711
    (cls == SmallInteger) ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   712
	"
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   713
	 this is the common case, dividing by a SmallInteger.
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   714
	 Use a special method for this case ...
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   715
	"
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   716
	(aNumber between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   717
	    ^ self absFastDivMod:aNumber abs.
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   718
	].
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   719
	n := aNumber asLargeInteger.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   720
    ] ifFalse:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   721
	(cls == self class) ifFalse:[
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   722
	    ^ super divMod:aNumber
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   723
	].
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   724
	n := aNumber.
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   725
    ].
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   726
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   727
    ^ self absDivMod:n abs.
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   728
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   729
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   730
     9000000000 // 4000000000   => 2
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   731
     9000000000 \\ 4000000000   => 1000000000
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   732
     9000000000 divMod: 4000000000   => #(2 1000000000)
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   733
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   734
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   735
    "Created: / 29.10.1996 / 21:22:05 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   736
    "Modified: / 27.4.1999 / 19:48:58 / stefan"
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   737
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   738
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   739
negated
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   740
    "return an integer with value negated from the receivers value."
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   741
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   742
    |newNumber sz|
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   743
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   744
    (sign == 0) ifTrue:[^ 0].
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   745
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   746
    "
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   747
     special case for SmallInteger minVal
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   748
    "
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   749
    sign == 1 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   750
        sz := digitByteArray size.
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   751
%{
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   752
        int idx;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   753
        unsigned char *bp;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   754
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   755
        bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   756
        idx = __intVal(sz);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   757
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   758
        while ((idx > 1) && (bp[idx-1] == 0)) idx--;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   759
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   760
        if (idx == sizeof(INT)) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   761
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   762
# if __POINTER_SIZE__ == 8
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   763
            if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L)
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   764
# else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   765
            if ( ((unsigned INT *)bp)[0] == 0x40000000)
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   766
# endif
3428
fb8dbb5dce40 fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents: 3424
diff changeset
   767
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   768
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   769
             * generic code
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   770
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   771
            if ((bp[idx-1] == 0x40)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   772
             && (bp[idx-2] == 0)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   773
             && (bp[idx-3] == 0)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   774
             && (bp[idx-4] == 0)
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   775
# if __POINTER_SIZE__ == 8
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   776
             && (bp[idx-5] == 0)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   777
             && (bp[idx-6] == 0)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   778
             && (bp[idx-7] == 0)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   779
             && (bp[idx-8] == 0)
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   780
# endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   781
            )
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   782
#endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   783
            {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   784
                RETURN (__mkSmallInteger(_MIN_INT));
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   785
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   786
        }
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   787
%}.
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   788
"/      sz == 4 ifTrue:[
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   789
"/        (digitByteArray at:1) == 0 ifTrue:[
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   790
"/          (digitByteArray at:2) == 0 ifTrue:[
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   791
"/            (digitByteArray at:3) == 0 ifTrue:[
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   792
"/              (digitByteArray at:4) == 16r40 ifTrue:[
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   793
"/                ^ SmallInteger minVal
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   794
"/              ].
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   795
"/            ]
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   796
"/          ]
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   797
"/        ]
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   798
"/      ]
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   799
    ].
5023
5e98a2a3ea6f slightly faster #negated
Claus Gittinger <cg@exept.de>
parents: 5022
diff changeset
   800
    "/ cg - can share the digits ...
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   801
    newNumber := self class digitBytes:digitByteArray sign:(sign negated).
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   802
    ^ newNumber
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   803
!
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   804
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   805
quo:aNumber
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   806
    "return the quotient of the receiver and the argument, aNumber.
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   807
     The result is truncated toward zero (which is different from //, which
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   808
     truncates toward negative infinity).
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   809
     The results sign is negative if the receiver has a sign
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   810
     different from the args sign.
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   811
     The following is always true:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   812
        (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   813
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   814
2521
43b9d4fafb35 removed unused locals
Claus Gittinger <cg@exept.de>
parents: 2495
diff changeset
   815
    |otherSign quo abs "{ Class: SmallInteger }" |
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   816
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   817
    otherSign := aNumber sign.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   818
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   819
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   820
     this is the common case, dividing by a SmallInteger.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   821
     Use a special method for this case ...
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   822
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   823
    (aNumber class == SmallInteger) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   824
        abs := aNumber.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   825
        abs := abs abs.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   826
        (abs between:1 and:16r00ffffff) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   827
            quo := (self absFastDivMod:abs) at:1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   828
            (sign == otherSign) ifTrue:[^ quo].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   829
            ^ quo setSign:-1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   830
        ]
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   831
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   832
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   833
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   834
     if the argument is not a largeInteger, coerce
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   835
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   836
    (aNumber class == self class) ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   837
        ^ self retry:#quo: coercing:aNumber
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   838
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   839
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   840
    sign < 0 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   841
        (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber negated) at:1].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   842
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   843
        (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber) at:1].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   844
    ].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   845
    ^ ((self absDivMod:aNumber) at:1) setSign:-1
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   846
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   847
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   848
     900 // 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   849
     -900 // 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   850
     900 // -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   851
     -900 // -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   852
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   853
     9000000000 // 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   854
     -9000000000 // 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   855
     9000000000 // -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   856
     -9000000000 // -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   857
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   858
     900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   859
     -900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   860
     900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   861
     -900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   862
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   863
     9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   864
     -9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   865
     9000000000 quo: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   866
     -9000000000 quo: -4000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   867
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   868
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   869
    "Modified: / 5.11.1996 / 14:14:17 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   870
    "Modified: / 27.4.1999 / 20:01:22 / stefan"
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   871
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   872
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   873
rem:aNumber
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   874
    "return the remainder of division of the receiver by the argument, aNumber.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   875
     The returned remainder has the same sign as the receiver.
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   876
     The following is always true:
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   877
        (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   878
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   879
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   880
    |rem abs "{ Class: SmallInteger }" |
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   881
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   882
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   883
     this is the common case, dividing by a SmallInteger.
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   884
     Use special code for this case ...
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   885
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   886
    (aNumber class == SmallInteger) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   887
        abs := aNumber.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   888
        abs := abs abs.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   889
        (abs between:1 and:16r00ffffff) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   890
            rem := (self absFastDivMod:abs) at:2.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   891
        ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   892
            rem := self absMod:abs asLargeInteger
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   893
        ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   894
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   895
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   896
         if the argument is not a largeInteger, coerce
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   897
        "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   898
        (aNumber class == self class) ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   899
            ^ self retry:#rem coercing:aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   900
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   901
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   902
        rem := self absMod:aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   903
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   904
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   905
    sign < 0 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   906
        ^ rem setSign:-1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   907
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   908
    ^ rem
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   909
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   910
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   911
     900 \\ 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   912
     -900 \\ 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   913
     900 \\ -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   914
     -900 \\ -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   915
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   916
     9000000000 \\ 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   917
     -9000000000 \\ 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   918
     9000000000 \\ -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   919
     -9000000000 \\ -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   920
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   921
     900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   922
     -900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   923
     900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   924
     -900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   925
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   926
     9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   927
     -9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   928
     9000000000 rem: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   929
     -9000000000 rem: -4000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   930
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   931
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   932
    "Modified: / 5.11.1996 / 14:02:59 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   933
    "Modified: / 29.4.1999 / 11:26:51 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   934
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   935
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
   936
!LargeInteger methodsFor:'bit operators'!
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
   937
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   938
bitAnd:anInteger
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   939
    "return the bitwise-and of the receiver and the argument, anInteger"
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   940
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   941
%{  /* NOCONTEXT */
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   942
    if (__isSmallInteger(anInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   943
	INT v2 = __intVal(anInteger);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   944
	INT v1;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   945
#if defined(__LSBFIRST__)
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   946
	v1 = *(INT *)(__byteArrayVal(__INST(digitByteArray)));
4663
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
   947
#else
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   948
	unsigned char *digits = (unsigned char *)(__byteArrayVal(__INST(digitByteArray)));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   949
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   950
	v1 = digits[0] & 0xFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   951
	v1 = v1 | ((digits[1] & 0xFF)<<8);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   952
	v1 = v1 | ((digits[2] & 0xFF)<<16);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   953
	v1 = v1 | ((digits[3] & 0xFF)<<24);
11605
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
   954
# if (__POINTER_SIZE__ == 8)
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   955
	v1 = v1 | ((digits[4] & 0xFF)<<32);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   956
	v1 = v1 | ((digits[5] & 0xFF)<<40);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   957
	v1 = v1 | ((digits[6] & 0xFF)<<48);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   958
	v1 = v1 | ((digits[7] & 0xFF)<<56);
11605
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
   959
 #endif
4663
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
   960
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   961
	RETURN ( __mkSmallInteger(v1 & v2) );
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   962
    }
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   963
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   964
    if (__isLargeInteger(anInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   965
	OBJ _myDigitByteArray = __INST(digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   966
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(anInteger)->l_digits;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   967
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   968
	if (__isByteArray(_myDigitByteArray)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   969
	 && __isByteArray(_otherDigitByteArray)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   970
	    unsigned char *pDigits1, *pDigits2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   971
	    int size1, size2, minSize;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   972
	    union {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   973
		double d;                    // force align
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   974
		unsigned char chars[2048+8];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   975
	    } buffer;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   976
	    unsigned char *pRslt;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   977
	    OBJ newDigits, newLarge;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   978
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   979
	    pDigits1 = (unsigned char *)(__byteArrayVal(_myDigitByteArray));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   980
	    pDigits2 = (unsigned char *)(__byteArrayVal(_otherDigitByteArray));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   981
	    pRslt = (void *)(buffer.chars);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   982
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   983
	    size1 = __byteArraySize(_myDigitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   984
	    size2 = __byteArraySize(_otherDigitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   985
	    minSize = (size1 < size2) ? size1 : size2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   986
	    if (minSize <= sizeof(buffer.chars)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   987
		int n = minSize;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   988
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   989
	    /* not worth it - but a nice try and first testbed for mmx... */
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   990
#define x__USE_MMX__
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   991
#ifdef __USE_MMX__
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   992
#ifdef __VISUALC__
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   993
		if (((INT)pRslt & 7) == 0) {    // 8-byte aligned
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   994
		    if (((INT)pDigits1 & 7) == ((INT)pDigits2 & 7)) {   // same align
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   995
			while (((INT)pDigits1 & 7) && (n >= sizeof(int))) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   996
			    ((int *)pRslt)[0] = ((int *)pDigits1)[0] & ((int *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   997
			    pRslt += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   998
			    pDigits1 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   999
			    pDigits2 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1000
			    pDigits2 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1001
			    n -= sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1002
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1003
			for (; n >= 8; n -= 8) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1004
			    __asm {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1005
				mov eax, pDigits1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1006
				movq mm0, [eax]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1007
				mov eax, pDigits2
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1008
				movq mm1, [eax]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1009
				pand mm0, mm1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1010
				mov eax, pRslt
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1011
				movq [eax], mm0
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1012
			    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1013
			    pDigits1 += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1014
			    pDigits2 += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1015
			    pRslt += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1016
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1017
			__asm {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1018
			    emms ; switch back to FPU state.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1019
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1020
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1021
		}
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1022
#endif /* __VISUALC__ */
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1023
#endif /* __USE_MMX__ */
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1024
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1025
		for (; n >= sizeof(INT)*4; n -= sizeof(INT)*4) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1026
		    ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1027
		    ((INT *)pRslt)[1] = ((INT *)pDigits1)[1] & ((INT *)pDigits2)[1];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1028
		    ((INT *)pRslt)[2] = ((INT *)pDigits1)[2] & ((INT *)pDigits2)[2];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1029
		    ((INT *)pRslt)[3] = ((INT *)pDigits1)[3] & ((INT *)pDigits2)[3];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1030
		    pRslt += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1031
		    pDigits1 += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1032
		    pDigits2 += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1033
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1034
		for (; n >= sizeof(INT); n -= sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1035
		    ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1036
		    pRslt += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1037
		    pDigits1 += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1038
		    pDigits2 += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1039
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1040
		for (; n > 0; n--) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1041
		    *pRslt = *pDigits1 & *pDigits2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1042
		    pRslt++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1043
		    pDigits1++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1044
		    pDigits2++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1045
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1046
		// normalize
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1047
		while ((pRslt[-1]==0) && (pRslt > buffer.chars)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1048
		    pRslt--;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1049
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1050
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1051
		// allocate result
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1052
		n = pRslt-buffer.chars;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1053
		if (n <= sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1054
		    INT val = 0;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1055
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1056
		    // make it a smallInteger / 32bitInteger
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1057
		    while (n > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1058
			val = (val << 8) + buffer.chars[--n];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1059
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1060
		    RETURN (__MKUINT(val));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1061
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1062
		newDigits = __MKBYTEARRAY(buffer.chars, n);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1063
		if (newDigits) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1064
		    __PROTECT__(newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1065
		    newLarge = __STX___new(sizeof(struct __LargeInteger));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1066
		    __UNPROTECT__(newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1067
		    if (newLarge) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1068
			__InstPtr(newLarge)->o_class = LargeInteger; __STORE(newLarge, LargeInteger);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1069
			__LargeIntegerInstPtr(newLarge)->l_digits = newDigits; __STORE(newLarge, newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1070
			__LargeIntegerInstPtr(newLarge)->l_sign = __MKSMALLINT(1);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1071
			RETURN (newLarge);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1072
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1073
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1074
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1075
	}
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1076
    }
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1077
%}.
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1078
    ^ super bitAnd:anInteger
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1079
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1080
    "
6051
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1081
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFF) hexPrintString
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1082
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1083
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1084
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1085
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1086
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1087
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1088
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFFFFFF) hexPrintString
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1089
    "
6051
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1090
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1091
    "Modified: / 26.9.2001 / 17:34:03 / cg"
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1092
!
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1093
8334
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1094
bitAt:anIntegerIndex
11501
4c31d5628937 changed #bitAt:
Claus Gittinger <cg@exept.de>
parents: 10728
diff changeset
  1095
    "return the value of the index's bit (index starts at 1) as 0 or 1.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1096
     Notice: the result of bitAt: on negative receivers is not
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1097
	     defined in the language standard (since the implementation
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1098
	     is free to choose any internal representation for integers)"
8334
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1099
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1100
%{  /* NOCONTEXT */
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1101
    if (__isSmallInteger(anIntegerIndex)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1102
	INT idx = __smallIntegerVal(anIntegerIndex) - 1;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1103
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1104
	if (idx >= 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1105
	    int v1;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1106
	    int byteOffset = idx / 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1107
	    int digitLen   = __byteArraySize(__INST(digitByteArray));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1108
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1109
	    if (digitLen < byteOffset) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1110
		RETURN(__mkSmallInteger(0));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1111
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1112
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1113
	    v1 = (__byteArrayVal(__INST(digitByteArray)))[byteOffset];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1114
	    if (v1 & (1 << (idx % 8))) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1115
		RETURN(__mkSmallInteger(1));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1116
	    } else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1117
		RETURN(__mkSmallInteger(0));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1118
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1119
	}
8334
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1120
    }
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1121
%}.
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1122
    ^ super bitAt:anIntegerIndex
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1123
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1124
    "
11605
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1125
     TestCase should:[ 16rFFFFFFFFFF01 bitAt:0 ] raise:Error
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1126
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:49 ) == 0
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1127
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:1  ) == 1
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1128
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:2  ) == 0
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1129
     TestCase assert:( 16rFFFFFFFFFF02 bitAt:2  ) == 1
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1130
     TestCase assert:( 16rFFFFFFFF01FF bitAt:8  ) == 1
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1131
     TestCase assert:( 16rFFFFFFFF01FF bitAt:9  ) == 1
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
  1132
     TestCase assert:( 16rFFFFFFFF01FF bitAt:10 ) == 0
8334
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1133
    "
13011
398b7b7440e7 changed: #bitAt:
Claus Gittinger <cg@exept.de>
parents: 12878
diff changeset
  1134
398b7b7440e7 changed: #bitAt:
Claus Gittinger <cg@exept.de>
parents: 12878
diff changeset
  1135
    "Modified: / 10-08-2010 / 12:33:04 / cg"
8334
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1136
!
4466f66d1f9a Define #bitAt:
Stefan Vogel <sv@exept.de>
parents: 7544
diff changeset
  1137
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1138
bitXor:anInteger
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1139
    "return the bitwise-or of the receiver and the argument, anInteger.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1140
     Here, a specially tuned version for largeInteger arguments
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1141
     (to speed up some cryptographic code)"
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1142
15626
d1cacaf45c0c class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15577
diff changeset
  1143
    |len1 len2 newBytes|
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1144
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1145
    anInteger class ~~ LargeInteger ifTrue:[^ super bitXor:anInteger].
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1146
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1147
    (len1 := anInteger digitLength) > (len2 := self digitLength) ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1148
	newBytes := anInteger digitBytes copy.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1149
	newBytes bitXorBytesFrom:1 to:len2 with:digitByteArray startingAt:1
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1150
    ] ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1151
	newBytes := digitByteArray copy.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1152
	newBytes bitXorBytesFrom:1 to:len1 with:anInteger digits startingAt:1
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1153
    ].
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  1154
    ^ (self class digitBytes:newBytes) compressed
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1155
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1156
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1157
     (16r112233445566778899 bitXor:16rFF                ) printStringRadix:16 '112233445566778866'
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1158
     (16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799'
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1159
     (16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899'
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1160
     (16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 '99'
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1161
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1162
     |bigNum1 bigNum2|
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1163
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1164
     bigNum1 := 2 raisedToInteger:512.
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1165
     bigNum2 := 2 raisedToInteger:510.
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1166
     Time millisecondsToRun:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1167
	1000000 timesRepeat:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1168
	   bigNum1 bitXor:bigNum2.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1169
	]
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1170
     ]
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1171
    "
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1172
!
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1173
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1174
lowBit
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1175
    "return the bitIndex of the lowest bit set. The returned bitIndex
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1176
     starts at 1 for the least significant bit.
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1177
     Returns 0 if no bit is set.
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1178
     For negative numbers, the low bit of my absolute value is returned.
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1179
     Redefined here for more performance of the gcd: algorithm, which
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1180
     is used when big fractions are reduced (should we write a primitive for this ?)."
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1181
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1182
    |sz   "{ Class: SmallInteger }"
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1183
     idx0 "{ Class: SmallInteger }"
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1184
     byte|
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1185
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1186
    idx0 := 1.
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1187
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1188
%{
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1189
    OBJ __digitByteArray = __INST(digitByteArray);
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1190
7544
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1191
    /*
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1192
     * quickly advance over full 0-words
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1193
     */
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1194
    if (__isByteArray(__digitByteArray)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1195
	int __sz = __byteArraySize(__digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1196
	unsigned char *__bP = __byteArrayVal(__digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1197
	unsigned char *__bP0 = __bP;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1198
15262
5047292c9107 all stx macros begin with double underline (eg. __qClass instead of _qClass)
Claus Gittinger <cg@exept.de>
parents: 15242
diff changeset
  1199
	sz = __MKSMALLINT(__sz);
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1200
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1201
#ifdef __UNROLL_LOOPS__
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1202
	while (__sz > (sizeof(INT) * 4)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1203
	    if (( ((INT *)__bP)[0]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1204
		 | ((INT *)__bP)[1]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1205
		 | ((INT *)__bP)[2]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1206
		 | ((INT *)__bP)[3] ) != 0) break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1207
	    __sz -= sizeof(INT) * 4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1208
	    __bP += sizeof(INT) * 4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1209
	}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1210
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1211
	while (__sz > sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1212
	    if ( ((INT *)__bP)[0] != 0 ) break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1213
	    __sz -= sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1214
	    __bP += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1215
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1216
	while (__sz > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1217
	    unsigned int c;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1218
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1219
	    if ( (c = *__bP) != 0 ) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1220
		int bitIdx = (__bP - __bP0) * 8;
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1221
#ifdef __BSF
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1222
		{
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1223
		    int index;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1224
		    int t = c;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1225
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1226
		    index = __BSF(t);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1227
		    RETURN ( __mkSmallInteger(index + 1 + bitIdx) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1228
		}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1229
#else
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1230
		if (c & 0x0F) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1231
		    if (c & 0x03) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1232
			if (c & 0x01) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1233
			    RETURN ( __mkSmallInteger( bitIdx + 1) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1234
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1235
			    RETURN ( __mkSmallInteger( bitIdx + 2) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1236
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1237
		    } else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1238
			if (c & 0x04) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1239
			    RETURN ( __mkSmallInteger( bitIdx + 3) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1240
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1241
			    RETURN ( __mkSmallInteger( bitIdx + 4) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1242
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1243
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1244
		} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1245
		    if (c & 0x30) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1246
			if (c & 0x10) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1247
			    RETURN ( __mkSmallInteger( bitIdx + 5) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1248
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1249
			    RETURN ( __mkSmallInteger( bitIdx + 6) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1250
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1251
		    } else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1252
			if (c & 0x40) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1253
			    RETURN ( __mkSmallInteger( bitIdx + 7) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1254
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1255
			    RETURN ( __mkSmallInteger( bitIdx + 8) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1256
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1257
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1258
		}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1259
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1260
		break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1261
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1262
	    __sz--;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1263
	    __bP++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1264
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1265
	idx0 = __mkSmallInteger( __bP - __bP0 + 1 );
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1266
    }
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1267
%}.
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1268
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1269
    "/ never actually reached
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1270
    idx0 to:sz do:[:digitIndex |
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1271
	(byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1272
	    ^ (digitIndex-1)*8 + (byte lowBit)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1273
	]
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1274
    ].
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1275
    ^ 0 "/ should not happen
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1276
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1277
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1278
     (1 bitShift:0) lowBit
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1279
     (1 bitShift:10) lowBit
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1280
     (1 bitShift:20) lowBit
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1281
     (1 bitShift:30) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1282
     (1 bitShift:30) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1283
     (1 bitShift:31) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1284
     (1 bitShift:31) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1285
     (1 bitShift:32) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1286
     (1 bitShift:32) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1287
     (1 bitShift:33) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1288
     (1 bitShift:33) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1289
     (1 bitShift:64) lowBit
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1290
     (1 bitShift:64) highBit
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1291
     (1 bitShift:1000) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1292
     (1 bitShift:1000) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1293
     ((1 bitShift:64)-1) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1294
     ((1 bitShift:64)-1) highBit
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1295
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1296
     1 to:1000 do:[:idx |
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1297
	self assert:(( 1 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1298
	self assert:(( 1 bitShift:idx) lowBit = ( 1 bitShift:idx) highBit).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1299
	self assert:(( 3 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1300
	self assert:(( 7 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1301
	self assert:(( 15 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1302
	self assert:(( 31 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1303
	self assert:(( 63 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1304
	self assert:(( 127 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1305
	self assert:(( 255 bitShift:idx) lowBit = (idx+1)).
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1306
     ]
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1307
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1308
     |num|
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1309
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1310
     num := (1 bitShift:1000).
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1311
     Time millisecondsToRun:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1312
	1000000 timesRepeat:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1313
	    num lowBit
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1314
	]
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1315
     ]
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1316
    "
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1317
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1318
    "Modified: 14.8.1997 / 11:55:34 / cg"
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1319
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1320
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1321
setBit:index
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1322
    "return a new integer, where the specified bit is on.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1323
     Bits are counted from 1 starting with the least significant.
15132
83d908af0aa1 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 14766
diff changeset
  1324
     The methods name may be misleading: the receiver is not changed,
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1325
     but a new number is returned. Should be named #withBitSet:"
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1326
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1327
    |myDigitLength newDigitLength newDigitBytes byteIndexOfBitToSet|
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1328
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1329
    index <= 0 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1330
        ^ SubscriptOutOfBoundsSignal
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1331
                raiseRequestWith:index
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1332
                errorString:'index out of bounds'
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1333
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1334
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1335
    myDigitLength := digitByteArray size.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1336
    byteIndexOfBitToSet := ((index-1)//8)+1.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1337
    byteIndexOfBitToSet > myDigitLength ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1338
        newDigitLength := myDigitLength max:byteIndexOfBitToSet.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1339
        newDigitBytes := ByteArray new:newDigitLength.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1340
        newDigitBytes replaceFrom:1 to:myDigitLength with:digitByteArray startingAt:1.
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1341
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1342
        newDigitBytes := digitByteArray copy
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1343
    ].
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1344
    newDigitBytes
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1345
        at:byteIndexOfBitToSet
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1346
        put:((newDigitBytes at:byteIndexOfBitToSet) setBit:(((index-1)\\8)+1)).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1347
    ^ self class digitBytes:newDigitBytes sign:sign
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1348
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1349
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1350
     TestCase assert:( 16r80000000 setBit:3  ) = 16r80000004
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1351
     TestCase assert:( 16r80000000 setBit:33 ) = 16r180000000
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1352
    "
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1353
! !
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1354
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1355
!LargeInteger methodsFor:'bit operators-32bit'!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1356
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1357
bitInvert32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1358
    "return the value of the receiver with all bits inverted in 32bit signed int space
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1359
     (changes the sign)"
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1360
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1361
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1362
    unsigned INT v;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1363
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1364
    v = __unsignedLongIntVal(self);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1365
    v = ~v;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1366
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1367
    v &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1368
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1369
    RETURN ( __MKUINT(v) );
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1370
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1371
    ^ self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1372
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1373
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1374
     16r80000000 bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1375
     16r7FFFFFFF bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1376
     16rFFFFFFFF bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1377
     0 bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1378
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1379
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1380
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1381
bitRotate32:shiftCount
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1382
    "return the value of the receiver rotated by shiftCount bits,
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1383
     but only within 32 bits, rotating left for positive, right for negative counts.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1384
     Rotates through the sign bit.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1385
     Useful for crypt algorithms, or to emulate C/Java semantics."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1386
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1387
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1388
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1389
    unsigned INT bits;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1390
    int count;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1391
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1392
    if (__isSmallInteger(shiftCount)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1393
	count = __intVal(shiftCount);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1394
	count = count % 32;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1395
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1396
	bits = __unsignedLongIntVal(self);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1397
	if (count > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1398
	    bits = (bits << count) | (bits >> (32-count));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1399
	} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1400
	    bits = (bits >> (-count)) | (bits << (32-(-count)));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1401
	}
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1402
#if __POINTER_SIZE__ == 8
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1403
	bits &= 0xFFFFFFFFL;
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1404
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1405
	RETURN (__MKUINT(bits));
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1406
    }
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1407
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1408
    ^ self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1409
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1410
    "
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1411
     (1 bitShift32:31) rotate32:0
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1412
     (1 bitShift32:31) rotate32:1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1413
     (1 bitShift32:31) rotate32:-1
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1414
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1415
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1416
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1417
bitShift32:shiftCount
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1418
    "return the value of the receiver shifted by shiftCount bits,
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1419
     but only within 32 bits, shifting into/out-of the sign bit.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1420
     May be useful for communication interfaces, to create ST-numbers
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1421
     from a signed 32bit int value given as individual bytes,
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1422
     or to emulate C/Java semantics.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1423
     The shift is unsigned"
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1424
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1425
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1426
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1427
    unsigned INT bits;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1428
    int count;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1429
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1430
    if (__isSmallInteger(shiftCount)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1431
	count = __intVal(shiftCount);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1432
	if (count >= 32) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1433
	    RETURN (__mkSmallInteger(0));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1434
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1435
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1436
	bits = __unsignedLongIntVal(self);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1437
	if (count > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1438
	    bits = bits << count;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1439
	} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1440
	    bits = bits >> (-count);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1441
	}
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1442
#if __POINTER_SIZE__ == 8
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1443
	bits &= 0xFFFFFFFFL;
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1444
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1445
	RETURN (__MKUINT(bits));
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1446
    }
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1447
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1448
    ^ self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1449
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1450
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1451
     128 bitShift:24
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1452
     128 bitShift32:24
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1453
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1454
     1 bitShift:31
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1455
     1 bitShift32:31
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1456
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1457
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1458
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1459
bitXor32:aNumber
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1460
    "return the xor of the receiver and the argument.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1461
     The argument must be a SmallInteger or a 4-byte LargeInteger.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1462
     If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1463
     This is of course not always correct, but allows for C/Java behavior to be emulated."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1464
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1465
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1466
    INT rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1467
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1468
    rslt =  __unsignedLongIntVal(self) ^ __unsignedLongIntVal(aNumber);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1469
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1470
    rslt &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1471
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1472
    RETURN ( __MKUINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1473
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1474
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1475
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1476
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1477
     16r7FFFFFFF bitXor: 16r80000000          4294967295
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1478
     16r7FFFFFFF bitXor32: 16r80000000
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1479
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1480
! !
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1481
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1482
!LargeInteger methodsFor:'byte access'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1483
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1484
byteSwapped
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1485
    "lsb -> msb;
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1486
     i.e. a.b ... y.z -> z.y. ... b.a"
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1487
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1488
    ^ self class digitBytes:digitByteArray MSB:true
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1489
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1490
    "
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1491
     (LargeInteger value:16r11223344) byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1492
     (LargeInteger value:16r44332211) byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1493
     16r88776655 byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1494
     16r11223344 byteSwapped hexPrintString
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1495
    "
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1496
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1497
    "Created: / 31-01-2012 / 11:07:42 / cg"
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1498
!
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1499
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1500
byteSwapped32
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1501
    "byte swap a 32bit value; lsb -> msb;
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1502
     i.e. a.b.c.d -> d.c.b.a
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1503
     Useful for communication protocols"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1504
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1505
%{
15909
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1506
    unsigned INT swapped;
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1507
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1508
    swapped = ( (__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1509
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1510
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1511
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1512
    RETURN (__MKUINT(swapped));
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1513
%}.
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1514
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1515
    "
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1516
     (LargeInteger value:16r11223344) byteSwapped hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1517
     (LargeInteger value:16r44332211) byteSwapped hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1518
     16r88776655 byteSwapped hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1519
     16r11223344 byteSwapped hexPrintString
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1520
    "
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1521
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1522
    "Created: / 31-01-2012 / 11:07:42 / cg"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1523
!
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1524
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1525
byteSwapped64
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1526
    "byte swap a 64bit value; lsb -> msb;
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1527
     i.e. a.b.c.d.e.f.g.h -> h.g.f.e.d.c.b.a
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1528
     Useful for communication protocols"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1529
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1530
%{
15909
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1531
    unsigned INT swappedLO = 0;
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1532
    unsigned INT swappedHI;
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1533
    unsigned INT swapped;
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1534
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1535
    swappedHI = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1536
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1537
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1538
	      | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1539
    if (__byteArraySize(__INST(digitByteArray)) > 4) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1540
	swappedLO = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[4]) << 24)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1541
		  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[5]) << 16)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1542
		  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[6]) << 8)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1543
		  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[7]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1544
    }
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1545
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1546
#if __POINTER_SIZE__ == 8
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1547
    swapped = (swappedHI<<32) | swappedLO;
16520
a1a1d558cfa3 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 16124
diff changeset
  1548
    RETURN(__MKUINT( swapped ));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1549
#else
16520
a1a1d558cfa3 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 16124
diff changeset
  1550
    RETURN(__MKLARGEINT64(1, swappedLO, swappedHI));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1551
#endif
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1552
%}.
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1553
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1554
    "
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1555
     (LargeInteger value:16r11223344) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1556
     (LargeInteger value:16r44332211) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1557
     (LargeInteger value:16r1122334455667788) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1558
     (LargeInteger value:16r8877665544332211) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1559
     16r88776655 byteSwapped hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1560
     16r11223344 byteSwapped hexPrintString
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1561
    "
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1562
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1563
    "Created: / 31-01-2012 / 11:07:42 / cg"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1564
!
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1565
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1566
digitAt:index
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1567
    "return 8 bits of value, starting at byte index"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1568
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1569
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1570
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1571
    return context._RETURN( ((STLargeInteger)self).digitAt( index.intValue() ) );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1572
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1573
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1574
    index > digitByteArray size ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1575
    ^ digitByteArray at:index
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1576
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1577
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1578
digitAt:index put:aByte
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1579
    "set the 8 bits, index is a byte index"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1580
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1581
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1582
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1583
    ERROR("cannot modify the digits of a LargeInteger");
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1584
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1585
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1586
    digitByteArray at:index put:aByte
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1587
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1588
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1589
digitByteAt:index
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1590
    "return 8 bits of my signed value, starting at byte index.
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1591
     For positive receivers, this is the same as #digitAt:;
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1592
     for negative ones, the actual bit representation is returned."
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1593
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1594
    |t digits|
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1595
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1596
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1597
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1598
    return context._RETURN( ((STLargeInteger)self).digitByteAt( index.intValue() ) );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1599
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1600
%}.
6359
fcb206734265 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6106
diff changeset
  1601
    sign >= 0 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1602
        index > digitByteArray size ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1603
            ^ 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1604
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1605
        ^ digitByteArray at:index.
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1606
    ].
6359
fcb206734265 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6106
diff changeset
  1607
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1608
    "/ negative int - do 2's complement here
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1609
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1610
    t := self bitInvert + 1.
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1611
    t setSign:1.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1612
    digits := t digitBytes.
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1613
    index > digits size ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1614
        ^ 16rFF
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1615
    ].
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1616
    ^ digits at:index.
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1617
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1618
    "
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1619
     16r11111111111111111111 negated digitByteAt:1
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1620
     0 asLargeInteger digitByteAt:1
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1621
    "
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1622
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1623
    "Created: / 25.10.1998 / 14:12:21 / cg"
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1624
!
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1625
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1626
digitBytes
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1627
    "return a byteArray filled with the receivers bits
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1628
     (8 bits of the absolute value per element).
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1629
     Least significant byte is first!!"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1630
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1631
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1632
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1633
    return context._RETURN( ((STLargeInteger)self).digitBytes() );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1634
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1635
%}.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1636
    ^ digitByteArray
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1637
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1638
    "Modified: / 5.5.1999 / 14:57:03 / stefan"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1639
!
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1640
15569
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1641
digitBytesMSB
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1642
    "return a byteArray filled with the receivers bits
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1643
     (8 bits of the absolute value per element),
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1644
     most significant byte first"
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1645
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1646
     ^ digitByteArray copyReverse.
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1647
!
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1648
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1649
digitBytesMSB:msbFlag
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1650
    "return a byteArray filled with the receivers bits
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1651
     (8 bits of the absolute value per element),
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1652
     if msbflag = true, most significant byte is first,
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1653
     otherwise least significant byte is first"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1654
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1655
    msbFlag ifTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1656
	^ digitByteArray copyReverse.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1657
    ].
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1658
    ^ digitByteArray
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1659
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1660
    "Modified: / 5.5.1999 / 14:57:03 / stefan"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1661
!
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1662
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1663
digitLength
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1664
    "return the number bytes used by this Integer.
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1665
     For negative receivers, the digitLength of its absolute value
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1666
     is returned."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1667
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1668
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1669
     check if there is a 0-byte ...
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1670
     this allows to ask unnormalized LargeIntegers
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1671
     for their digitLength
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1672
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1673
    |l "{ Class: SmallInteger }" |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1674
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1675
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1676
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1677
    return context._RETURN( ((STLargeInteger)self).digitLength() );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1678
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1679
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1680
    l := digitByteArray size.
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1681
    [l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  1682
	l := l - 1.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1683
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1684
    ^ l
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1685
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1686
    "Modified: 31.7.1997 / 13:18:28 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1687
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1688
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1689
digits
7288
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1690
    "obsolete, use #digitBytes"
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1691
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1692
    <resource:#obsolete>
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1693
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1694
    ^ digitByteArray
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1695
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1696
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1697
!LargeInteger methodsFor:'coercing & converting'!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1698
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1699
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1700
    "return a LargeInteger with same value as myself - thats me"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1702
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1703
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1704
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1705
asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1706
    "return a 32-bit integer with my bit-pattern. Receiver must be unsigned (i.e. positive).
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1707
     May be required for bit operations on the sign-bit and/or to
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1708
     convert C/Java numbers."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1709
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1710
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1711
    int rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1712
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1713
    rslt =  (int)(__unsignedLongIntVal(self));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1714
    RETURN ( __MKINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1715
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1716
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1717
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1718
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1719
     16r80000000 asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1720
     16r40000000 asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1721
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1722
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1723
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1724
asSmallInteger
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1725
    "return a SmallInteger with same value as myself -
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1726
     the result is invalid if the receivers value cannot
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1727
     be represented as a SmallInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1728
     Q: should we raise an exception if this happens ?"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1729
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1730
    |value|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1731
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1732
    value := 0.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1733
    (sign == 0) ifFalse:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1734
	digitByteArray reverseDo:[:aDigit |
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1735
	    value := (value times:256) + aDigit
345
claus
parents: 250
diff changeset
  1736
	].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1737
	(sign < 0) ifTrue:[
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1738
	    value := value negated
345
claus
parents: 250
diff changeset
  1739
	]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1740
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1741
    ^ value
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1742
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1743
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1744
asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1745
    "return a 32-bit integer with my bit-pattern. Receiver must be unsigned (i.e. positive).
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1746
     May be required for bit operations on the sign-bit and/or to
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1747
     convert C/Java numbers."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1748
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1749
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1750
    unsigned int rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1751
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1752
    rslt =  (int)(__unsignedLongIntVal(self));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1753
    RETURN ( __MKUINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1754
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1755
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1756
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1757
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1758
     16r80000000 asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1759
     16r40000000 asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1760
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1761
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1762
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1763
coerce:aNumber
11723
40267c26c7eb comment
Claus Gittinger <cg@exept.de>
parents: 11605
diff changeset
  1764
    "convert the argument aNumber into an instance of the receivers class and return it."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1765
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1766
    ^ aNumber asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1767
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1768
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  1769
compressed
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1770
    "if the receiver can be represented as a SmallInteger, return
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1771
     a SmallInteger with my value;
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1772
     otherwise remove leading zero bytes in the digitByte array
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1773
     and return the receiver"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1774
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1775
    |index "{ Class: SmallInteger }"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1776
     val   "{ Class: SmallInteger }"
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1777
     index0
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1778
    |
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1779
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1780
%{  /* NOCONTEXT */
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1781
    OBJ t;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1782
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1783
    if (__INST(sign) == __mkSmallInteger(0)) {
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1784
	RETURN (__mkSmallInteger(0));
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1785
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1786
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1065
diff changeset
  1787
    t = __INST(digitByteArray);
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1788
    if (__isByteArray(t)) {
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1789
	unsigned char *__digitBytes = __ByteArrayInstPtr(t)->ba_element;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1790
	int _idx, _idx0;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1791
	INT _val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1792
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1793
	_idx = _idx0 = __byteArraySize(t);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1794
	while ((_idx > 0) && (__digitBytes[_idx - 1] == 0)) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1795
	    _idx--;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1796
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  1797
#if __POINTER_SIZE__ == 8
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1798
	switch (_idx) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1799
	    case 8:
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1800
		_val = __digitBytes[7];
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1801
		if (_val <= 0x40) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1802
		    _val = (_val<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1803
		    _val = (_val + __digitBytes[6]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1804
		    _val = (_val + __digitBytes[5]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1805
		    _val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1806
		    _val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1807
		    _val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1808
		    _val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1809
		    _val += __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1810
		    if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1811
			_val = -_val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1812
		    if (__ISVALIDINTEGER(_val)) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1813
			RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1814
		    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1815
		}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1816
		break;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1817
	    case 7:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1818
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1819
		_val = ((INT *)__digitBytes)[0] & 0x00FFFFFFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1820
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1821
		_val = (__digitBytes[6]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1822
		_val = (_val + __digitBytes[5]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1823
		_val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1824
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1825
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1826
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1827
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1828
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1829
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1830
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1831
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1832
	    case 6:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1833
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1834
		_val = ((INT *)__digitBytes)[0] & 0x0000FFFFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1835
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1836
		_val = (__digitBytes[5]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1837
		_val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1838
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1839
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1840
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1841
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1842
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1843
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1844
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1845
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1846
	    case 5:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1847
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1848
		_val = ((INT *)__digitBytes)[0] & 0x000000FFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1849
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1850
		_val = (__digitBytes[4]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1851
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1852
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1853
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1854
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1855
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1856
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1857
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1858
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1859
	    case 4:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1860
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1861
		_val = ((INT *)__digitBytes)[0] & 0x00000000FFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1862
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1863
		_val = (__digitBytes[3]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1864
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1865
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1866
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1867
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1868
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1869
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1870
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1871
	    case 3:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1872
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1873
		_val = ((int *)__digitBytes)[0] & 0x00FFFFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1874
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1875
		_val = (__digitBytes[2]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1876
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1877
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1878
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1879
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1880
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1881
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1882
	    case 2:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1883
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1884
		_val = ((int *)__digitBytes)[0] & 0x0000FFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1885
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1886
		_val = (__digitBytes[1]<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1887
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1888
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1889
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1890
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1891
	    case 1:
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1892
		_val = __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1893
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1894
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1895
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1896
	    case 0:
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1897
		RETURN (__mkSmallInteger(0));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1898
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1899
	}
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  1900
#else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1901
	if (_idx <= 4) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1902
	    if (_idx <= 2) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1903
		if (_idx == 0) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1904
		    RETURN (__mkSmallInteger(0));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1905
		}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1906
		if (_idx == 1) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1907
		    _val = __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1908
		    if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1909
			_val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1910
		    RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1911
		}
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1912
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1913
		_val = ((int *)__digitBytes)[0] & 0x0000FFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1914
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1915
		_val = (__digitBytes[1]<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1916
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1917
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1918
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1919
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1920
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1921
	    if (_idx == 3) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1922
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1923
		_val = ((int *)__digitBytes)[0] & 0x00FFFFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1924
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1925
		_val = (((__digitBytes[2]<<8) + __digitBytes[1])<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1926
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1927
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1928
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1929
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1930
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1931
	    _val = __digitBytes[3];
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1932
	    if (_val <= 0x40) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1933
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1934
		_val = ((int *)__digitBytes)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1935
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1936
		_val = (((((_val<<8) + __digitBytes[2])<<8) + __digitBytes[1])<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1937
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1938
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1939
		    _val = -_val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1940
		if (__ISVALIDINTEGER(_val)) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1941
		    RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1942
		}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1943
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1944
	}
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  1945
#endif
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1946
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1947
	if (_idx == _idx0) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1948
	    RETURN (self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1949
	}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1950
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1951
	/*
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1952
	 * must copy & cut off some (zero)bytes
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1953
	 */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1954
	{
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1955
	    OBJ newDigits;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1956
	    OBJ oldDigits;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1957
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1958
	    /*
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1959
	     * careful - there is no context here to protect
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1960
	     * the receiver ...
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1961
	     */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1962
	    __PROTECT__(self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1963
	    __PROTECT__(__INST(digitByteArray));
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1964
	    newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1965
	    __UNPROTECT__(oldDigits);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1966
	    __UNPROTECT__(self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1967
	    if (newDigits) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1968
		bcopy(__ByteArrayInstPtr(oldDigits)->ba_element,
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1969
		      __ByteArrayInstPtr(newDigits)->ba_element,
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1970
		      _idx);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1971
		__INST(digitByteArray) = newDigits; __STORE(self, newDigits);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1972
		RETURN (self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1973
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1974
	    /*
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1975
	     * allocation failed ...
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1976
	     * ... fall through to trigger the error
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1977
	     */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1978
	}
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1979
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1980
%}.
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1981
    index0 := index := digitByteArray size.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1982
    [(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1983
	index := index - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1984
    ].
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1985
"/    ((index < SmallInteger maxBytes)
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1986
"/    or:[(index == SmallInteger maxBytes)
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1987
"/            and:[(digitByteArray at:index) < 16r20]])
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1988
"/    ifTrue:[
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1989
"/        val := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1990
"/        1 to:index do:[:i |
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1991
"/            val := val bitShift:8.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1992
"/            val := val bitOr:(digitByteArray at:i).
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1993
"/        ].
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1994
"/        ^ val * sign
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  1995
"/    ].
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1996
    (index ~~ index0) ifTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1997
	digitByteArray := digitByteArray copyFrom:1 to:index
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1998
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1999
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2000
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2001
7446
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2002
generality
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2003
    "return the generality value - see ArithmeticValue>>retry:coercing:"
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2004
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2005
    ^ 40
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2006
!
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2007
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2008
nilAllInstvars
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2009
    digitByteArray nilAllInstvars.
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2010
    digitByteArray := sign := nil.
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2011
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2012
    "
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2013
      100 factorial nilAllInstvars
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2014
    "
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2015
!
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2016
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2017
value:aSmallInteger
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2018
    "setup my contents to represent the same value as aSmallInteger.
3428
fb8dbb5dce40 fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents: 3424
diff changeset
  2019
     This method will fail, if the argument is not a smallInteger.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2020
     This should only be used internally,
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2021
     since it will create an unnormalized LargeInt (by purpose) if asked for."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2022
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2023
    |absValue
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2024
     b1 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2025
     b2 "{ Class: SmallInteger }"
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2026
     b3 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2027
     b4 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2028
     b5 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2029
     b6 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2030
     b7 "{ Class: SmallInteger }"|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2031
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2032
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2033
     could have simply created a 4-byte largeinteger and normalize it.
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2034
     The code below does the normalize right away, avoiding the
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2035
     overhead of producing any intermediate byte-arrays (and the scanning)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2036
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2037
    (aSmallInteger == 0) ifTrue: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2038
	digitByteArray := ByteArray with:0.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2039
	sign := 0.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2040
	^ self
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2041
    ].
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2042
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2043
    (aSmallInteger < 0) ifTrue: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2044
	sign := -1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2045
	absValue := aSmallInteger negated
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2046
    ] ifFalse: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2047
	sign := 1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2048
	absValue := aSmallInteger
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2049
    ].
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2050
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2051
    b1 := absValue bitAnd:16rFF.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2052
    absValue := absValue bitShift:-8.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2053
    absValue == 0 ifTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2054
	digitByteArray := ByteArray with:b1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2055
    ] ifFalse:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2056
	b2 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2057
	absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2058
	absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2059
	    digitByteArray := ByteArray with:b1 with:b2
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2060
	] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2061
	    b3 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2062
	    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2063
	    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2064
		digitByteArray := ByteArray with:b1 with:b2 with:b3
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2065
	    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2066
		b4 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2067
		absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2068
		absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2069
		    digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2070
		] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2071
		    b5 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2072
		    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2073
		    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2074
			digitByteArray := ByteArray new:5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2075
			digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2076
			digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2077
			digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2078
			digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2079
			digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2080
		    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2081
			b6 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2082
			absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2083
			absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2084
			    digitByteArray := ByteArray new:6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2085
			    digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2086
			    digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2087
			    digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2088
			    digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2089
			    digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2090
			    digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2091
			] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2092
			    b7 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2093
			    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2094
			    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2095
				digitByteArray := ByteArray new:7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2096
				digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2097
				digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2098
				digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2099
				digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2100
				digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2101
				digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2102
				digitByteArray at:7 put:b7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2103
			    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2104
				digitByteArray := ByteArray new:8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2105
				digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2106
				digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2107
				digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2108
				digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2109
				digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2110
				digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2111
				digitByteArray at:7 put:b7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2112
				digitByteArray at:8 put:absValue.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2113
			    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2114
			]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2115
		    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2116
		]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2117
	    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2118
	]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2119
    ]
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2120
4230
e1cfaecf453c alpha64 bug fixes
Claus Gittinger <cg@exept.de>
parents: 4229
diff changeset
  2121
    "Modified: / 26.5.1999 / 22:18:14 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2122
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2123
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2124
!LargeInteger methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2125
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2126
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2127
    "return true, if the argument, aNumber is greater than the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2128
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2129
    |otherSign|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2130
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2131
    (aNumber class == self class) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2132
	otherSign := aNumber sign.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2133
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2134
	(sign > 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2135
	    "I am positive"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2136
	    (otherSign > 0) ifTrue:[^ self absLess:aNumber].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2137
	    ^ false "aNumber is <= 0"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2138
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2139
	"I am negative"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2140
	(otherSign > 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2141
	(otherSign == 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2142
	^ (aNumber absLess:self)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2143
    ].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2144
    (aNumber class == SmallInteger) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2145
	otherSign := aNumber sign.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2146
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2147
	(sign > 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2148
	    "I am positive"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2149
	    ^ false "aNumber is <= 0"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2150
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2151
	(sign == 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2152
	    (otherSign > 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2153
	    ^ false
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2154
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2155
	"I am negative"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2156
	^ true
14766
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2157
    ].
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2158
    "/ hack for epsilon tests
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2159
    (aNumber class == Float) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2160
	self negative ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2161
	    "/ I am a large negative; so my value is definitely below SmallInteger minVal
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2162
	    aNumber >= SmallInteger minVal asFloat ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2163
	] ifFalse:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2164
	    "/ I am a large positive; so my value is definitely above SmallInteger maxVal
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2165
	    aNumber <= SmallInteger maxVal asFloat ifTrue:[^ false].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2166
	].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2167
    ].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2168
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2169
    ^ aNumber lessFromInteger:self
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
  2170
6677
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2171
    "Modified: / 31.7.2002 / 10:08:19 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2172
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2173
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2174
= aNumber
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2175
    "return true, if the argument represents the same numeric value
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2176
     as the receiver, false otherwise"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2177
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2178
    "/ speed up compare to 0
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2179
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2180
    (aNumber == 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2181
	^ sign == 0
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2182
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2183
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2184
    (aNumber class == self class) ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2185
	"/
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2186
	"/ here, we depend on the fact, that largeinteger
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2187
	"/ results are always converted to smallInts, if possible.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2188
	"/ therefore, a largeInt in the smallInt range is not allowed (possible)
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2189
	"/
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2190
	aNumber class == SmallInteger ifTrue:[^ false ].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2191
	^ aNumber equalFromInteger:self
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2192
    ].
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2193
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2194
    (aNumber sign == sign) ifFalse:[^ false].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2195
    ^ digitByteArray = aNumber digitBytes "/ ^ self absEq:aNumber
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2196
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2197
    "Modified: / 13.2.1998 / 11:43:15 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2198
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2199
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2200
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2201
    "return true, if the argument, aNumber is less than the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2202
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2203
    |otherSign|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2204
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2205
    (aNumber class == self class) ifFalse:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2206
	^ (aNumber < self)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2207
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2208
    otherSign := aNumber sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2209
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2210
    (sign > 0) ifTrue:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2211
	"I am positive"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2212
	(otherSign > 0) ifTrue:[^ aNumber absLess:self].
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2213
	^ true "aNumber is <= 0"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2214
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2215
    (sign == 0) ifTrue:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2216
	"I am zero"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2217
	(otherSign > 0) ifTrue:[^ false].
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2218
	^ true
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2219
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2220
    "I am negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2221
    (otherSign > 0) ifTrue:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2222
    (otherSign == 0) ifTrue:[^ false].
6677
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2223
    ^ (self absLess:aNumber)
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2224
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2225
    "Modified: / 31.7.2002 / 10:08:59 / cg"
4663
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2226
!
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2227
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2228
hash
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2229
    "return an integer useful for hashing on large numbers"
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2230
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2231
    |l h m|
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2232
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2233
    h := self bitAnd:16r3FFFFFFF.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2234
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2235
    l := digitByteArray size.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2236
    l >= 8 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2237
	h := h bitXor:(digitByteArray at:l).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2238
	h := h bitXor:((digitByteArray at:l-1) bitShift:8).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2239
	h := h bitXor:((digitByteArray at:l-2) bitShift:16).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2240
	h := h bitXor:((digitByteArray at:l-3) bitShift:22).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2241
	l >= 12 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2242
	    m := l // 2.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2243
	    h := h bitXor:(digitByteArray at:m-1).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2244
	    h := h bitXor:((digitByteArray at:m) bitShift:8).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2245
	    h := h bitXor:((digitByteArray at:m+1) bitShift:16).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2246
	    h := h bitXor:((digitByteArray at:m+2) bitShift:22).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2247
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2248
	^ h
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2249
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2250
    ^ (h bitShift:3) + l
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2251
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2252
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2253
     16r80000000 hash
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2254
     16r80000008 hash
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2255
     16r8000000000008 hash
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2256
    "
17246
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2257
!
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2258
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2259
hashMultiply
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2260
    "used in some squeak code to generate an alternative hash value for integers"
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2261
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2262
    "Truncate to 28 bits and try again"
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2263
    ^(self bitAnd: 16rFFFFFFF) hashMultiply
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2264
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2265
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2266
!LargeInteger methodsFor:'double dispatching'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2267
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2268
differenceFromInteger:anInteger
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2269
    "sent, when anInteger does not know how to subtract the receiver.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2270
     Return the result of 'anInteger - self'. The argument must be a SmallInteger."
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2271
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2272
    anInteger > 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2273
	sign > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2274
	    ^ self absFastMinus:anInteger sign:-1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2275
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2276
	^ self absFastPlus:anInteger sign:1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2277
    ].
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2278
    anInteger == 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2279
	^ self negated
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2280
    ].
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2281
    sign > 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2282
	^ self absFastPlus:anInteger negated sign:-1
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2283
    ].
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2284
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2285
    self > anInteger ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2286
	^ self absFastMinus:anInteger asLargeInteger sign:-1
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2287
    ] ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2288
	^ anInteger asLargeInteger absFastMinus:self sign:-1
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2289
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2290
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2291
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2292
     12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2293
     -12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2294
     12345678901234567890 differenceFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2295
     12345678901234567890 differenceFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2296
     12345678901234567890 differenceFromInteger:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2297
     -12345678901234567890 differenceFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2298
     -12345678901234567890 differenceFromInteger:-1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2299
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2300
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2301
    "Modified: 20.10.1996 / 18:41:54 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2302
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2303
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2304
equalFromInteger:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2305
    "sent when an integer does not know how to compare to the receiver, a largeInt"
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2306
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2307
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2308
    "/ here, we depend on the fact, that largeinteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2309
    "/ results are always converted to smallInts, if possible.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2310
    "/ therefore, a largeInt in the smallInt range is not allowed (possible)
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2311
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2312
    anInteger class == SmallInteger ifTrue:[^ false ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2313
    anInteger class == self class ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2314
	^ super equalFromInteger:anInteger
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2315
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2316
    (anInteger sign == sign) ifFalse:[^ false].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2317
    ^ self absEq:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2318
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2319
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2320
lessFromInteger:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2321
    "sent when an integer does not know how to compare to the receiver, a largeInt"
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2322
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2323
    |otherSign|
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2324
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2325
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2326
    "/ here, we depend on the fact, that largeinteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2327
    "/ results are always converted to smallInts, if possible.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2328
    "/ therefore, a largeInt in the smallInt range is not allowed (possible)
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2329
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2330
    (anInteger class == SmallInteger) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2331
	otherSign := anInteger sign.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2332
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2333
	(sign > 0) ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2334
	    "I am positive"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2335
	    ^ true "aNumber is <= 0"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2336
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2337
	"I am negative"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2338
	^ false
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2339
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2340
    ^ super lessFromInteger:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2341
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2342
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2343
productFromInteger:anInteger
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2344
    "sent, when anInteger does not know how to multiply the receiver.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2345
     Return the product of the receiver and the argument, aSmallInteger"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2346
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2347
    |num result
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2348
     resultDigitByteArray
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2349
     val     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2350
     len     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2351
     carry   "{ Class: SmallInteger }"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2352
     prod    "{ Class: SmallInteger }"
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2353
     ok lResult|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2354
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2355
    "multiplying by a small integer is done here"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2356
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2357
    "trivial cases"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2358
    anInteger == 0 ifTrue:[^ 0].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2359
    anInteger == 1 ifTrue:[^ self].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2360
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2361
    num := anInteger abs.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2362
    (num > 16r3FFFFF) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2363
        "if num is too big (so that multiplying by a byte could create a Large)"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2364
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2365
        ^ anInteger retry:#* coercing:self
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2366
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2367
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2368
    len := digitByteArray size.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2369
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2370
    val := num.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
  2371
    val <= 16rFF ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2372
        lResult := len + 1.
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2373
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2374
        val <= 16rFFFF ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2375
            lResult := len + 2
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2376
        ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2377
            val <= 16rFFFFFF ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2378
                lResult := len + 4.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2379
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2380
                lResult := len + 6.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2381
            ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2382
        ]
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2383
    ].
2933
8b65cb642d91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2923
diff changeset
  2384
    resultDigitByteArray := ByteArray uninitializedNew:lResult.
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2855
diff changeset
  2385
    result := self class basicNew setDigits:resultDigitByteArray.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2386
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2387
    anInteger < 0 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2388
        sign > 0 ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2389
            result setSign:-1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2390
        ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2391
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2392
        sign < 0 ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2393
            result setSign:sign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2394
        ]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2395
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2396
85
claus
parents: 77
diff changeset
  2397
    ok := false.
claus
parents: 77
diff changeset
  2398
%{
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2399
    OBJ __digitByteArray = __INST(digitByteArray);
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2400
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2401
    if (__isSmallInteger(len)
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2402
     && __isByteArray(__digitByteArray)
85
claus
parents: 77
diff changeset
  2403
     && __isByteArray(resultDigitByteArray)) {
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2404
        INT _l = __intVal(len);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2405
        INT _v = __intVal(val);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2406
        unsigned INT _carry = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2407
        unsigned INT _prod;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2408
        unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2409
        unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2410
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2411
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2412
         * skipping zeros does not help much (a few percent) on
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2413
         * a P5 or other CPUS with a fast multiplier.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2414
         * It may make more of a difference on CPUs with slower 0-multiply.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2415
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2416
        while ((_l >= sizeof(INT)) && (((unsigned INT *)digitP)[0] == 0)) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2417
            ((unsigned INT *)resultP)[0] = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2418
            digitP += sizeof(INT);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2419
            resultP += sizeof(INT);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2420
            _l -= sizeof(INT);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2421
        }
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
  2422
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  2423
#if defined(__LSBFIRST__)
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  2424
# if defined (__GNUC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2425
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2426
         * can do it long-word-wise;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2427
         * 32*32 -> 64 multiplication
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2428
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2429
        while (_l > 3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2430
            unsigned __pHi, __pLow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2431
            unsigned __digit;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2432
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2433
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2434
             * max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2435
             * + maxCarry (0xFFFF.FFFF)  -> 0xFFFF.FFFF.0000.0000
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2436
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2437
            __digit = ((unsigned long *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2438
            asm ("mull %3               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2439
                  addl %4,%%eax         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2440
                  adcl $0,%%edx"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2441
                    : "=a"  (__pLow),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2442
                      "=d"  (__pHi)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2443
                    : "0"   (__digit),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2444
                      "1"   ((unsigned long)(_v)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2445
                      "rm"  ((unsigned long)(_carry)) );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2446
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2447
            ((unsigned long *)resultP)[0] = __pLow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2448
            _carry = __pHi;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2449
            digitP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2450
            resultP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2451
            _l -= 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2452
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2453
# else /* not GNU-i386 */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  2454
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2455
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2456
         * can do it long-word-wise;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2457
         * 32*32 -> 64 multiplication
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2458
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2459
        while (_l > 3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2460
            unsigned __pLow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2461
            unsigned digit;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2462
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2463
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2464
             * max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2465
             * + maxCarry (0xFFFF.FFFF)  -> 0xFFFF.FFFF.0000.0000
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2466
             */
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2467
/*
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2468
            digit = ((unsigned long *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2469
            edx::eax = (digit * _v);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2470
            edx::eax += _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2471
            ((unsigned long *)resultP)[0] = eax; -- pLow
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2472
            _carry = edx; -- pHigh
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2473
            digitP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2474
            resultP += 4;
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2475
*/
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2476
            digit = ((unsigned long *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2477
            asm {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2478
                mov   eax, digit
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2479
                mov   edx, _v
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2480
                mul   edx
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2481
                add   eax, _carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2482
                adc   edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2483
                mov   __pLow, eax
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2484
                mov   _carry, edx
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2485
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2486
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2487
            ((unsigned long *)resultP)[0] = __pLow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2488
            digitP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2489
            resultP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2490
            _l -= 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2491
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2492
#  else /* not WIN32-i386 */
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2493
#   if defined(INT64)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2494
        if (_v <= 0xFFFFFFFFL) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2495
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2496
             * have a 64bit int type ... good
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2497
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2498
            UINT64 _prod64;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2499
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2500
            /* have 64bit ints; can do it int-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2501
             *
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2502
             * max: 0xFFFFFFFF * 0xFFFFFFFF -> 0xFFFFFFFE.0001
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2503
             * + maxCarry (0xFFFFFFFF)  -> 0xFFFFFFFF.0000
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2504
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2505
            while (_l > 3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2506
                unsigned __t;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2507
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2508
                __t = ((unsigned *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2509
                digitP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2510
                _prod64 = (INT64)_v;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2511
                _prod64 *= __t;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2512
                _prod64 += _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2513
                ((unsigned *)resultP)[0] = _prod64 /* & 0xFFFFFFFFL */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2514
                _carry = _prod64 >> 32;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2515
                resultP += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2516
                _l -= 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2517
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2518
            if (_l > 1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2519
                unsigned short __t;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2520
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2521
                __t = ((unsigned short *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2522
                digitP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2523
                _prod64 = (INT64)_v;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2524
                _prod64 *= __t;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2525
                _prod64 += _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2526
                ((unsigned short *)resultP)[0] = _prod64 /* & 0xFFFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2527
                _carry = _prod64 >> 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2528
                resultP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2529
                _l -= 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2530
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2531
            if (_l > 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2532
                _prod64 = *digitP++ * _v + _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2533
                *resultP++ = _prod64 /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2534
                _carry = _prod64 >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2535
                _l--;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2536
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2537
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2538
#   else /* no INT64 type */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2539
        if (_v <= 0xFFFF) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2540
            /* can do it short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2541
             *
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2542
             * max: 0xFFFF * 0xFFFF -> 0xFFFE.0001
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2543
             * + maxCarry (0xFFFF)  -> 0xFFFF.0000
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2544
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2545
            while (_l > 1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2546
                _prod = ((unsigned short *)digitP)[0] * _v + _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2547
                ((unsigned short *)resultP)[0] = _prod /* & 0xFFFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2548
                _carry = _prod >> 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2549
                digitP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2550
                resultP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2551
                _l -= 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2552
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2553
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2554
#   endif /* no INT64 */
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2555
#  endif /* not WIN32-i386 */
3040
ea839412d642 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2964
diff changeset
  2556
# endif /* not GNU-i386 */
3099
9b76bb13bc5e 16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents: 3040
diff changeset
  2557
#else /* not LSB_FIRST */
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2558
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  2559
# ifdef __mips__
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2560
#  define LOAD_WORD_WISE
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2561
   /* no, STORE_WORD_WISE makes it slower */
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2562
# endif
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2563
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2564
        if (_v <= 0xFFFF) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2565
            /* can do it short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2566
             *
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2567
             * max: 0xFFFF * 0xFFFF -> 0xFFFE.0001
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2568
             * + maxCarry (0xFFFF)  -> 0xFFFF.0000
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2569
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2570
            while (_l > 1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2571
                unsigned int t;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2572
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2573
#if defined(LOAD_WORD_WISE)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2574
                /* better fetch short-wise */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2575
                t = ((unsigned short *)digitP)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2576
                digitP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2577
                t = ((t >> 8) | (t << 8)) & 0xFFFF;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2578
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2579
                t = (digitP[1]<<8) + digitP[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2580
                digitP += 2;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2581
#endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2582
                _prod = t * _v + _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2583
                _carry = _prod >> 16;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2584
#if defined(STORE_WORD_WISE)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2585
                /* better store short-wise */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2586
                _prod = ((_prod >> 8) | (_prod << 8)) & 0xFFFF;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2587
                ((unsigned short *)resultP)[0] = _prod;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2588
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2589
                resultP[0] = _prod /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2590
                resultP[1] = (_prod>>8) /* & 0xFF */;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2591
#endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2592
                resultP += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2593
                _l -= 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2594
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2595
        }
3099
9b76bb13bc5e 16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents: 3040
diff changeset
  2596
3040
ea839412d642 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2964
diff changeset
  2597
#endif /* LSB_FIRST */
1797
8dafe47b33fd faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents: 1794
diff changeset
  2598
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2599
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2600
         * rest is done byte-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2601
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2602
        while (_l > 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2603
            _prod = *digitP++ * _v + _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2604
            *resultP++ = _prod /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2605
            _carry = _prod >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2606
            _l--;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2607
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2608
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2609
        _l = __intVal(lResult) - __intVal(len);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2610
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2611
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2612
         * remaining carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2613
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2614
        while (_carry) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2615
            *resultP++ = _carry /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2616
            _carry >>= 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2617
            _l--;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2618
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2619
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2620
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2621
         * remaining zeros
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2622
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2623
        while (_l--) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2624
            *resultP++ = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2625
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2626
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2627
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2628
         * need compress ?
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2629
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2630
        if (resultP[-1]) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2631
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2632
             * no
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2633
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2634
            RETURN(result);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2635
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2636
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2637
        ok = true;
85
claus
parents: 77
diff changeset
  2638
    }
claus
parents: 77
diff changeset
  2639
%}.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2640
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2641
     fall back - normally not reached
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2642
     (could make it a primitive-failure as well)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2643
    "
85
claus
parents: 77
diff changeset
  2644
    ok ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2645
        carry := 0.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2646
        1 to:len do:[:i |
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2647
            prod := (digitByteArray basicAt:i) * val + carry.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2648
            resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2649
            carry := prod bitShift:-8.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2650
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2651
        [carry ~~ 0] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2652
            len := len + 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2653
            resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2654
            carry := carry bitShift:-8
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2655
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2656
        [len < lResult] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2657
            len := len + 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2658
            resultDigitByteArray basicAt:len put:0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  2659
        ]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2660
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2661
    ^ result compressed
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2662
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2663
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2664
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2665
    "sent, when anInteger does not know how to add the receiver.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2666
     Return the sum of the receiver and the argument, (which must be a SmallInteger)"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2667
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2668
    anInteger > 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2669
	sign > 0 ifTrue:[
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2670
	    ^ self absFastPlus:anInteger sign:1
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2671
	].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2672
	^ self absFastMinus:anInteger sign:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2673
    ].
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2674
    anInteger == 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2675
	^ self
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2676
    ].
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2677
    sign > 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2678
	^ self absFastMinus:anInteger sign:1
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2679
    ].
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  2680
    ^ self absFastPlus:anInteger sign:-1
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2681
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2682
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2683
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2684
     12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2685
     -12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2686
     12345678901234567890 sumFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2687
     -12345678901234567890 sumFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2688
     12345678901234567890 sumFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2689
     12345678901234567890 sumFromInteger:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2690
     -12345678901234567890 sumFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2691
     -12345678901234567890 sumFromInteger:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2692
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2693
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2694
    "Modified: / 9.1.1998 / 13:27:37 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2695
! !
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2696
15577
cc63cd495a01 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15569
diff changeset
  2697
!LargeInteger methodsFor:'modulo arithmetic'!
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2698
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2699
plus32:aNumber
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2700
    "return the sum of the receiver and the argument, as SmallInteger.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2701
     The argument must be another SmallInteger.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2702
     If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2703
     This is of course not always correct, but allows for C/Java behavior to be emulated."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2704
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2705
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2706
    INT sum;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2707
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2708
    sum =  __unsignedLongIntVal(self) + __unsignedLongIntVal(aNumber);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2709
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2710
    sum &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2711
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2712
    RETURN ( __MKUINT(sum));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2713
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2714
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2715
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2716
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2717
     16r7FFFFFFF + 1       ->  2147483648
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2718
     16r7FFFFFFF plus32: 1 ->  -2147483648
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2719
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2720
! !
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2721
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2722
!LargeInteger methodsFor:'printing & storing'!
345
claus
parents: 250
diff changeset
  2723
5682
a1a516779f72 remove storeOn: use insteat the default printString
ca
parents: 5437
diff changeset
  2724
xxxstoreOn:aStream
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2725
    "append a representation of the receiver to aStream, which can
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2726
     be used to reconstruct the receiver."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2727
5682
a1a516779f72 remove storeOn: use insteat the default printString
ca
parents: 5437
diff changeset
  2728
    aStream nextPutAll:'('.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2729
    self printOn:aStream.
5954
d706efecb134 ZeroDivide has a parameter
Claus Gittinger <cg@exept.de>
parents: 5682
diff changeset
  2730
    aStream nextPutAll:' asLargeInteger)'
d706efecb134 ZeroDivide has a parameter
Claus Gittinger <cg@exept.de>
parents: 5682
diff changeset
  2731
! !
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2732
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2733
!LargeInteger methodsFor:'private'!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2734
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2735
absDivMod:anInteger
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2736
    "return an array with two LargeIntegers representing
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2737
     abs(self) // abs(theArgument) and abs(self) \\ abs(theArgument).
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  2738
     Used as a helper for \\, //, rem: and quo:.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  2739
     This method needs a rewrite."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2740
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2741
    |dividend divisor
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2742
     quo digits
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2743
     shift "{ Class: SmallInteger }" |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2744
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2745
    anInteger == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2746
	^ ZeroDivide raiseRequestWith:thisContext
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2747
    ].
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2748
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2749
    self = anInteger ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2750
	^ Array with:1 with:0
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2751
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2752
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2753
    shift := self highBit - anInteger highBit.
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2754
    dividend := self class digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2755
    shift < 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2756
	^ Array with:0 with:dividend compressed.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2757
    ].
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  2758
    shift == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2759
	divisor := self class digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy.
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  2760
    ] ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2761
	divisor := anInteger bitShift:shift.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2762
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2763
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2764
    quo := self class basicNew numberOfDigits:((shift // 8) + 1).
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  2765
    digits := quo digitBytes.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2766
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2767
    shift := shift + 1.
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2768
    [shift > 0] whileTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2769
	(dividend absLess:divisor) ifFalse:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2770
	    digits bitSetAt:shift.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2771
	    (dividend absSubtract: divisor) ifFalse:[ "result == 0"
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2772
		^ Array with:quo compressed with:dividend compressed
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2773
	    ].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2774
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2775
	shift := shift - 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2776
	divisor div2.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2777
    ].
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2778
    ^ Array with:quo compressed with:dividend compressed
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2779
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2780
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2781
     Time millisecondsToRun:[ 10000 timesRepeat:[  16000000000 absDivMod:4000000000] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2782
     Time millisecondsToRun:[ 10000 timesRepeat:[  16000000000 absDivMod:3000000000] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2783
     16000000000 absDivMod:4000000000
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2784
     16000000000 absDivMod:3000000000
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2785
    "
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2786
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2787
    "Modified: / 5.11.1996 / 18:40:24 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2788
    "Created: / 27.4.1999 / 19:45:44 / stefan"
4454
16acdd393dd0 Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents: 4299
diff changeset
  2789
    "Modified: / 26.7.1999 / 10:46:36 / stefan"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2790
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2791
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2792
absEq:aLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2793
    "return true, if abs(self) = abs(theArgument)"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2794
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2795
    |len1 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2796
     len2 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2797
     d1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2798
     d2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2799
     otherDigitByteArray |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2800
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2801
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2802
    if (__isLargeInteger(aLargeInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2803
	OBJ _digitByteArray = __INST(digitByteArray);
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2804
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2805
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2806
	if (__isByteArray(_digitByteArray)
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2807
	 && __isByteArray(_otherDigitByteArray)) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2808
	    INT _myLen = __byteArraySize(_digitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2809
	    INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2810
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2811
	    unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2812
	    unsigned char *_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2813
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2814
	    if (_myLen == _otherLen) {
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2815
tryAgain:
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2816
		while (_myLen >= (sizeof(INT)*4)) {
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2817
		    if ( ((unsigned INT *)_myDigits)[0] != ((unsigned INT *)_otherDigits)[0]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2818
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2819
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2820
		    if ( ((unsigned INT *)_myDigits)[1] != ((unsigned INT *)_otherDigits)[1]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2821
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2822
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2823
		    if ( ((unsigned INT *)_myDigits)[2] != ((unsigned INT *)_otherDigits)[2]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2824
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2825
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2826
		    if ( ((unsigned INT *)_myDigits)[3] != ((unsigned INT *)_otherDigits)[3]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2827
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2828
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2829
		    _myDigits += sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2830
		    _otherDigits += sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2831
		    _myLen -= sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2832
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2833
		while (_myLen >= (sizeof(INT))) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2834
		    if ( *(unsigned INT *)_myDigits != *(unsigned INT *)_otherDigits) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2835
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2836
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2837
		    _myDigits += sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2838
		    _otherDigits += sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2839
		    _myLen -= sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2840
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2841
		while (_myLen > 0) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2842
		    if ( *_myDigits != *_otherDigits) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2843
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2844
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2845
		    _myDigits++;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2846
		    _otherDigits++;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2847
		    _myLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2848
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2849
		RETURN(true);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2850
	    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2851
	    /* care for unnormalized ints */
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2852
	    while ((_myLen > 0) && (_myDigits[_myLen-1] == 0)) _myLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2853
	    while ((_otherLen > 0) && (_otherDigits[_otherLen-1] == 0)) _otherLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2854
	    if (_myLen == _otherLen) goto tryAgain;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2855
	    RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2856
	}
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2857
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2858
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2859
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2860
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  2861
    otherDigitByteArray := aLargeInteger digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2862
    len2 := otherDigitByteArray size.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2863
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2864
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2865
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2866
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2867
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2868
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2869
    [(digitByteArray basicAt:len1) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2870
	len1 := len1 - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2871
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2872
    [(otherDigitByteArray basicAt:len2) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2873
	len2 := len2 - 1
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2874
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2875
    (len1 ~~ len2) ifTrue:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2876
    [len1 > 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2877
	d1 := digitByteArray basicAt:len1.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2878
	d2 := otherDigitByteArray basicAt:len1.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2879
	(d1 ~~ d2) ifTrue:[^ false].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2880
	len1 := len1 - 1
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2881
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2882
    ^ true
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2883
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2884
    "Modified: / 8.5.1999 / 18:37:02 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2885
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2886
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2887
absFastDivMod:aPositiveSmallInteger
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2888
    "return an array with two LargeIntegers representing
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2889
     abs(self) // aPositiveSmallInteger and
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2890
     abs(self) \\ aPositiveSmallInteger."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2891
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2892
    |tmp1     "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2893
     prevRest "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2894
     count    "{ Class: SmallInteger }"
85
claus
parents: 77
diff changeset
  2895
     newDigitByteArray result
claus
parents: 77
diff changeset
  2896
     ok|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2897
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2898
    aPositiveSmallInteger == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2899
	^ ZeroDivide raiseRequestWith:thisContext
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2900
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2901
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2902
"This cannot happen (if always normalized)
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2903
    self < aPositiveSmallInteger ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2904
	^ Array with:0 with:self
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2905
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2906
"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2907
    count := digitByteArray size.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2908
    result := self class basicNew numberOfDigits:count.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  2909
    newDigitByteArray := result digitBytes.
85
claus
parents: 77
diff changeset
  2910
    ok := false.
claus
parents: 77
diff changeset
  2911
%{
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  2912
    OBJ __digits;
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  2913
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  2914
    __digits = __INST(digitByteArray);
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  2915
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  2916
    if (__isByteArray(__digits)
85
claus
parents: 77
diff changeset
  2917
     && __isByteArray(newDigitByteArray)
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2918
     && __bothSmallInteger(count, aPositiveSmallInteger)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2919
	unsigned INT rest = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2920
	int index = __intVal(count);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2921
	int index0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2922
	unsigned INT divisor = __intVal(aPositiveSmallInteger);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2923
	unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2924
	unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2925
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2926
	index0 = index - 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2927
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2928
# if (__POINTER_SIZE__ == 8)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2929
	if (sizeof(int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2930
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2931
	     * divide int-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2932
	     */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2933
	    if (divisor <= 0xFFFFFFFF) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2934
		if ((index & 3) == 0) { /* even number of int32's */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2935
		    while (index > 3) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2936
			unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2937
			unsigned INT div;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2938
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2939
			index -= 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2940
# if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2941
			t = *((unsigned int *)(&digitBytes[index]));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2942
# else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2943
			t = digitBytes[index+3];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2944
			t = (t << 8) | digitBytes[index+2];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2945
			t = (t << 8) | digitBytes[index+1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2946
			t = (t << 8) | digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2947
# endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2948
			t = t | (rest << 32);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2949
			div = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2950
			rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2951
# if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2952
			*((unsigned int *)(&resultBytes[index])) = (div & 0xFFFFFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2953
# else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2954
			resultBytes[index+3] = div >> 24;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2955
			resultBytes[index+2] = div >> 16;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2956
			resultBytes[index+1] = div >> 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2957
			resultBytes[index] = div /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2958
# endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2959
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2960
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2961
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2962
	}
5014
fa22b711e749 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5008
diff changeset
  2963
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2964
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2965
	 * divide short-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2966
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2967
	if (divisor <= 0xFFFF) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2968
	    if ((index & 1) == 0) { /* even number of bytes */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2969
		while (index > 1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2970
		    unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2971
		    unsigned INT div;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2972
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2973
		    index -= 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2974
#if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2975
		    t = *((unsigned short *)(&digitBytes[index]));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2976
#else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2977
		    t = digitBytes[index+1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2978
		    t = (t << 8) | digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2979
#endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2980
		    t = t | (rest << 16);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2981
		    div = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2982
		    rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2983
#if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2984
		    *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2985
#else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2986
		    resultBytes[index+1] = div >> 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2987
		    resultBytes[index] = div /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2988
#endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2989
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2990
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2991
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2992
	while (index > 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2993
	    unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2994
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2995
	    index--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2996
	    t = digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2997
	    t = t | (rest << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2998
	    resultBytes[index] = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2999
	    rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3000
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3001
	prevRest = __mkSmallInteger(rest);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3002
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3003
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3004
	 * no need to normalize ?
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3005
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3006
	index = index0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3007
	while ((index >= sizeof(INT)) && (resultBytes[index]==0)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3008
	    index--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3009
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3010
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3011
	if (index == index0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3012
	    if (index > sizeof(INT)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3013
		RETURN ( __ARRAY_WITH2(result, prevRest));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3014
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3015
	    if ((index == sizeof(INT))
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3016
	    && resultBytes[index0] >= 0x40) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3017
		RETURN ( __ARRAY_WITH2(result, prevRest));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3018
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3019
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3020
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3021
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3022
	 * must compress
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3023
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3024
	ok = true;
85
claus
parents: 77
diff changeset
  3025
    }
claus
parents: 77
diff changeset
  3026
%}.
claus
parents: 77
diff changeset
  3027
    "
claus
parents: 77
diff changeset
  3028
     slow code - not normally reached
claus
parents: 77
diff changeset
  3029
     (could also do a primitiveFailure here)
claus
parents: 77
diff changeset
  3030
    "
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  3031
    ok ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3032
	^ self absDivMod:(self class value:aPositiveSmallInteger).
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3033
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3034
2933
8b65cb642d91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2923
diff changeset
  3035
    ^ Array with:(result compressed) with:prevRest
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3036
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3037
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3038
     16r123412341234 asLargeInteger absDivMod:(LargeInteger value:10)
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  3039
     ((16r1234 asLargeInteger absFastDivMod:16rffff) at:2) printStringRadix:16
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3040
     ((16r00123456 asLargeInteger absFastDivMod:16rffffff) at:2) printStringRadix:16
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3041
     ((666666666666666666 asLargeInteger absFastDivMod:16r3))
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3042
     ((1666666666 asLargeInteger absFastDivMod:16r3))
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3043
    "
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3044
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3045
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3046
absFastMinus:aSmallInteger sign:newSign
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3047
    "return a LargeInteger representing abs(self) - abs(theArgument)
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3048
     with sign: newSign.
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3049
     The result is normalized.
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3050
     This is a helper for addition and subtraction - not for public use."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3051
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3052
    |result resultDigitByteArray lastDigit ok
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3053
     len     "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3054
     rsltLen "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3055
     index   "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3056
     borrow  "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3057
     diff    "{ Class: SmallInteger }" |
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3058
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3059
    "/ the following code only works with
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3060
    "/ smallIntegers in the range _MIN_INT+255 .. _MAX_INT-255
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3061
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3062
    ((aSmallInteger < (SmallInteger minVal + 255))
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3063
    or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3064
        ^ self absMinus:(self class value:aSmallInteger) sign:newSign.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3065
    ].
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3066
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3067
    len := digitByteArray size.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3068
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3069
    rsltLen := len "+ 1".
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3070
    result := self class basicNew numberOfDigits:rsltLen sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3071
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3072
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3073
    borrow := aSmallInteger abs.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3074
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3075
%{
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3076
    if (__isByteArray(__INST(digitByteArray))
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3077
     && __isByteArray(resultDigitByteArray)) {
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3078
        unsigned INT __borrow = __intVal(borrow);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3079
        INT __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3080
        int __index = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3081
        int __len = __intVal(len);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3082
        unsigned char *__digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3083
        unsigned char *__resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3084
        int __len3;
4230
e1cfaecf453c alpha64 bug fixes
Claus Gittinger <cg@exept.de>
parents: 4229
diff changeset
  3085
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3086
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3087
# if (__POINTER_SIZE__ == 8)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3088
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3089
         * subtract int-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3090
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3091
        __len3 = __len - 3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3092
        while (__index < __len3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3093
            /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3094
            __diff = ((unsigned int *)(__digitP + __index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3095
            __diff -= (__borrow & 0xFFFFFFFFL);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3096
            __borrow >>= 32;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3097
            if (__diff < 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3098
                /* __diff += 0x100000000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3099
                __borrow++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3100
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3101
            ((unsigned int *)(__resultP+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3102
            __index += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3103
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3104
# endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3105
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3106
         * subtract short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3107
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3108
        while (__index < __len) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3109
            /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3110
            __diff = ((unsigned short *)(__digitP+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3111
            __diff -= (__borrow & 0xFFFF);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3112
            __borrow >>= 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3113
            if (__diff < 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3114
                /* __diff += 0x10000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3115
                __borrow++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3116
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3117
                if (__borrow == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3118
                    ((unsigned short *)(__resultP+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3119
                    __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3120
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3121
                    /* nothing more to subtract .. */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3122
                    while (__index < __len) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3123
                        ((unsigned short *)(__resultP+__index-1))[0] = ((unsigned short *)(__digitP+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3124
                        __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3125
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3126
                    if (__index <= __len) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3127
                        __resultP[__index-1] = __digitP[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3128
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3129
                    break;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3130
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3131
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3132
            ((unsigned short *)(__resultP+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3133
            __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3134
        }
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3135
#endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3136
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3137
         * subtract byte-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3138
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3139
        while (__index <= __len) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3140
            __diff = __digitP[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3141
            __diff -= (__borrow & 0xFF);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3142
            __borrow >>= 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3143
            if (__diff < 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3144
                /* __diff += 0x100; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3145
                __borrow++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3146
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3147
                if (__borrow == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3148
                    __resultP[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3149
                    __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3150
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3151
                    /* nothing more to subtract .. */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3152
                    while (__index <= __len) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3153
                        __resultP[__index-1] = __digitP[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3154
                        __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3155
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3156
                    break;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3157
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3158
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3159
            __resultP[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3160
            __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3161
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3162
        lastDigit = __mkSmallInteger( __resultP[__index-1-1] );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3163
        ok = true;
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3164
    }
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3165
%}.
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3166
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3167
    ok == true ifFalse:[        "/ cannot happen
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3168
        index := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3169
        [borrow ~~ 0] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3170
            (index <= len) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3171
                diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3172
                borrow := borrow bitShift:-8.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3173
                diff < 0 ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3174
                    diff := diff + 256.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3175
                    borrow := borrow + 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3176
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3177
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3178
                diff := borrow bitAnd:255.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3179
                borrow := borrow bitShift:-8.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3180
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3181
            resultDigitByteArray basicAt:index put:(lastDigit := diff).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3182
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3183
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3184
        [index <= len] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3185
            resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3186
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3187
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3188
        (index <= rsltLen) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3189
            lastDigit := 0.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3190
        ]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3191
    ].
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3192
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3193
    (lastDigit == 0 or:[rsltLen <= SmallInteger maxBytes]) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3194
        ^ result compressed.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3195
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3196
    ^ result
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3197
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3198
    "
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3199
     12345678900000000000 absFastMinus:1 sign:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3200
     12345678900000000000 absFastMinus:1 sign:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3201
     12345678900000000000 absFastMinus:1000000 sign:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3202
     12345678900000000000 absFastMinus:255 sign:1
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3203
     (SmallInteger maxVal + 1) absFastMinus:1 sign:1
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3204
     (SmallInteger minVal - 1) absFastMinus:1 sign:1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3205
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  3206
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3207
    "Modified: 24.3.1997 / 21:33:25 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3208
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3209
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3210
absFastPlus:aSmallInteger sign:newSign
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3211
    "return a LargeInteger representing abs(self) + abs(theArgument).
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3212
     with sign: newSign.
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
  3213
     The result is normalized. The argument must be a smallInteger
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3214
     with byteSize one less than the integer byteSize.
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3215
     This is a helper for addition and subtraction - not for public use."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3216
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  3217
    |result resultDigitByteArray lastDigit
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3218
     ok
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3219
     len     "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3220
     rsltLen "{ Class: SmallInteger }"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3221
     index "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3222
     carry "{ Class: SmallInteger }" |
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3223
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3224
    "/ the following code only works with
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3225
    "/ smallIntegers in the range _MIN_INT+255 .. _MAX_INT-255
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3226
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3227
    ((aSmallInteger < (SmallInteger minVal + 255))
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3228
    or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3229
        ^ self absPlus:(self class value:aSmallInteger) sign:newSign.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3230
    ].
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3231
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3232
    len := rsltLen := digitByteArray size.
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3233
    "/
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3234
    "/ there can only be an overflow from the high byte,
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3235
    "/ if it is 255 (since the other number is definitely smaller)
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3236
    "/
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3237
    (digitByteArray at:len) == 16rFF ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3238
        rsltLen := len + 1.
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3239
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3240
        "/ or the argument has something in the high byte ..
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3241
%{
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3242
#if __POINTER_SIZE__ == 8
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3243
        if (__intVal(aSmallInteger) & 0xFF00000000000000L) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3244
            rsltLen = __mkSmallInteger(__intVal(len) + 1);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3245
        }
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3246
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3247
        if (__intVal(aSmallInteger) & 0xFF000000) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3248
            rsltLen = __mkSmallInteger(__intVal(len) + 1);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3249
        }
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3250
#endif
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3251
%}
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3252
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3253
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3254
    result := self class basicNew numberOfDigits:rsltLen sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3255
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3256
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3257
%{
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3258
    if (__isByteArray(__INST(digitByteArray))
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3259
     && __isByteArray(resultDigitByteArray)
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3260
     && __isSmallInteger(aSmallInteger)) {
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3261
        /* carry is NOT unsigned (see negation below) */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3262
        INT __carry = __intVal(aSmallInteger);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3263
        int __index = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3264
        int __len = __intVal(len);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3265
        unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3266
        unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3267
        INT __ptrDelta = __dst - __src;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3268
        unsigned char *__srcLast = __src + __len - 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3269
        int __rsltLen = __intVal(rsltLen);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3270
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3271
        if (__carry < 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3272
            __carry = -__carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3273
        }
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
  3274
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3275
#if defined(__LSBFIRST__)
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  3276
# if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4)
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3277
#  if 0 /* NOTICE - the code below is 20% slower ... - why */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3278
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3279
         * add long-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3280
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3281
        asm("  jecxz nothingToDo                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3282
               movl  %%eax, %%esi      /* __src input */              \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3283
               movl  %%ebx, %%edi      /* __dst input */              \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3284
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3285
               /* the first 4-byte int */                             \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3286
               lodsl                   /* fetch */                    \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3287
               addl  %%edx, %%eax      /* add */                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3288
               stosl                   /* store */                    \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3289
               leal  -1(%%ecx),%%ecx   /* do not clobber carry */     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3290
               jecxz doneLoop          /* any more ? */               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3291
               /* remaining 4-byte ints */                            \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3292
               jmp   addLoop                                          \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3293
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3294
               .align 8                                               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3295
             addLoop:                                                 \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3296
               movl  0(%%esi), %%ebx   /* fetch  */                   \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3297
               jnc   copyLoop2                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3298
               movl  $0, %%eax                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3299
               leal  4(%%esi), %%esi                                  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3300
               adcl  %%ebx, %%eax      /* & add carry from prev int */\n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3301
               leal  8(%%edi), %%edi                                  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3302
               movl  %%eax, -8(%%edi)  /* store */                    \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3303
               leal  -1(%%ecx),%%ecx   /* do not clobber carry */     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3304
               jecxz doneLoop          /* any more ? */               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3305
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3306
               movl  0(%%esi), %%ebx   /* fetch  */                   \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3307
               movl  $0, %%eax                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3308
               leal  4(%%esi), %%esi                                  \
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3309
               adcl  %%ebx, %%eax      /* & add carry from prev int */\n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3310
               movl  %%eax, -4(%%edi)  /* store */                    \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3311
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3312
               loop  addLoop                                          \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3313
               jmp   doneLoop                                         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3314
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3315
               .align 8                                               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3316
             copyLoop:                                                \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3317
               movl  0(%%esi), %%ebx                                  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3318
             copyLoop2:                                               \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3319
               add   $4, %%esi                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3320
               add   $4, %%edi                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3321
               movl  %%ebx, -4(%%edi)                                 \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3322
               loop  copyLoop                                         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3323
                                                                      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3324
             doneLoop:                                                \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3325
               movl  $0, %%edx         /* do not clobber carry (xorl clears it) */   \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3326
               adcl  $0, %%edx                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3327
               movl  %%esi, %%eax      /* __src output */             \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3328
             nothingToDo:                                             \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3329
            " : "=d"  ((unsigned long)(__carry)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3330
                "=a"  (__src)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3331
              : "1"   (__src),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3332
                "b"   (__dst),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3333
                "c"   (__len / 4),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3334
                "0"   (__carry)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3335
              : "esi", "edi");
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3336
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3337
#  else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3338
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3339
            unsigned char *__srcLastX;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3340
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3341
            __srcLastX = __srcLast - 3 - 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3342
            while (__src <= __srcLastX) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3343
                unsigned int __sum, __sum2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3344
                unsigned __digit1, __digit2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3345
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3346
                __digit1 = ((unsigned *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3347
                __digit2 = ((unsigned *)__src)[1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3348
                asm ("addl %%edx,%%ecx          \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3349
                      adcl $0, %%eax            \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3350
                      movl $0, %%edx            \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3351
                      adcl $0, %%edx"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3352
                        : "=d"  ((unsigned long)(__carry)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3353
                          "=c"  ((unsigned long)(__sum)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3354
                          "=a"  ((unsigned long)(__sum2))
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3355
                        : "0"   ((unsigned long)(__carry)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3356
                          "1"   (__digit1),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3357
                          "2"   (__digit2));
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3358
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3359
                ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3360
                ((unsigned int *)(__src + __ptrDelta))[1] = __sum2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3361
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3362
                __src += 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3363
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3364
                if (__carry == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3365
                    while (__src <= __srcLastX) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3366
                        /* copy over words */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3367
                        ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3368
                        ((unsigned int *)(__src + __ptrDelta))[1] = ((unsigned int *)__src)[1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3369
                        __src += 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3370
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3371
                    while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3372
                        /* copy over bytes */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3373
                        __src[__ptrDelta] = __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3374
                        __src ++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3375
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3376
                    goto doneSource;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3377
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3378
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3379
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3380
            __srcLastX = __srcLastX + 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3381
            if (__src <= __srcLastX) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3382
                unsigned int __sum, __digit;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3383
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3384
                __digit = ((unsigned *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3385
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3386
                asm ("addl %%eax,%%edx  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3387
                      movl $0,%%eax     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3388
                      adcl $0,%%eax"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3389
                        : "=a"  ((unsigned long)(__carry)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3390
                          "=d"  ((unsigned long)(__sum))
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3391
                        : "0"   ((unsigned long)(__carry)),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3392
                          "1"   (__digit) );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3393
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3394
                ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3395
                __src += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3396
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3397
                if (__carry == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3398
                    while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3399
                        /* copy over bytes */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3400
                        __src[__ptrDelta] = __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3401
                        __src ++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3402
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3403
                    goto doneSource;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3404
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3405
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3406
        }
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3407
#  endif
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3408
# else /* not i386-GNUC */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  3409
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3410
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3411
            unsigned char *__srcLast4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3412
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3413
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3414
             * add long-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3415
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3416
            __srcLast4 = __srcLast - 3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3417
            while (__src <= __srcLast4) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3418
                unsigned int __sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3419
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3420
                __sum = ((unsigned int *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3421
                asm {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3422
                      mov eax, __sum
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3423
                      add eax, __carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3424
                      mov edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3425
                      adc edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3426
                      mov __sum, eax
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3427
                      mov __carry, edx
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3428
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3429
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3430
                ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3431
                __src += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3432
                if (__carry == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3433
                    while (__src <= __srcLast4) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3434
                        /* copy over words */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3435
                        ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3436
                        __src += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3437
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3438
                    while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3439
                        /* copy over bytes */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3440
                        __src[__ptrDelta] = __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3441
                        __src ++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3442
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3443
                    goto doneSource;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3444
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3445
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3446
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3447
#  else /* not i386-WIN32 */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3448
#   if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3449
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3450
            unsigned char *__srcLast4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3451
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3452
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3453
             * add long-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3454
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3455
            __srcLast4 = __srcLast - 3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3456
            while (__src <= __srcLast4) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3457
                unsigned INT __sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3458
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3459
                __sum = ((unsigned int *)__src)[0] + __carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3460
                ((unsigned int *)(__src + __ptrDelta))[0] = __sum /* & 0xFFFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3461
                __src += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3462
                __carry = __sum >> 32;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3463
                if (__carry == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3464
                    while (__src <= __srcLast4) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3465
                        /* copy over words */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3466
                        ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3467
                        __src += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3468
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3469
                    while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3470
                        /* copy over bytes */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3471
                        __src[__ptrDelta] = __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3472
                        __src ++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3473
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3474
                    goto doneSource;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3475
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3476
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3477
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3478
#   endif /* LSB+64bit */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3479
#  endif /* __i386__ & WIN32 */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3480
# endif /* __i386__ & GNUC */
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  3481
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3482
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3483
         * add short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3484
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3485
        while (__src < __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3486
            __carry += ((unsigned short *)__src)[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3487
            ((unsigned short *)(__src + __ptrDelta))[0] = __carry /* & 0xFFFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3488
            __carry >>= 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3489
            __src += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3490
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3491
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3492
         * last (odd) byte
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3493
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3494
        if (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3495
            __carry += __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3496
            __src[__ptrDelta] = __carry /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3497
            __carry >>= 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3498
            __src++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3499
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3500
#else /* not __LSBFIRST__ */
4292
1a12f88acb8f sparc tuning (large + small)
Claus Gittinger <cg@exept.de>
parents: 4291
diff changeset
  3501
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3502
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3503
         * add byte-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3504
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3505
        while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3506
            __carry += __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3507
            __src[__ptrDelta] = __carry /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3508
            __src++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3509
            __carry >>= 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3510
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3511
            if (__carry == 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3512
                while (__src <= __srcLast) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3513
                    /* copy over rest */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3514
                    __src[__ptrDelta] = __src[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3515
                    __src++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3516
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3517
                goto doneSource;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3518
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3519
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3520
#endif /* __LSBFIRST__ */
4185
375439993eb7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4182
diff changeset
  3521
4255
73212f63b6a6 slight speedup in large + small
Claus Gittinger <cg@exept.de>
parents: 4254
diff changeset
  3522
    doneSource: ;
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3523
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3524
         * now, at most one other byte is to be stored ...
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3525
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3526
        if (__len < __rsltLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3527
            __src[__ptrDelta] = __carry /* & 0xFF */;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3528
            __src++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3529
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3530
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3531
        if (__src[__ptrDelta-1]) {      /* lastDigit */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3532
            RETURN (result);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3533
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3534
        ok = true;
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3535
    }
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3536
%}.
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3537
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3538
    ok ~~ true ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3539
        index := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3540
        carry := aSmallInteger abs.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3541
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3542
        [carry ~~ 0] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3543
            (index <= len) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3544
                carry := (digitByteArray basicAt:index) + carry.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3545
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3546
            resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3547
            carry := carry bitShift:-8.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3548
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3549
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3550
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3551
        (index <= rsltLen) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3552
            [index <= len] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3553
                resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3554
                index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3555
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3556
            lastDigit := 0.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3557
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3558
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3559
        (lastDigit ~~ 0 and:[rsltLen > SmallInteger maxBytes]) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3560
            ^ result
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3561
        ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3562
    ].
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3563
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3564
    ^ result compressed
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  3565
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3566
    "Modified: 24.3.1997 / 21:32:41 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3567
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3568
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3569
absLess:aLargeInteger
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3570
    "return true, if abs(self) < abs(theArgument).
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3571
     This handles unnormalized largeIntegers."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3572
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3573
    |myLen "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3574
     otherLen "{ Class: SmallInteger }"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3575
     d1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3576
     d2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3577
     otherDigitByteArray |
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3578
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3579
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3580
#if defined(__LSBFIRST__)
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3581
    if (__isByteArray(__INST(digitByteArray))
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3582
     && __isLargeInteger(aLargeInteger)) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3583
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3584
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3585
	if (__isByteArray(_otherDigitByteArray)) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3586
	    unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3587
	    unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3588
	    INT _myLen = __byteArraySize(__INST(digitByteArray));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3589
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3590
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3591
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3592
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3593
		if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3594
		    unsigned INT _myVal = *((unsigned INT *)_myDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3595
		    unsigned INT _otherVal = *((unsigned INT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3596
		    RETURN( (_myVal < _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3597
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3598
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3599
# if defined(UINT64) && (__POINTER_SIZE__ != 8)
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3600
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3601
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3602
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3603
		if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3604
		    UINT64 _myVal = (UINT64)(*((UINT *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3605
		    UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3606
		    RETURN( (_myVal < _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3607
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3608
	    } else {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3609
		if (_myLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3610
		    INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3611
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3612
		    if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3613
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3614
			UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3615
			RETURN( (_myVal < _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3616
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3617
		    if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3618
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3619
			UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3620
			RETURN( (_myVal < _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3621
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3622
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3623
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3624
# endif /* UINT64 */
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3625
	}
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3626
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3627
#endif /* LSBFIRST */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3628
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3629
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3630
    myLen := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3631
    otherDigitByteArray := aLargeInteger digitBytes.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3632
    otherLen := otherDigitByteArray size.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3633
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3634
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3635
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3636
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3637
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3638
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3639
    [myLen > 0 and:[(digitByteArray basicAt:myLen) == 0]] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3640
	myLen := myLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3641
    ].
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3642
    [otherLen > 0 and:[(otherDigitByteArray basicAt:otherLen) == 0]] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3643
	otherLen := otherLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3644
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3645
    (myLen < otherLen) ifTrue:[^ true].
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3646
    (myLen > otherLen) ifTrue:[^ false].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3647
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3648
    [myLen > 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3649
	d1 := digitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3650
	d2 := otherDigitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3651
	d1 == d2 ifFalse:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3652
	    (d1 < d2) ifTrue:[^ true].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3653
	    ^ false
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3654
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3655
	myLen := myLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3656
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3657
    ^ false
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3658
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3659
    "
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3660
     |a b|
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3661
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3662
     a := 16rFEFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3663
     b := 16rFFFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3664
     Time millisecondsToRun:[
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3665
       10000000 timesRepeat:[ a absLess_old:b ]
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3666
     ]    1185 1233 1185
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3667
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3668
     |a b|
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3669
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3670
     a := 16rFEFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3671
     b := 16rFFFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3672
     Time millisecondsToRun:[
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3673
       10000000 timesRepeat:[ a absLess_n:b ]
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3674
     ] 686 655 702 702
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3675
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3676
     16rEFFFFFFF  absLess_n: 16rFFFFFFFF
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3677
     16rEFFFFFFF  absLess: 16rFFFFFFFF
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3678
    "
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3679
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3680
    "Modified: / 3.5.1999 / 08:06:28 / stefan"
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3681
    "Modified: / 8.5.1999 / 18:37:11 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3682
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3683
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3684
absLessEq:aLargeInteger
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3685
    "return true, if abs(self) <= abs(theArgument).
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3686
     This handles unnormalized largeIntegers."
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3687
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3688
    |myLen "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3689
     otherLen "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3690
     d1   "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3691
     d2   "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3692
     otherDigitByteArray |
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3693
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3694
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3695
#if defined(__LSBFIRST__)
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3696
    if (__isByteArray(__INST(digitByteArray))
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3697
     && __isLargeInteger(aLargeInteger)) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3698
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3699
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3700
	if (__isByteArray(_otherDigitByteArray)) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3701
	    unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3702
	    unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3703
	    INT _myLen = __byteArraySize(__INST(digitByteArray));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3704
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3705
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3706
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3707
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3708
		if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3709
		    unsigned INT _myVal = *((unsigned INT *)_myDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3710
		    unsigned INT _otherVal = *((unsigned INT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3711
		    RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3712
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3713
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3714
# if defined(UINT64) && (__POINTER_SIZE__ != 8)
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3715
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3716
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3717
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3718
		if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3719
		    UINT64 _myVal = (UINT64)(*((UINT *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3720
		    UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3721
		    RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3722
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3723
	    } else {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3724
		if (_myLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3725
		    INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3726
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3727
		    if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3728
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3729
			UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3730
			RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3731
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3732
		    if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3733
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3734
			UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3735
			RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3736
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3737
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3738
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3739
# endif /* UINT64 */
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3740
	}
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3741
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3742
#endif /* LSBFIRST */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3743
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3744
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3745
    myLen := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3746
    otherDigitByteArray := aLargeInteger digitBytes.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3747
    otherLen := otherDigitByteArray size.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3748
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3749
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3750
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3751
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3752
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3753
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3754
    [(digitByteArray basicAt:myLen) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3755
	myLen := myLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3756
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3757
    [(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3758
	otherLen := otherLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3759
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3760
    (myLen < otherLen) ifTrue:[^ true].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3761
    (myLen > otherLen) ifTrue:[^ false].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3762
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3763
    [myLen > 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3764
	d1 := digitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3765
	d2 := otherDigitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3766
	d1 == d2 ifFalse:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3767
	    (d1 < d2) ifTrue:[^ true].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3768
	    ^ false.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3769
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3770
	myLen := myLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3771
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3772
    ^ true
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3773
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3774
    "Created: / 13.2.1998 / 12:19:45 / stefan"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3775
    "Modified: / 30.4.1999 / 12:46:31 / stefan"
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3776
    "Modified: / 8.5.1999 / 18:37:15 / cg"
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3777
!
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3778
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3779
absMinus:aLargeInteger sign:newSign
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3780
    "return a LargeInteger representing abs(self) - abs(theArgument)
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3781
     with sign: newSign.
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  3782
     The result is normalized. The argument must be a largeInteger
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  3783
     with a byteSize smaller than the receivers byteSize.
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3784
     This is a helper for addition and subtraction - not for public use."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3785
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3786
    |result done
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3787
     otherDigitByteArray resultDigitByteArray
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3788
     len1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3789
     len2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3790
     index  "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3791
     borrow "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3792
     diff   "{ Class: SmallInteger }"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3793
     carry  "{ Class: SmallInteger }"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3794
     lastDigit
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3795
     lResult ok|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3796
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3797
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3798
    otherDigitByteArray := aLargeInteger digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3799
    len2 := otherDigitByteArray size.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3800
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3801
    len1 > len2 ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3802
        lResult := len1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3803
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3804
        lResult := (len1 max: len2) + 1.
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3805
    ].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3806
    result := self class basicNew numberOfDigits:lResult sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3807
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3808
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3809
    lastDigit := 0.
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3810
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3811
%{
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3812
    OBJ _digitByteArray = __INST(digitByteArray);
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3813
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3814
    if (__isByteArray(_digitByteArray)
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3815
     && __isByteArray(otherDigitByteArray)
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3816
     && __isByteArray(resultDigitByteArray)) {
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3817
        int __len1 = __intVal(len1);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3818
        int __len2 = __intVal(len2);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3819
        int __minLen = __len1 < __len2 ? __len1 : __len2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3820
        int __index, __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3821
        INT __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3822
        unsigned char *__myDigits, *__otherDigits, *__resultDigits;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3823
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3824
        ok = true;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3825
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3826
        __resultDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3827
        __otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3828
        __myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3829
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3830
        __index = 1;
4942
de4ae3110e9b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4920
diff changeset
  3831
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3832
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3833
# if __POINTER_SIZE__ == 8
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3834
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3835
         * subtract int-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3836
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3837
        while ((__index+3) <= __minLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3838
            /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3839
            __diff = ((unsigned int *)(__myDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3840
            __diff -= ((unsigned int *)(__otherDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3841
            __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3842
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3843
            if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3844
                __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3845
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3846
                __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3847
                /* __diff += 0x10000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3848
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3849
            ((unsigned int *)(__resultDigits+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3850
            __index += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3851
        }
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  3852
# endif /* 64bit */
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3853
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3854
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3855
         * subtract short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3856
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3857
        while (__index < __minLen) {   /* i.e. index+1 <= minLen */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3858
            /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3859
            __diff = ((unsigned short *)(__myDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3860
            __diff -= ((unsigned short *)(__otherDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3861
            __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3862
            if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3863
                __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3864
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3865
                __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3866
                /* __diff += 0x10000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3867
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3868
            ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3869
            __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3870
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3871
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3872
        if (__index == __minLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3873
            /* one of the operands has odd length - cannot continue short-wise */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3874
        } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3875
            if (__len1 > __len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3876
                while (__index < __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3877
                    /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3878
                    __diff = ((unsigned short *)(__myDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3879
                    __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3880
                    if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3881
                        __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3882
                        ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3883
                        __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3884
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3885
                        /* copy over rest */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3886
                        while (__index < __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3887
                            ((unsigned short *)(__resultDigits+__index-1))[0] = ((unsigned short *)(__myDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3888
                            __index+=2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3889
                        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3890
                        if (__index <= __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3891
                            __resultDigits[__index-1] = __myDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3892
                            __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3893
                        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3894
                        break;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3895
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3896
                    __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3897
                    /* __diff += 0x10000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3898
                    ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3899
                    __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3900
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3901
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3902
                if (__len2 > __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3903
                    while (__index < __len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3904
                        /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3905
                        __diff = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3906
                        __diff -= ((unsigned short *)(__otherDigits+__index-1))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3907
                        __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3908
                        if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3909
                            __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3910
                        } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3911
                            __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3912
                            /* __diff += 0x10000; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3913
                        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3914
                        ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3915
                        __index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3916
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3917
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3918
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3919
        }
4214
ba20de3793e2 slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents: 4213
diff changeset
  3920
#endif
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3921
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3922
         * subtract byte-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3923
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3924
        while (__index <= __minLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3925
            /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3926
            __diff = __myDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3927
            __diff -= __otherDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3928
            __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3929
            if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3930
                __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3931
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3932
                __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3933
                /* __diff += 0x100; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3934
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3935
            __resultDigits[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3936
            __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3937
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3938
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3939
        if (__len1 > __len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3940
            while (__index <= __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3941
                /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3942
                __diff = __myDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3943
                __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3944
                if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3945
                    __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3946
                    /* copy over rest */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3947
                    __resultDigits[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3948
                    __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3949
                    while (__index <= __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3950
                        __resultDigits[__index-1] = __myDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3951
                        __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3952
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3953
                    break;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3954
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3955
                __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3956
                /* __diff += 0x100; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3957
                __resultDigits[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3958
                __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3959
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3960
        } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3961
            if (__len2 > __len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3962
                while (__index <= __len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3963
                    /* do not make this into one expression - ask cg why */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3964
                    __diff = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3965
                    __diff -= __otherDigits[__index-1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3966
                    __diff -= __borrow;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3967
                    if (__diff >= 0) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3968
                        __borrow = 0;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3969
                    } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3970
                        __borrow = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3971
                        /* __diff += 0x100; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3972
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3973
                    __resultDigits[__index-1] = __diff;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3974
                    __index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3975
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3976
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3977
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3978
        borrow = __mkSmallInteger(__borrow);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3979
        index = __mkSmallInteger(__index);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3980
        lastDigit = __mkSmallInteger(__resultDigits[__intVal(lResult)-1]);
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3981
    }
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3982
%}.
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3983
    ok == true ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3984
        index := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3985
        borrow := 0.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3986
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3987
        done := false.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3988
        [done] whileFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3989
            diff := borrow.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3990
            (index <= len1) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3991
                diff := diff + (digitByteArray basicAt:index).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3992
                (index <= len2) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3993
                    diff := diff - (otherDigitByteArray basicAt:index)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3994
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3995
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3996
                (index <= len2) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3997
                    diff := diff - (otherDigitByteArray basicAt:index)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3998
                ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3999
                    "end reached"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4000
                    done := true
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4001
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4002
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4003
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4004
            "/ workaround for
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4005
            "/ gcc code generator bug
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4006
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4007
            (diff >= 0) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4008
                borrow := 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4009
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4010
                borrow := -1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4011
                diff := diff + 16r100
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4012
            ].
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4013
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4014
    "/        (diff < 0) ifTrue:[
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4015
    "/            borrow := -1.
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4016
    "/            diff := diff + 16r100
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4017
    "/        ] ifFalse:[
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4018
    "/            borrow := 0
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4019
    "/        ].
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4020
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4021
            resultDigitByteArray basicAt:index put:diff.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4022
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4023
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4024
        lastDigit := resultDigitByteArray basicAt:lResult.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4025
    ].
1408
e9db184b34ee oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  4026
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4027
    (borrow ~~ 0) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4028
        "/ must generate 255's complement
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4029
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4030
        result sign:newSign negated.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4031
        [index <= lResult] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4032
            resultDigitByteArray basicAt:index put:16rFF.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4033
            index := index + 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4034
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4035
        index := lResult.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4036
        [index > 0] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4037
            resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4038
            index := index - 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4039
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4040
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4041
        index := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4042
        carry := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4043
        [carry ~~ 0] whileTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4044
            (index <= lResult) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4045
                carry := (resultDigitByteArray basicAt:index) + carry.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4046
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4047
            resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4048
            carry := carry bitShift:-8.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4049
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4050
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4051
        lastDigit := resultDigitByteArray basicAt:lResult.
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  4052
    ].
15242
4539185c87ed class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15240
diff changeset
  4053
    (lastDigit == 0 or:[lResult <= SmallInteger maxBytes]) ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4054
        ^ result compressed.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4055
    ].
15242
4539185c87ed class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15240
diff changeset
  4056
    ^ result
1408
e9db184b34ee oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  4057
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4058
    "Modified: 5.11.1996 / 14:09:25 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4059
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4060
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4061
absMod:anInteger
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4062
    "return a LargeIntegers representing
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4063
     abs(self) \\ abs(theArgument).
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4064
     Used as a helper for \\ and rem:"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4065
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4066
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4067
    |dividend divisor
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4068
     shift "{ Class: SmallInteger }" |
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4069
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4070
    anInteger == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4071
	^ ZeroDivide raiseRequestWith:thisContext
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4072
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4073
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4074
    self = anInteger ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4075
	^ 0
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4076
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4077
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4078
    shift := self highBit - anInteger highBit.
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  4079
    dividend := self class digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4080
    shift < 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4081
	^ dividend compressed.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4082
    ].
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  4083
    shift == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4084
	divisor := LargeInteger digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  4085
    ] ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4086
	divisor := anInteger bitShift:shift.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4087
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4088
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4089
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4090
    shift := shift + 1.
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4091
    [shift > 0] whileTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4092
	(dividend absLess:divisor) ifFalse:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4093
	    (dividend absSubtract: divisor) ifFalse:[ "result == 0"
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4094
		^ dividend compressed
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4095
	    ].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4096
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4097
	shift := shift - 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4098
	divisor div2.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4099
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4100
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4101
    ^ dividend compressed
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4102
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4103
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4104
Time millisecondsToRun:[   10000 timesRepeat:[  16000000001 absMod:4000000001] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4105
Time millisecondsToRun:[   10000 timesRepeat:[  16000000001 absDivMod:4000000001] ]
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4106
     16000000000 absMod:4000000000
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4107
     16000000000 absMod:3000000000
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4108
    "
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4109
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4110
    "Modified: / 5.11.1996 / 18:40:24 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4111
    "Created: / 27.4.1999 / 19:45:44 / stefan"
4454
16acdd393dd0 Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents: 4299
diff changeset
  4112
    "Modified: / 26.7.1999 / 10:46:18 / stefan"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4113
!
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4114
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4115
absMul:aLargeInteger
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4116
    "return a LargeInteger representing abs(self) * abs(theArgument)"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4117
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4118
    |result otherDigitByteArray resultDigitByteArray ok
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4119
     idx      "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4120
     carry    "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4121
     len1     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4122
     len2     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4123
     dstIndex "{ Class: SmallInteger }"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  4124
     prod     "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  4125
     v        "{ Class: SmallInteger }"|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4126
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4127
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4128
    otherDigitByteArray := aLargeInteger digitBytes.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4129
    len2 := otherDigitByteArray size.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4130
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  4131
    result := self class basicNew numberOfDigits:(len1 + len2).
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4132
    resultDigitByteArray := result digitBytes.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4133
    ok := false.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4134
%{
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1065
diff changeset
  4135
    if (__isByteArray(__INST(digitByteArray))
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4136
     && __isByteArray(otherDigitByteArray)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4137
     && __isByteArray(resultDigitByteArray)
250
a5deb61ffdac *** empty log message ***
claus
parents: 175
diff changeset
  4138
     && __bothSmallInteger(len1, len2)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4139
	unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4140
	unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4141
	unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4142
	unsigned char *_p1, *_pResult0, *_p1Last, *_p2Last, *_pResultLast;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4143
	unsigned char *_pResultLastShort, *_pResultLastInt;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4144
	unsigned INT _v;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4145
	int _len1 = __intVal(len1);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4146
	int _len2 = __intVal(len2);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4147
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4148
	_p1Last = myBytes    + _len1 - 1;  /* the last byte */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4149
	_p2Last = otherBytes + _len2 - 1;  /* the last byte */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4150
	_pResult0 = resultBytes;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4151
	_pResultLast = resultBytes + _len1 + _len2 - 1; /* the last byte */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4152
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4153
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4154
	 *        aaa...aaa      f1[0] * f2
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4155
	 *       bbb...bbb       f1[1] * f2
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4156
	 *      ccc...ccc        f1[2] * f2
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4157
	 *     ...
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4158
	 *    xxx...xxx          f1[high] * f2
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4159
	 *
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4160
	 * start short-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4161
	 * bounds: (16rFFFF * 16rFFFF) + 16rFFFF -> FFFF0000
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4162
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4163
	_p1 = myBytes;
4231
5e9e9319a9de alpha large*large speedup
Claus Gittinger <cg@exept.de>
parents: 4230
diff changeset
  4164
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4165
#if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4166
	_pResultLastInt = _pResultLast - 3;   /* the last int */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4167
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4168
	/* loop over ints of f1 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4169
	for (; _p1 <= (_p1Last-3); _p1 += 4, _pResult0 += 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4170
	    unsigned char *_pResult, *_p2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4171
	    unsigned INT word1 = ((unsigned int *)_p1)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4172
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4173
	    _pResult = _pResult0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4174
	    _p2 = otherBytes;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4175
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4176
	    /* loop over ints of f2 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4177
	    while (_p2 <= (_p2Last-3)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4178
		_v = (word1 * (unsigned INT)(((unsigned int *)_p2)[0])) + ((unsigned INT)((unsigned int *)_pResult)[0]);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4179
		((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4180
		_v >>= 32; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4181
		_pResult += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4182
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4183
		    unsigned char *_pResult1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4184
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4185
		    /* distribute carry - int-wise, then byte-wise */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4186
		    for (_pResult1 = _pResult; _v; _pResult1 += 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4187
			if (_pResult1 > _pResultLastInt) break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4188
			_v += (unsigned INT)(((unsigned int *)_pResult1)[0]);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4189
			((unsigned int *)_pResult1)[0] = _v /* & 0xFFFFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4190
			_v >>= 32;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4191
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4192
		    for (; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4193
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4194
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4195
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4196
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4197
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4198
		_p2 += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4199
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4200
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4201
	    /* possible odd highByte of f2 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4202
	    while (_p2 <= _p2Last) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4203
		_v = (word1 * (unsigned INT)((unsigned char *)_p2[0])) + (unsigned INT)((unsigned char *)_pResult[0]);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4204
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4205
		((unsigned char *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4206
		_v >>= 8; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4207
		_pResult++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4208
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4209
		    unsigned char *_pResult1 = _pResult;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4210
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4211
		    /* distribute carry byte-wise */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4212
		    for (; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4213
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4214
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4215
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4216
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4217
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4218
		_p2++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4219
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4220
	}
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4221
#endif /* 64bit */
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4222
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4223
	/* possible odd high short of f1 (or shortLoop, if not 64bit) */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4224
	_pResultLastShort = _pResultLast - 1; /* the last short */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4225
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4226
	for (; _p1 < _p1Last; _p1 += 2, _pResult0 += 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4227
	    unsigned char *_pResult, *_p2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4228
	    unsigned int short1 = ((unsigned short *)_p1)[0];
4231
5e9e9319a9de alpha large*large speedup
Claus Gittinger <cg@exept.de>
parents: 4230
diff changeset
  4229
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4230
#if !defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4231
	    short1 = ((short1 >> 8) & 0xFF) | ((short1 & 0xFF) << 8);
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4232
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4233
	    _pResult = _pResult0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4234
	    _p2 = otherBytes;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4235
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4236
	    /* loop over shorts of f2 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4237
	    while (_p2 < _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4238
#if !defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4239
		unsigned int _short2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4240
		unsigned int _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4241
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4242
		_short2 = ((unsigned short *)_p2)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4243
		_short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4244
		_short3 = ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4245
		_short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4246
		_v = (short1 * _short2) + _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4247
		_pResult[0] = _v;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4248
		_pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4249
#else /* __LSBFIRST__ */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4250
		_v = (short1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4251
		((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4252
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4253
		_v >>= 16; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4254
		_pResult += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4255
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4256
		    unsigned char *_pResult1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4257
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4258
		    /* distribute carry - short-wise, then byte-wise */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4259
		    _pResult1 = _pResult;
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4260
#if defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4261
		    for (; _v; _pResult1 += 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4262
			if (_pResult1 > _pResultLastShort) break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4263
			_v += ((unsigned short *)_pResult1)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4264
			((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4265
			_v >>= 16;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4266
		    }
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4267
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4268
		    for (; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4269
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4270
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4271
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4272
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4273
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4274
		_p2 += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4275
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4276
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4277
	    /* possible odd highByte of f2 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4278
	    if (_p2 <= _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4279
#if !defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4280
		unsigned int _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4281
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4282
		_short3 = ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4283
		_short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4284
		_v = (short1 * _p2[0]) + _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4285
		_pResult[0] = _v;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4286
		_pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4287
#else /* __LSBFIRST__ */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4288
		_v = (short1 * _p2[0]) + ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4289
		((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4290
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4291
		_v >>= 16; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4292
		_pResult += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4293
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4294
		    unsigned char *_pResult1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4295
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4296
		    /* distribute carry - short-wise, then byte-wise */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4297
		    _pResult1 = _pResult;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4298
#if defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4299
		    for (; _v; _pResult1 += 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4300
			if (_pResult1 > _pResultLastShort) break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4301
			_v += ((unsigned short *)_pResult1)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4302
			((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4303
			_v >>= 16;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4304
		    }
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4305
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4306
		    for (; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4307
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4308
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4309
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4310
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4311
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4312
		_p2++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4313
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4314
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4315
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4316
	/* possible odd highByte of f1 (or byteLoop, if above is ever disabled) */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4317
	for (; _p1 <= _p1Last; _p1++, _pResult0++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4318
	    unsigned char *_pResult, *_p2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4319
	    unsigned int byte1 = _p1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4320
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4321
	    _pResult = _pResult0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4322
	    _p2 = otherBytes;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4323
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4324
	    /* loop over shorts of f2 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4325
	    while (_p2 < _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4326
#if !defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4327
		unsigned int _short2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4328
		unsigned int _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4329
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4330
		_short2 = ((unsigned short *)_p2)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4331
		_short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4332
		_short3 = ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4333
		_short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4334
		_v = (byte1 * _short2) + _short3;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4335
		_pResult[0] = _v;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4336
		_pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4337
#else /* __LSBFIRST__ */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4338
		_v = (byte1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4339
		((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4340
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4341
		_v >>= 16; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4342
		_pResult += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4343
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4344
		    unsigned char *_pResult1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4345
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4346
		    /* distribute carry - short-wise, then byte-wise */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4347
		    _pResult1 = _pResult;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4348
#if defined(__LSBFIRST__)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4349
		    for (_pResult1 = _pResult; _v; _pResult1 += 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4350
			if (_pResult1 > _pResultLastShort) break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4351
			_v += ((unsigned short *)_pResult1)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4352
			((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4353
			_v >>= 16;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4354
		    }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4355
#endif /* __LSBFIRST__ */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4356
		    for (; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4357
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4358
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4359
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4360
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4361
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4362
		_p2 += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4363
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4364
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4365
	    /* possible odd highByte of f2 (or byteLoop, if not __LSBFIRST__) */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4366
	    while (_p2 <= _p2Last) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4367
		_v = (byte1 * _p2[0]) + _pResult[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4368
		_pResult[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4369
		_v >>= 8; /* now _v contains the carry*/
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4370
		_pResult++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4371
		if (_v) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4372
		    unsigned char *_pResult1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4373
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4374
		    /* distribute carry */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4375
		    for (_pResult1 = _pResult; _v; _pResult1++) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4376
			_v += _pResult1[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4377
			_pResult1[0] = _v /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4378
			_v >>= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4379
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4380
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4381
		_p2++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4382
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4383
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4384
	ok = true;
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4385
    }
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4386
%}.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4387
    ok ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4388
	1 to:len1 do:[:index1 |
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4389
	    1 to:len2 do:[:index2 |
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4390
		dstIndex := index1 + index2 - 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4391
		prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4392
		prod := prod + (resultDigitByteArray basicAt:dstIndex).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4393
		resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4394
		carry := prod bitShift:-8.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4395
		carry ~~ 0 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4396
		    idx := dstIndex + 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4397
		    [carry ~~ 0] whileTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4398
			v := (resultDigitByteArray basicAt:idx) + carry.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4399
			resultDigitByteArray basicAt:idx put:(v bitAnd:255).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4400
			carry := v bitShift:-8.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4401
			idx := idx + 1
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4402
		    ]
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4403
		]
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4404
	    ]
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4405
	].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4406
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4407
    ^ result compressed
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4408
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4409
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4410
absPlus:aLargeInteger sign:newSign
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4411
    "return a LargeInteger representing abs(self) + abs(theArgument)
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4412
     with sign: newSign.
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4413
     The result is normalized. The argument must be a smallInteger
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4414
     with byteSize one less than the integer byteSize.
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4415
     This is a helper for addition and subtraction - not for public use."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4416
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4417
    |result done otherDigitByteArray resultDigitByteArray
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4418
     len1   "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4419
     len2   "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4420
     newLen "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4421
     index  "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4422
     carry  "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4423
     sum    "{ Class: SmallInteger }" |
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4424
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4425
    otherDigitByteArray := aLargeInteger digitBytes.
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4426
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4427
%{
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4428
    OBJ _digitByteArray = __INST(digitByteArray);
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4429
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4430
    if (__isByteArray(_digitByteArray)
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4431
     && __isByteArray(otherDigitByteArray)) {
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4432
        int _len1, _len2, _newLen;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4433
        unsigned char *_myDigits, *_otherDigits, *_newDigits;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4434
        int _index, _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4435
        int _comLen;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4436
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4437
        _len1 = __byteArraySize(_digitByteArray);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4438
        _len2 = __byteArraySize(otherDigitByteArray);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4439
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4440
        _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4441
        _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4442
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4443
        if (_len1 < _len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4444
            _comLen = _len1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4445
            _newLen = _len2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4446
            if (_otherDigits[_len2 - 1] == 0xFF) _newLen++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4447
        } else if (_len2 < _len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4448
            _comLen = _len2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4449
            _newLen = _len1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4450
            if (_myDigits[_len1 - 1] == 0xFF) _newLen++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4451
        } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4452
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4453
             * there can only be an overflow from the high bytes,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4454
             * if their sum is >= 255
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4455
             * (with sum==255, a carry could still occur from the next lower bytes)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4456
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4457
            _newLen = _len1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4458
            if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4459
                _newLen++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4460
            } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4461
                if (_newLen == sizeof(INT)) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4462
                    OBJ _uint;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4463
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4464
                    /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4465
                     * two word-sized numbers, no carry - a very common case ...
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4466
                     */
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4467
#if defined(__LSB_FIRST__)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4468
                    unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits;
4216
65f14da89fd4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4215
diff changeset
  4469
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4470
                    unsigned INT _sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aLargeInteger);
4216
65f14da89fd4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4215
diff changeset
  4471
#endif /* not LSB_FIRST */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4472
                    if (_sum <= _MAX_INT) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4473
                        _uint = __mkSmallInteger(_sum * __intVal(newSign));
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4474
                    } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4475
                        _uint = __MKULARGEINT(_sum);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4476
                        __LargeIntegerInstPtr(_uint)->l_sign = newSign;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4477
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4478
                    RETURN (_uint);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4479
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4480
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4481
            _comLen = _len1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4482
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4483
        resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4484
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4485
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4486
         * must refetch - GC could have been invoked
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4487
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4488
        _digitByteArray = __INST(digitByteArray);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4489
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4490
        _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4491
        _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4492
        _newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4493
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4494
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4495
         * add them ...
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4496
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4497
        _index = 1;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4498
        _carry = 0;
4172
3fb2b94e7535 tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents: 4171
diff changeset
  4499
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4500
#if defined(__LSBFIRST__)
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4501
# if (__POINTER_SIZE__ == 8) && defined(__GNUC__)
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4502
#  if 0  /* not faster (on alpha) */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4503
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4504
            int _comLen7;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4505
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4506
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4507
             * have a 64bit integers;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4508
             * add quad-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4509
             * accessing bytes at: [index-1][index][index+1]..[index+6]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4510
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4511
            _comLen7 = _comLen - 3 - 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4512
            while (_index <= _comLen7) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4513
                UINT64 _sum, _t1, _t2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4514
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4515
                asm ("addq   %5,%6,%1         /* sum */                  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4516
                      addq   %0,%1,%1         /* plus carryIn */         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4517
                      cmpult %1,%5,%2         /* was there a carry ? */  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4518
                      cmpult %1,%6,%3         /* was there a carry ? */  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4519
                      bis    %2,%3,%0         /* carryOut */             \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4520
                     "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4521
                        : "=r"  (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4522
                          "=r"  (_sum),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4523
                          "r"   (_t1),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4524
                          "r"   (_t2)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4525
                        : "r"   (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4526
                          "r"   (((unsigned long *)(&(_myDigits[_index - 1])))[0]),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4527
                          "r"   (((unsigned long *)(&(_otherDigits[_index - 1])))[0])
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4528
                    );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4529
                /* _sum = _sum & 0xFFFFFFFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4530
                ((unsigned long *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4531
                _index += 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4532
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4533
        }
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4534
#  endif
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4535
# endif /* 64bit */
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4536
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4537
# if (__POINTER_SIZE__ == 8)
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4538
# if 0  /* not faster (on alpha) */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4539
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4540
            int _comLen7;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4541
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4542
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4543
             * have a 64bit integers;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4544
             * add quad-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4545
             * accessing bytes at: [index-1][index][index+1]..[index+6]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4546
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4547
            _comLen7 = _comLen - 3 - 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4548
            while (_index <= _comLen7) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4549
                UINT64 _sum, _t1, _t2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4550
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4551
                _t1 = ((UINT64 *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4552
                _t2 = ((UINT64 *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4553
                _sum = _t1 + _t2 + _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4554
                ((UINT64 *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4555
                _carry = (_sum < _t1) | (_sum < _t2);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4556
                _index += 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4557
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4558
        }
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4559
#  endif
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4560
# endif /* 64bit */
4263
7f7caf39ebb4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  4561
7f7caf39ebb4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  4562
# ifdef UINT64
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4563
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4564
            int _comLen3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4565
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4566
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4567
             * have a 64bit integer type;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4568
             * add int-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4569
             * accessing bytes at: [index-1][index][index+1][index+2]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4570
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4571
            _comLen3 = _comLen - 3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4572
            while (_index <= _comLen3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4573
                UINT64 _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4574
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4575
                /* do not merge the 3 lines below into one -
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4576
                 * (will do sign extension then, which is wrong here)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4577
                 */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4578
                _sum = (unsigned)_carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4579
                _sum += ((unsigned int *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4580
                _sum += ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4581
                _carry = _sum >> 32;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4582
                /* _sum = _sum & 0xFFFFFFFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4583
                ((unsigned int *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4584
                _index += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4585
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4586
        }
4172
3fb2b94e7535 tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents: 4171
diff changeset
  4587
# else
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  4588
#  if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4589
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4590
            int _comLen3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4591
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4592
            _comLen3 = _comLen - 3 - 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4593
            while (_index <= _comLen3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4594
                unsigned int _sum, _sum2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4595
                unsigned int __in1A, __in1B, __in2A, __in2B;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4596
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4597
                __in1A = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4598
                __in2A = ((unsigned int *)(&(_myDigits[_index - 1])))[1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4599
                __in1B = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4600
                __in2B = ((unsigned int *)(&(_otherDigits[_index - 1])))[1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4601
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4602
                asm ("addl %%edx,%%eax  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4603
                      movl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4604
                      adcl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4605
                      addl %5,%%eax     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4606
                      adcl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4607
                                        \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4608
                      addl %%edx,%%ecx  \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4609
                      movl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4610
                      adcl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4611
                      addl %7,%%ecx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4612
                      adcl $0,%%edx     \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4613
                     "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4614
                        : "=d"  (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4615
                          "=a"  (_sum),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4616
                          "=c"  (_sum2)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4617
                        : "0"   (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4618
                          "1"   (__in1A),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4619
                          "rm"  (__in1B),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4620
                          "2"   (__in2A),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4621
                          "rm"  (__in2B)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4622
                    );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4623
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4624
                ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4625
                ((unsigned *)(&(_newDigits[_index - 1])))[1] = _sum2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4626
                _index += 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4627
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4628
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4629
             * add int-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4630
             * accessing bytes at: [index-1][index][index+1][index+2]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4631
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4632
            _comLen3 = _comLen3 + 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4633
            if (_index <= _comLen3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4634
                unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4635
                unsigned int __inA, __inB;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4636
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4637
                __inA = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4638
                __inB = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4639
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4640
                asm ("addl %%edx,%%eax      \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4641
                      movl $0,%%edx         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4642
                      adcl $0,%%edx         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4643
                      addl %4,%%eax         \n\
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4644
                      adcl $0,%%edx"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4645
                        : "=d"  (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4646
                          "=a"  (_sum)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4647
                        : "0"   (_carry),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4648
                          "1"   (__inA),
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4649
                          "rm"  (__inB)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4650
                    );
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4651
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4652
                ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4653
                _index += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4654
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4655
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4656
#  endif /* __i386__ && GNUC */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  4657
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4658
        {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4659
            int _comLen3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4660
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4661
            /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4662
             * add long-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4663
             * accessing bytes at: [index-1][index][index+1][index+2]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4664
             */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4665
            _comLen3 = _comLen - 3;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4666
            while (_index <= _comLen3) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4667
                unsigned int _sum, _v1, _v2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4668
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4669
                _v1 = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4670
                _v2 = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4671
                asm {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4672
                      mov eax, _v1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4673
                      add eax, _v2
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4674
                      mov edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4675
                      adc edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4676
                      add eax, _carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4677
                      adc edx, 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4678
                      mov _carry, edx
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4679
                      mov _sum, eax
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4680
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4681
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4682
                ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4683
                _index += 4;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4684
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4685
        }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4686
#  endif /* __i386__ && WIN32 */
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  4687
# endif /* INT64 */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4688
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4689
         * add short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4690
         * accessing bytes at: [index-1][index]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4691
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4692
        while (_index < _comLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4693
            unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4694
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4695
            _sum = _carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4696
                   + ((unsigned short *)(&(_myDigits[_index - 1])))[0]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4697
                   + ((unsigned short *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4698
            _carry = _sum >> 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4699
            /* _sum = _sum & 0xFFFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4700
            *(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4701
            _index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4702
        }
4284
20f47ad19322 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4283
diff changeset
  4703
#else
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4704
# ifdef __sparc__
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4705
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4706
         * add short-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4707
         * accessing bytes at: [index-1][index]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4708
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4709
        while (_index < _comLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4710
            unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4711
            unsigned short _v1, _v2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4712
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4713
            _v1 = ((unsigned short *)(&(_myDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4714
            _v2 = ((unsigned short *)(&(_otherDigits[_index - 1])))[0];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4715
            _sum = _carry + (_v1>>8) + (_v2>>8);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4716
            _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4717
            _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4718
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4719
            _sum = _carry + (_v1 & 0xFF) + (_v2 & 0xFF);
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4720
            _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4721
            _newDigits[_index] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4722
            _index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4723
        }
4284
20f47ad19322 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4283
diff changeset
  4724
# endif
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4725
#endif /* __LSBFIRST__ */
3119
65247b9d1201 oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents: 3099
diff changeset
  4726
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4727
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4728
         * add byte-wise
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4729
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4730
        while (_index <= _comLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4731
            unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4732
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4733
            _sum = _carry
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4734
                   + _myDigits[_index - 1]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4735
                   + _otherDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4736
            _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4737
            /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4738
            _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4739
            _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4740
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4741
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4742
        /*
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4743
         * rest
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4744
         */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4745
        if (_len1 > _len2) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4746
#if defined(__LSBFIRST__)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4747
            if (_index <= _len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4748
                if ((_index - 1) & 1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4749
                    /* odd byte */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4750
                    unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4751
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4752
                    _sum = _carry + _myDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4753
                    _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4754
                    /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4755
                    _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4756
                    _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4757
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4758
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4759
                while (_index < _len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4760
                    /* shorts */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4761
                    unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4762
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4763
                    _sum = _carry + *(unsigned short *)(&(_myDigits[_index - 1]));
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4764
                    _carry = _sum >> 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4765
                    /* _sum = _sum & 0xFFFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4766
                    *(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4767
                    _index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4768
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4769
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4770
                if (_index <= _len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4771
                    /* last byte */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4772
                    unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4773
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4774
                    _sum = _carry + _myDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4775
                    _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4776
                    /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4777
                    _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4778
                    _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4779
                }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4780
            }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4781
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4782
            while (_index <= _len1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4783
                unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4784
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4785
                _sum = _carry + _myDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4786
                _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4787
                /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4788
                _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4789
                _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4790
            }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4791
#endif /* not LSB */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4792
        } else {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4793
            if (_len2 > _len1) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4794
#if defined(__LSBFIRST__)
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4795
                if (_index <= _len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4796
                    if ((_index - 1) & 1) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4797
                        /* odd byte */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4798
                        unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4799
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4800
                        _sum = _carry + _otherDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4801
                        _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4802
                        /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4803
                        _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4804
                        _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4805
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4806
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4807
                    while (_index < _len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4808
                        /* shorts */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4809
                        unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4810
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4811
                        _sum = _carry + *(unsigned short *)(&(_otherDigits[_index - 1]));
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4812
                        _carry = _sum >> 16;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4813
                        /* _sum = _sum & 0xFFFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4814
                        *(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4815
                        _index += 2;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4816
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4817
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4818
                    if (_index <= _len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4819
                        /* last byte */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4820
                        unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4821
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4822
                        _sum = _carry + _otherDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4823
                        _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4824
                        /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4825
                        _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4826
                        _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4827
                    }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4828
                }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4829
#else
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4830
                while (_index <= _len2) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4831
                    unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4832
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4833
                    _sum = _carry + _otherDigits[_index - 1];
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4834
                    _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4835
                    /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4836
                    _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4837
                    _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4838
                }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4839
#endif /* not LSB */
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4840
            }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4841
        }
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4842
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4843
        while (_index <= _newLen) {
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4844
            unsigned int _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4845
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4846
            _sum = _carry;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4847
            _carry = _sum >> 8;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4848
            /* _sum = _sum & 0xFF; */
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4849
            _newDigits[_index - 1] = _sum;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4850
            _index++;
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4851
        }
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4852
    }
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4853
%}.
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4854
    resultDigitByteArray notNil ifTrue:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4855
        result := self class basicNew.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4856
        result setDigits:resultDigitByteArray.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4857
        result setSign:newSign.
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4858
    ] ifFalse:[
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4859
        len1 := digitByteArray size.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4860
        len2 := otherDigitByteArray size.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4861
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4862
        "/ earlier versions estimated the newLength as:
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4863
        "/ (len1 max:len2) + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4864
        "/ and reduced the result.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4865
        "/ however, if one of the addends is smaller,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4866
        "/ the result will never require another digit,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4867
        "/ if the highest digit of the larger addent is
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4868
        "/ not equal to 255. Therefore, in most cases,
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4869
        "/ we can avoid the computation and resizing
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4870
        "/ in #reduced.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4871
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4872
        len1 < len2 ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4873
            newLen := len2.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4874
            (otherDigitByteArray at:len2) == 16rFF ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4875
                newLen := newLen + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4876
            ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4877
        ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4878
            len2 < len1 ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4879
                newLen := len1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4880
                (digitByteArray at:len1) == 16rFF ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4881
                    newLen := newLen + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4882
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4883
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4884
                newLen := len1 + 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4885
            ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4886
        ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4887
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4888
        result := self class basicNew numberOfDigits:newLen.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4889
        result sign:newSign.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4890
        resultDigitByteArray := result digitBytes.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4891
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4892
        index := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4893
        carry := 0.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4894
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4895
        done := false.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4896
        [done] whileFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4897
            sum := carry.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4898
            (index <= len1) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4899
                sum := sum + (digitByteArray basicAt:index).
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4900
                (index <= len2) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4901
                    sum := sum + (otherDigitByteArray basicAt:index)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4902
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4903
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4904
                (index <= len2) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4905
                    sum := sum + (otherDigitByteArray basicAt:index)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4906
                ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4907
                    "end reached"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4908
                    done := true
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4909
                ]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4910
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4911
            (sum >= 16r100) ifTrue:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4912
                carry := 1.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4913
                sum := sum - 16r100
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4914
            ] ifFalse:[
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4915
                carry := 0
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4916
            ].
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4917
            resultDigitByteArray basicAt:index put:sum.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4918
            index := index + 1
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  4919
        ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4920
    ].
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4921
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4922
    ^ result compressed
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4923
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4924
    "Modified: 11.8.1997 / 03:23:37 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4925
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4926
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4927
absSubtract:aLargeInteger
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  4928
    "private helper for division:
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4929
	destructively subtract aLargeInteger from myself
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4930
	AND return true, if the result is non-zero, false otherwise.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4931
	(i.e. this method has both a return value and a side-effect
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4932
	 on the receiver)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4933
	Only allowed for positive receiver and argument
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4934
	The receiver must be >= the argument.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4935
	The receiver must be a temporary scratch-number"
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4936
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4937
    |otherDigitByteArray
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4938
     len1   "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4939
     len2   "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4940
     index  "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4941
     borrow "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4942
     diff   "{ Class: SmallInteger }"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4943
     notZero
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4944
    |
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4945
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4946
    notZero := false.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4947
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4948
    otherDigitByteArray := aLargeInteger digitBytes.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4949
    len2 := otherDigitByteArray size.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4950
    len2 > len1 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4951
	[(otherDigitByteArray at:len2) == 0] whileTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4952
	    len2 := len2 - 1
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4953
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4954
	len2 > len1 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4955
	    self error:'operation failed' "/ may not be called that way
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4956
	].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  4957
    ].
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4958
    "/ knowing that len2 is <= len1
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4959
%{
4189
Claus Gittinger <cg@exept.de>
parents: 4188
diff changeset
  4960
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4961
    OBJ _digitByteArray = __INST(digitByteArray);
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4962
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4963
    if (__isByteArray(_digitByteArray)
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  4964
     && __isByteArray(otherDigitByteArray)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4965
	int _len1 = __intVal(len1),
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4966
	    _len2 = __intVal(len2);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4967
	unsigned char *_myDigits, *_otherDigits;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4968
	int _index = 1, _borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4969
	INT _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4970
	int anyBitNonZero = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4971
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4972
	_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4973
	_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  4974
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4975
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4976
# if __POINTER_SIZE__ == 8
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4977
	{
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4978
	    int _len2Q;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4979
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4980
	     * subtract int-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4981
	     */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4982
	    _len2Q = _len2-2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4983
	    while (_index < _len2Q) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4984
		/* do not combine the expression below (may lead to unsigned result on some machines */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4985
		_diff = ((unsigned int *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4986
		_diff -= ((unsigned int *)(_otherDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4987
		_diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4988
		if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4989
		    _borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4990
		} else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4991
		    _borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4992
		    /* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4993
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4994
		((unsigned int *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4995
		anyBitNonZero |= (_diff & 0xFFFFFFFFL);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4996
		_index += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4997
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  4998
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4999
# endif
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  5000
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5001
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5002
	 * subtract short-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5003
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5004
	while (_index < _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5005
	    /* do not combine the expression below (may lead to unsigned result on some machines */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5006
	    _diff = ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5007
	    _diff -= ((unsigned short *)(_otherDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5008
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5009
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5010
		_borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5011
	    } else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5012
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5013
		/* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5014
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5015
	    ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5016
	    anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5017
	    _index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5018
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5019
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5020
	if (_index <= _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5021
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5022
	     * cannot continue with shorts - there is an odd number of
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5023
	     * bytes in the minuent
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5024
	     */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5025
	} else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5026
	    while (_index < _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5027
		/* do not combine the expression below (may lead to unsigned result on some machines */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5028
		_diff = ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5029
		_diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5030
		if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5031
		    /* _borrow = 0; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5032
		    ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5033
		    anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5034
		    _index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5035
		    while (_index < _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5036
			anyBitNonZero |= ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5037
			if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5038
			    RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5039
			}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5040
			_index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5041
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5042
		    /* last odd index */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5043
		    if (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5044
			anyBitNonZero |= _myDigits[_index - 1];;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5045
			if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5046
			    RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5047
			}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5048
			_index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5049
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5050
		    RETURN (anyBitNonZero ? true : false);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5051
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5052
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5053
		/* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5054
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5055
		((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5056
		anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5057
		_index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5058
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5059
	}
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5060
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5061
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5062
	 * subtract byte-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5063
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5064
	while (_index <= _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5065
	    /* do not combine the expression below (may lead to unsigned result on some machines */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5066
	    _diff = _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5067
	    _diff -= _otherDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5068
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5069
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5070
		_borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5071
	    } else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5072
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5073
		/* _diff += 0x100; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5074
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5075
	    _myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5076
	    anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5077
	    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5078
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5079
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5080
	while (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5081
	    /* do not combine the expression below (may lead to unsigned result on some machines */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5082
	    _diff = _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5083
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5084
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5085
		/* _borrow = 0; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5086
		_myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5087
		anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5088
		_index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5089
		while (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5090
		    anyBitNonZero |= _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5091
		    if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5092
			RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5093
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5094
		    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5095
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5096
		break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5097
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5098
	    _borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5099
	    /* _diff += 0x100; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5100
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5101
	    _myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5102
	    anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5103
	    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5104
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5105
	RETURN (anyBitNonZero ? true : false);
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5106
    }
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5107
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5108
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5109
    index := 1.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5110
    borrow := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5111
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5112
    [index <= len1] whileTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5113
	diff := borrow.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5114
	diff := diff + (digitByteArray basicAt:index).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5115
	index <= len2 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5116
	    diff := diff - (otherDigitByteArray basicAt:index).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5117
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5118
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5119
	"/ workaround for
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5120
	"/ gcc code generator bug
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5121
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5122
	(diff >= 0) ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5123
	    borrow := 0
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5124
	] ifFalse:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5125
	    borrow := -1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5126
	    diff := diff + 16r100
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5127
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5128
	diff ~~ 0 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5129
	    notZero := true
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5130
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5131
	digitByteArray basicAt:index put:diff.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5132
	index := index + 1
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5133
    ].
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5134
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5135
    ^ notZero
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5136
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5137
    "Created: / 5.11.1996 / 16:23:47 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5138
    "Modified: / 5.11.1996 / 18:56:50 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5139
    "Modified: / 27.4.1999 / 18:08:23 / stefan"
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5140
!
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5141
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5142
div2
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5143
    "private helper for division:
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5144
       destructively divide the receiver by 2.
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5145
       may leave the receiver unnormalized (i.e. with a leftover 0 high-byte)"
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5146
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5147
    |prevBit|
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5148
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5149
%{  /* NOCONTEXT */
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5150
    OBJ __digits = __INST(digitByteArray);
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5151
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5152
    if (__isByteArray(__digits)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5153
	int __nBytes = __byteArraySize(__digits);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5154
	unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5155
	unsigned INT __this, __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5156
	int __idx;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5157
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5158
	if (__nBytes == 1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5159
	    __bp[0] >>= 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5160
	    RETURN (self);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5161
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5162
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5163
	__idx = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5164
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5165
#if defined(__LSBFIRST__)
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5166
# if (__POINTER_SIZE__ == 8)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5167
	if (sizeof(unsigned INT) == 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5168
	    int __endIndex = __nBytes - 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5169
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5170
	    if (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5171
		__this = ((unsigned INT *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5172
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5173
		while (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5174
		    __next = ((unsigned INT *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5175
		    __this = (__this >> 1) /* & 0x7FFFFFFFFFFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5176
		    __this |= __next << 63;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5177
		    ((unsigned INT *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5178
		    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5179
		    __bp += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5180
		    __idx += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5181
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5182
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5183
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5184
	    if (__idx < (__nBytes - 4)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5185
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5186
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5187
		__next = ((unsigned int *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5188
		__this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5189
		__this |= __next << 31;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5190
		((unsigned int *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5191
		__this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5192
		__bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5193
		__idx += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5194
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5195
	    if (__idx < (__nBytes - 2)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5196
		__this = ((unsigned short *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5197
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5198
		__next = ((unsigned short *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5199
		__this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5200
		__this |= __next << 15;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5201
		((unsigned short *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5202
		__this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5203
		__bp += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5204
		__idx += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5205
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5206
	}
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5207
# else
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5208
	if (sizeof(unsigned int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5209
	    int __endIndex = __nBytes - 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5210
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5211
	    if (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5212
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5213
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5214
		while (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5215
		    __next = ((unsigned int *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5216
		    __this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5217
		    __this |= __next << 31;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5218
		    ((unsigned int *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5219
		    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5220
		    __bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5221
		    __idx += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5222
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5223
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5224
	}
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5225
# endif
3119
65247b9d1201 oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents: 3099
diff changeset
  5226
#endif
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5227
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5228
	__this = __bp[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5229
	while (__idx < __nBytes) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5230
	    __next = __bp[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5231
	    __this >>= 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5232
	    __this |= __next << 7;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5233
	    __bp[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5234
	    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5235
	    __bp++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5236
	    __idx++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5237
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5238
	__bp[0] = __this >> 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5239
	RETURN (self);
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5240
    }
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5241
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5242
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5243
    prevBit := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5244
    digitByteArray size to:1 by:-1 do:[:idx |
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5245
	|thisByte|
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5246
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5247
	thisByte := digitByteArray at:idx.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5248
	digitByteArray at:idx put:((thisByte bitShift:-1) bitOr:prevBit).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5249
	prevBit := (thisByte bitAnd:1) bitShift:7.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5250
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5251
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5252
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5253
     100000 asLargeInteger div2
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5254
     1000000000000000000000000000 div2
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5255
     10000000000000000000000000000000000000000000 div2
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5256
    "
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5257
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5258
    "Modified: 5.11.1996 / 16:12:56 / cg"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5259
!
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5260
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5261
mul2
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5262
    "private helper for division:
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5263
       destructively multiply the receiver by 2."
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5264
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5265
    |nBytes "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5266
     b      "{ Class: SmallInteger }"
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5267
     t prevBit|
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5268
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5269
    nBytes := digitByteArray size.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5270
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5271
    b := digitByteArray at:nBytes.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5272
    (b bitAnd:16r80) ~~ 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5273
	"/ need another byte
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5274
	nBytes := nBytes + 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5275
	t := ByteArray uninitializedNew:nBytes.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5276
	t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5277
	t at:nBytes put:0.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5278
	digitByteArray := t.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5279
    ].
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5280
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5281
%{
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5282
    OBJ __digits = __INST(digitByteArray);
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5283
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5284
    if (__isByteArray(__digits)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5285
	int __nBytes = __intVal(nBytes);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5286
	unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5287
	unsigned INT __carry = 0, __newCarry;
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5288
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5289
#if defined(__LSBFIRST__)
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5290
# if (__POINTER_SIZE__ == 8)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5291
	if (sizeof(unsigned INT) == 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5292
	    while (__nBytes >= 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5293
		unsigned INT __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5294
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5295
		__this = ((unsigned INT *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5296
		__newCarry = (__this >> 63) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5297
		((unsigned INT *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5298
		__carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5299
		__bp += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5300
		__nBytes -= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5301
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5302
	}
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5303
# endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5304
	if (sizeof(unsigned int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5305
	    while (__nBytes >= 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5306
		unsigned int __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5307
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5308
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5309
		__newCarry = (__this >> 31) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5310
		((unsigned int *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5311
		__carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5312
		__bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5313
		__nBytes -= 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5314
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5315
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5316
	if (__nBytes >= 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5317
	    unsigned short __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5318
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5319
	    __this = ((unsigned short *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5320
	    __newCarry = (__this >> 15) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5321
	    ((unsigned short *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5322
	    __carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5323
	    __bp += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5324
	    __nBytes -= 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5325
	}
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5326
#endif /* LSBFIRST */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5327
	while (__nBytes) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5328
	    unsigned char __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5329
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5330
	    __this = __bp[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5331
	    __newCarry = (__this >> 7) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5332
	    __bp[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5333
	    __carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5334
	    __bp++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5335
	    __nBytes--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5336
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5337
	RETURN (self);
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5338
    }
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5339
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5340
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5341
    prevBit := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5342
    1 to:digitByteArray size do:[:idx |
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5343
	|thisByte|
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5344
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5345
	thisByte := digitByteArray at:idx.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5346
	digitByteArray at:idx put:(((thisByte bitShift:1) bitAnd:16rFF) bitOr:prevBit).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5347
	prevBit := (thisByte bitShift:-7) bitAnd:1.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5348
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5349
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5350
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5351
     100000 asLargeInteger mul2
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5352
     16r7FFFFFFFFFFF copy mul2 hexPrintString
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5353
     16rFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5354
     16r7FFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5355
     16rFFFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5356
     10000000000000000000000000000 mul2
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5357
    "
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5358
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5359
    "Modified: 5.11.1996 / 16:37:32 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5360
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5361
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5362
mul256
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5363
    "private helper for division:
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5364
       destructively multiply the receiver by 256."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5365
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5366
    |newDigits newSize|
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5367
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5368
    newSize := digitByteArray size + 1.
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5369
    newDigits := ByteArray uninitializedNew:newSize.
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5370
    newDigits replaceBytesFrom:2 to:newSize with:digitByteArray startingAt:1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5371
    newDigits at:1 put:0.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5372
    digitByteArray := newDigits
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5373
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5374
    "Modified: / 20.5.1999 / 09:16:42 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5375
!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5376
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5377
numberOfDigits:nDigits
2001
8c2b957f0cb3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1983
diff changeset
  5378
"/    digitByteArray := ByteArray uninitializedNew:nDigits.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5379
    digitByteArray := ByteArray new:nDigits.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5380
    sign := 1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5381
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5382
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5383
numberOfDigits:nDigits sign:newSign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5384
"/    digitByteArray := ByteArray uninitializedNew:nDigits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5385
    digitByteArray := ByteArray new:nDigits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5386
    sign := newSign.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5387
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5388
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5389
setDigits:digits
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5390
    digitByteArray := digits.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5391
    sign := 1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5392
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5393
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5394
setDigits:digits sign:newSign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5395
    digitByteArray := digits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5396
    sign := newSign.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5397
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5398
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5399
setSign:aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5400
    "destructively change the sign of the receiver"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5401
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5402
    sign := aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5403
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5404
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5405
sign:aNumber
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5406
    <resource: #obsolete>
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5407
    "destructively change the sign of the receiver"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5408
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5409
    ^ self setSign:aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5410
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5411
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5412
!LargeInteger methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5413
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5414
even
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5415
    "return true if the receiver is even"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5416
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5417
    ^ (digitByteArray at:1) even
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5418
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5419
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5420
negative
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5421
    "return true, if the receiver is < 0"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5422
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5423
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5424
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5425
    return context._RETURN( ((STLargeInteger)self).largeValue.signum() < 0 ? STObject.True : STObject.False);
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5426
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5427
%}.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5428
    ^ (sign < 0)
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5429
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5430
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5431
odd
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5432
    "return true if the receiver is odd"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5433
1983
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5434
    ^ (digitByteArray at:1) odd
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5435
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5436
    "Modified: 18.11.1996 / 22:19:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5437
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5438
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5439
positive
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5440
    "return true, if the receiver is >= 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5441
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5442
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5443
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5444
    return context._RETURN( ((STLargeInteger)self).largeValue.signum() >= 0 ? STObject.True : STObject.False);
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5445
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5446
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5447
    ^ (sign >= 0)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5448
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5449
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5450
sign
7471
c5d4bd612d9f comments
Claus Gittinger <cg@exept.de>
parents: 7446
diff changeset
  5451
    "return the sign of the receiver (-1, 0 or 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5452
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5453
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5454
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5455
    return context._RETURN( STInteger._new( ((STLargeInteger)self).largeValue.signum() ));
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5456
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5457
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5458
    ^ sign
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5459
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5460
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5461
strictlyPositive
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5462
    "return true, if the receiver is > 0"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5463
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5464
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5465
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5466
    return context._RETURN( ((STLargeInteger)self).largeValue.signum() > 0 ? STObject.True : STObject.False);
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5467
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5468
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5469
    ^ (sign > 0)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5470
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5471
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  5472
!LargeInteger class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5473
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5474
version
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5475
    ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.226 2015-05-20 16:01:28 cg Exp $'
12121
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5476
!
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5477
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5478
version_CVS
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5479
    ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.226 2015-05-20 16:01:28 cg Exp $'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5480
! !
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5481