LargeInteger.st
author Claus Gittinger <cg@exept.de>
Mon, 01 Feb 2016 15:24:23 +0100
changeset 19132 0c0046873268
parent 19126 98adb7dc1950
child 19135 e2d82660b855
permissions -rw-r--r--
#FEATURE class: LargeInteger changed: #absLess: tuned with inline c code #byteSwapped32 #byteSwapped64 #productFromInteger: tuned for 64bit machines
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
345
claus
parents: 250
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
5382
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
    12
"{ Package: 'stx:libbasic' }"
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
    13
17648
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
    14
"{ NameSpace: Smalltalk }"
8d017b41a3c3 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17246
diff changeset
    15
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    16
Integer subclass:#LargeInteger
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    17
	instanceVariableNames:'sign digitByteArray'
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
    18
	classVariableNames:'UseKarazuba'
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    19
	poolDictionaries:''
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    20
	category:'Magnitude-Numbers'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
    23
!LargeInteger class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    25
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    26
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    27
 COPYRIGHT (c) 1994 by Claus Gittinger
345
claus
parents: 250
diff changeset
    28
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    39
documentation
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    40
"
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    41
    This class provides arbitrary precision integers.
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    42
    These are represented as:
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    43
      sign (-1/0/+1) and, if sign ~~ 0
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    44
      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
    45
      least significant 8 bits at index 1...
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    46
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
    47
    The implementation is not completely tuned for high performance
4264
9fd0385c26d0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4263
diff changeset
    48
    (more key-methods should be rewritten as primitives), although the
9fd0385c26d0 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4263
diff changeset
    49
    common operations have been pretty tuned for some architectures.
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    50
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    51
    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
    52
    SmallInteger arithmetic (overflowing the SmallInteger range).
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    53
    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
    54
    SmallIntegers, when possible (see #compressed).
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    55
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    56
    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
    57
    which keeps the sign as an instance variable
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    58
    (ST-80 has LargePositiveInteger and LargeNegativeInteger).
17128
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    59
    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
    60
    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
    61
    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
    62
    128 bit numbers (which make up almost all instances in practice).
ec053282f87c class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17035
diff changeset
    63
    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
    64
    change in the future.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    65
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1264
diff changeset
    66
    [author:]
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    67
	Claus Gittinger
1556
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1504
diff changeset
    68
134d96466f5a commentary
Claus Gittinger <cg@exept.de>
parents: 1504
diff changeset
    69
    [see also:]
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    70
	Number
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    71
	Float Fraction FixedPoint
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
    72
	SmallInteger
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
    73
"
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    74
!
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    75
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    76
testing
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    77
"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
    78
   test largeInt multiplication & division
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    79
   (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
    80
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    81
   |last v|
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    82
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    83
   v := last := 1.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    84
   2 to:3000 do:[:i |
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    85
       i printCR.
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    86
       v := v * i.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    87
       (v / i) ~= last ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
    88
	   self halt
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    89
       ].
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    90
       last := v.
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
    91
   ]
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    92
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    93
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    94
   test addition, subtraction:
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
   SmallInteger maxVal                -> 1073741823
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    97
   SmallInteger maxVal + 1            -> 1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    98
   SmallInteger maxVal + 2            -> 1073741825
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
    99
   (SmallInteger maxVal + 2) - 2      -> 1073741823
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   100
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   101
   SmallInteger minVal                -> -1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   102
   SmallInteger minVal - 1            -> -1073741825
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   103
   SmallInteger minVal - 2            -> -1073741826
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   104
   (SmallInteger minVal - 2) + 2      -> -1073741824
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   105
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   106
   1234567890 + 10                    -> 1234567900
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   107
   1111111111 + 1111111111            -> 2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   108
   1111111111 - 1111111111            -> 0
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   109
   1111111111 - 2222222222            -> -1111111111
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   110
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   111
   1111111111 * 2                     -> 2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   112
   1111111111 * -2                    -> -2222222222
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   113
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   114
   1111111111 * 1111111111            -> 1234567900987654321
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   115
   1234567900987654321 // 1111111111  -> 1111111111
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   116
   1234567900987654321 \\ 1111111111  -> 0
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   117
   1234567900987654322 \\ 1111111111  -> 1
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   118
   1234567900987654421 \\ 1111111111  -> 100
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   119
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   120
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   121
   addition:
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   122
   (16rFFFFFFF0 + 1          ) hexPrintString -> 'FFFFFFF1'
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   123
   (16rFFFFFFFF + 1          ) hexPrintString -> '100000000'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   124
   (16rFFFFFFFF + 2          ) hexPrintString -> '100000001'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   125
   (16rFFFFFFFF + 16rFFFFFF  ) hexPrintString -> '100FFFFFE'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   126
   (16rFFFFFFFF + 16rFFFFFFFF) hexPrintString -> '1FFFFFFFE'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   127
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   128
   20 factorial         -> 2432902008176640000
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   129
   20 factorial + 10000 -> 2432902008176650000
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   130
   20 factorial               hexPrintString -> '21C3677C82B40000'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   131
   (20 factorial + 16rFFFF)   hexPrintString -> '21C3677C82B4FFFF'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   132
   (20 factorial + 16rFFFFFF) hexPrintString -> '21C3677C83B3FFFF'
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
   133
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   134
   test comparison:
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   135
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   136
   -1234567890 >  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   137
   -1234567890 >= -1234567890         true
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         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   141
   -1234567890 ~= -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   142
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   143
   -1234567890 >  -1234567891         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   144
   -1234567890 >= -1234567891         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   145
   -1234567890 <  -1234567891         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   146
   -1234567890 <= -1234567891         false
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         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   149
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   150
   -1234567891 >  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   151
   -1234567891 >= -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   152
   -1234567891 <  -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   153
   -1234567891 <= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   154
   -1234567891 =  -1234567890         false
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
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   157
    1234567890 >  -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   158
    1234567890 >= -1234567890         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   159
    1234567890 <  -1234567890         false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   160
    1234567890 <= -1234567890         false
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         true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   163
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   164
   -1234567890 >  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   165
   -1234567890 >= 1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   166
   -1234567890 <  1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   167
   -1234567890 <= 1234567890          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   168
   -1234567890 =  1234567890          false
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
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   171
    1234567890 >  1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   172
    1234567890 >= 1234567890          true
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          true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   176
    1234567890 ~= 1234567890          false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   177
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   178
   -1234567890 >  -10                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   179
   -1234567890 >= -10                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   180
   -1234567890 <  -10                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   181
   -1234567890 <= -10                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   182
   -1234567890 =  -10                 false
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
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   185
   -1234567890 >  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   186
   -1234567890 >= 10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   187
   -1234567890 <  10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   188
   -1234567890 <= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   189
   -1234567890 =  10                  false
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
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   192
   -10 >  -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   193
   -10 >= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   194
   -10 <  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   195
   -10 <= -1234567890                 false
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                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   198
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
    10 >= -1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   201
    10 <  -1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   202
    10 <= -1234567890                 false
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                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   205
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   206
    1234567890 >  -10                 true
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   207
    1234567890 >= -10                 true
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   208
    1234567890 <  -10                 false
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   209
    1234567890 <= -10                 false
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                 true
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   212
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   213
    1234567890 >  10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   214
    1234567890 >= 10                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   215
    1234567890 <  10                  false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   216
    1234567890 <= 10                  false
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                  true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   219
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   220
   -10 >   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   221
   -10 >=  1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   222
   -10 <   1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   223
   -10 <=  1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   224
   -10 =   1234567890                 false
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
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   227
    10 >   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   228
    10 >=  1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   229
    10 <   1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   230
    10 <=  1234567890                 true
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   231
    10 =   1234567890                 false
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
   232
    10 ~=  1234567890                 true
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
   233
"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   234
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   235
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   236
!LargeInteger class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   237
4794
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   238
digitBytes:aByteArrayOfDigits
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   239
    "create and return a new LargeInteger with digits (lsb-first)
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   240
     from the argument, aByteArray.
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   241
     Experimental interface - May change/be removed without notice."
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   242
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   243
    ^ self basicNew setDigits:aByteArrayOfDigits
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
    "
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   246
     LargeInteger digitBytes:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
4806
1b48e7420cfe comment
Claus Gittinger <cg@exept.de>
parents: 4794
diff changeset
   247
     (LargeInteger digitBytes:#[16r12 16r34 16r56 16r78 16r90]) hexPrintString
4794
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   248
    "
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   249
19e05d7c2d3b new instance creation: #digitBytes:
Claus Gittinger <cg@exept.de>
parents: 4663
diff changeset
   250
    "Modified: / 8.5.1998 / 21:40:41 / cg"
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
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   253
digitBytes:aByteArrayOfDigits MSB:msb
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   254
    "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
   255
     from the argument, aByteArray."
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   256
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   257
    |digits|
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   258
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   259
    msb == false ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   260
	digits := aByteArrayOfDigits
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   261
    ] ifFalse:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   262
	digits := aByteArrayOfDigits reversed
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   263
    ].
6106
daf0e9692237 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6067
diff changeset
   264
    ^ self basicNew setDigits:digits
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   265
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   266
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   267
     (LargeInteger digitBytes:#[16r10 16r20 16r30 16r00] MSB:false) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   268
     (LargeInteger digitBytes:#[16r10 16r20 16r30 16r00] MSB:true) hexPrintString
5020
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   269
    "
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   270
!
eff43341e72c added #digitBytes:MSB:
ca
parents: 5014
diff changeset
   271
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   272
digitBytes:aByteArrayOfDigits sign:sign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   273
    "create and return a new LargeInteger with digits (lsb-first)
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   274
     from the argument, aByteArray.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   275
     Experimental interface - May change/be removed without notice."
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   276
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   277
    ^ self basicNew setDigits:aByteArrayOfDigits sign:sign
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
    "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   280
     LargeInteger digitBytes:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   281
     (LargeInteger digitBytes:#[16r12 16r34 16r56 16r78 16r90]) hexPrintString
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   282
    "
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   283
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   284
    "Modified: / 8.5.1998 / 21:40:41 / cg"
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
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   287
new
4299
2028db93d182 comment
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
   288
    "catch creation message.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   289
     LargeIntegers are only created by system code, which
4299
2028db93d182 comment
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
   290
     uses basicNew and cares for correct initialization."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
a27a279701f8 Initial revision
claus
parents:
diff changeset
   292
    self error:'LargeIntegers cannot be created with new'
a27a279701f8 Initial revision
claus
parents:
diff changeset
   293
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   295
new:numberOfDigits
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   296
    "catch creation message"
2
claus
parents: 1
diff changeset
   297
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
    self error:'LargeIntegers cannot be created with new'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   299
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
value:aSmallInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   302
    "create and return a new LargeInteger with value taken from
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   303
     the argument, aSmallInteger.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   304
     Notice:
4162
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   305
	this should be only used internally, since such small
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   306
	largeIntegers do not normally occur in the system.
0e19153c1481 slightly tuned absMul:
Claus Gittinger <cg@exept.de>
parents: 4160
diff changeset
   307
	(They are used by myself)
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
   308
     May change/be removed without notice."
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
   309
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
    ^ self basicNew value:aSmallInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   311
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
    "LargeInteger value:3689"
3438
2e64ef848871 oops - last change was not good
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   313
2e64ef848871 oops - last change was not good
Claus Gittinger <cg@exept.de>
parents: 3431
diff changeset
   314
    "Modified: / 8.5.1998 / 21:40:41 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   315
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   316
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   317
!LargeInteger class methodsFor:'queries'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   319
isBuiltInClass
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   320
    "return true if this class is known by the run-time-system.
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   321
     Here, true is returned."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   322
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
    ^ true
1264
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   324
8d916aa63bce commentary
Claus Gittinger <cg@exept.de>
parents: 1210
diff changeset
   325
    "Modified: 23.4.1996 / 15:59:21 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   326
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   327
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   328
!LargeInteger methodsFor:'arithmetic'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   329
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
* aNumber
11731
f3adbab61060 comment
Claus Gittinger <cg@exept.de>
parents: 11723
diff changeset
   331
    "return the product of the receiver and the argument."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   333
    |otherSign numberClass|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   335
    (sign == 0) ifTrue:[^ 0].  "cannot happen if correctly normalized"
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
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
     this is the common case, multiplying with SmallInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
     Use a special method for this case ...
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   341
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   342
	^ self productFromInteger:aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   343
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   344
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
     if the argument is not a largeInteger, coerce
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   348
    (numberClass == self class) ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   349
	^ self retry:#* coercing:aNumber
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   350
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   351
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   352
    otherSign := aNumber sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
    (sign == otherSign) ifTrue:[^ self absMul:aNumber].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
    (otherSign == 0) ifTrue:[^ 0].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   355
    ^ (self absMul:aNumber) setSign:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   356
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   357
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   358
+ aNumber
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   359
    "return the sum of the receiver and the argument, aNumber"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   360
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   361
    |otherSign numberClass|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   362
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   363
    (sign == 0) ifTrue:[^ aNumber].  "cannot happen if correctly normalized"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   364
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   365
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   366
     this is the common case, adding a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   367
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   368
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   369
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   370
	^ self sumFromInteger:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   371
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   372
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   373
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   374
     if the argument is not a largeInteger, coerce
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   375
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   376
    (numberClass == self class) ifFalse:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   377
	^ self retry:#+ coercing:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   378
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   379
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   380
    otherSign := aNumber sign.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   381
    (sign > 0) ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   382
	"I am positive"
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   383
	(otherSign > 0) ifTrue:[^ self absPlus:aNumber sign:1].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   384
	(otherSign < 0) ifTrue:[^ self absMinus:aNumber sign:1].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
   385
	^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   386
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   387
    "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
   388
    (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
   389
    (otherSign < 0) ifTrue:[^ self absPlus:aNumber sign:-1].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   390
    ^ self
357
claus
parents: 345
diff changeset
   391
claus
parents: 345
diff changeset
   392
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   393
     SmallInteger maxVal
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   394
     SmallInteger maxVal + 1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   395
     SmallInteger maxVal + 2
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   396
     SmallInteger minVal
357
claus
parents: 345
diff changeset
   397
     SmallInteger minVal - 1
claus
parents: 345
diff changeset
   398
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   399
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   400
    "Modified: / 9.1.1998 / 13:26:28 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   401
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   402
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   403
- aNumber
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   404
    "return the difference of the receiver and the argument, aNumber"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   405
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   406
    |otherSign numberClass|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   407
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   408
    (sign == 0) ifTrue:[^ aNumber negated].     "cannot happen if correctly normalized"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   409
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   410
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   411
     this is the common case, subtracting a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   412
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   413
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   414
    ((numberClass := aNumber class) == SmallInteger) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   415
	sign > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   416
	    aNumber > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   417
		^ self absFastMinus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   418
	    ].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   419
	    ^ self absFastPlus: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
	aNumber > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   422
	    ^ self absFastPlus:aNumber sign:-1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   423
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   424
	^ self absFastMinus:aNumber sign:-1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   425
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   426
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   427
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   428
     if the argument is not a largeInteger, coerce
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   429
    "
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
   430
    (numberClass == self class) ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   431
	^ self retry:#- coercing:aNumber
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   432
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   433
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   434
    otherSign := aNumber sign.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   435
    (sign > 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   436
	"I am positive"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   437
	(otherSign > 0) ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   438
	    "+large - +large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   439
	    ^ self absMinus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   440
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   441
	(otherSign < 0) ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   442
	    "+large - -large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   443
	    ^ self absPlus:aNumber sign:1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   444
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   445
	"should not happen"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   446
	^ self
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   447
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   448
    "I am negative"
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   449
    (otherSign > 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   450
	"-large - +large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   451
	^ self absPlus:aNumber sign:-1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   452
    ].
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   453
    (otherSign < 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   454
	"-large - -large"
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
   455
	^ self absMinus:aNumber sign:-1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
   456
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   457
    ^ self
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   458
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   459
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   460
     12345678901234567890 - 0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   461
     12345678901234567890 - 1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   462
     12345678901234567890 - -1
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
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   466
     12345678901234567890 - 12345678901234567880
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   467
     12345678901234567890 - 12345000000000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   468
     12345678901234567890 - -87654321098765432110
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   469
     -12345678901234567890 - 87654321098765432110
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   470
     -12345678901234567890 - -12345678901234567880
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   471
     -12345678901234567890 - -12345678901234567980
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   472
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   473
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
   474
    "Modified: 20.10.1996 / 18:42:16 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   475
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   476
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   477
/ aNumber
1065
77b25fb9f9d7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 927
diff changeset
   478
    "return the quotient of the receiver and the argument, aNumber"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   479
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   480
    aNumber isInteger ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   481
	^ Fraction numerator:self denominator:aNumber
14766
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
   482
    ].
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
   483
    aNumber isFraction ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   484
	^ Fraction numerator:(self * aNumber denominator) denominator:(aNumber numerator)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   485
    ].
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   486
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   487
    "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
   488
    ^ (self asFloat / aNumber asFloat)
2793
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2788
diff changeset
   489
e40dedf51177 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2788
diff changeset
   490
    "Modified: 28.7.1997 / 19:07:55 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   491
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   492
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   493
// aNumber
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   494
    "return the integer part of the quotient of the receiver's value
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   495
     and the argument's value.
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   496
     The result is truncated toward negative infinity
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   497
     and will be negative, if the operands signs differ.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   498
     The following is always true:
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   499
        (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   500
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   501
     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
   502
     Especially surprising:
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   503
        -1 // 10 -> -1 (because -(1/10) is truncated towards next smaller integer, which is -1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   504
        -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
   505
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   506
     See #quo: which returns -2 in the above case and #rem: which is the corresponding remainder."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   508
    |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
   509
5382
b4a9021ea256 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5023
diff changeset
   510
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   511
    cls := aNumber class.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   512
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   513
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   514
     this is the common case, dividing by a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   515
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   516
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   517
    (cls == SmallInteger) ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   518
        abs := aNumber.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   519
        abs := abs abs.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   520
        (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   521
            divMod := self absFastDivMod:abs.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   522
        ] ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   523
            n := abs asLargeInteger.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   524
        ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   525
    ] ifFalse:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   526
        "
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   527
         if the argument is not a largeInteger, coerce
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   528
        "
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   529
        (cls == self class) ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   530
            ^ self retry:#// coercing:aNumber
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   531
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   532
        n := aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   533
    ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   534
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   535
    divMod isNil ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   536
        divMod := self absDivMod:n.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   537
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   538
    quo := divMod at:1.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   539
    (sign == aNumber sign) ifFalse:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   540
        "/ adjust for truncation if negative and there is a remainder ...
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   541
        "/ be careful: there is one special case to care for here:
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   542
        "/ if quo is maxInt+1, the negation can be represented as a smallInt.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   543
        quo := quo setSign:-1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   544
        (divMod at:2) == 0 ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   545
            ^ quo - 1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   546
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   547
        quo digitLength == SmallInteger maxBytes ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   548
            ^ quo compressed
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   549
        ].
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   550
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   551
    ^ quo
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   552
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   553
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   554
     (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
   555
     (-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
   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
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   559
     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
   560
     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
   561
     16rfffffffff // 16r001fffff  =  32768 ifFalse:[self halt].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   562
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   563
     900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   564
     -900 quo: 400
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
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   568
     9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   569
     -9000000000 quo: 4000000000
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
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   572
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   573
     0 // 40000000000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   574
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   575
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   576
    "Modified: / 5.11.1996 / 16:39:36 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   577
    "Modified: / 27.4.1999 / 19:50:26 / stefan"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   578
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   579
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   580
\\ aNumber
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   581
    "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
   582
     negative infinity.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   583
     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
   584
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   585
     The returned remainder has the same sign as aNumber.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   586
     The following is always true:
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   587
	(receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   588
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   589
     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
   590
     Especially surprising:
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   591
	-1 \\ 10 -> 9  (because -(1/10) is truncated towards next smaller integer, which is -1,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   592
			and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   593
	-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   594
			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
   595
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   596
     See #rem: which is the corresponding remainder for division via #quo:.
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
     Redefined here for speed."
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   599
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   600
    |abs rem negativeDivisor|
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
    aNumber negative ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   603
	negativeDivisor := true.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   604
	abs := aNumber negated.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   605
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   606
	negativeDivisor := false.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   607
	abs := aNumber.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   608
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   609
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   610
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   611
     this is the common case, dividing by a SmallInteger.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   612
     Use a special method for this case ...
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   613
    "
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   614
    (aNumber class == SmallInteger) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   615
	(abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   616
	    rem := (self absFastDivMod:abs) at:2.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   617
	] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   618
	    rem := self absMod:abs asLargeInteger
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   619
	].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   620
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   621
	"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   622
	 if the argument is not a largeInteger, coerce
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   623
	"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   624
	(aNumber class == self class) ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   625
	    ^ self retry:#\\ coercing:aNumber
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   626
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   627
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   628
	rem := self absMod:abs.
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   629
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   630
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   631
    rem = 0 ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   632
	negativeDivisor ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   633
	    rem := rem setSign:-1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   634
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   635
	(self negative ~~ negativeDivisor) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   636
	    "different sign, so remainder would have been negative.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   637
	     rem has been rounded toward zero, this code will simulate
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   638
	     rounding to negative infinity."
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   639
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   640
	    rem := aNumber - rem.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   641
	].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   642
    ].
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   643
    ^ rem
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   644
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   645
    "
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   646
     (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
   647
     (-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
   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
     (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
   651
     (-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
   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].
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   654
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   655
     9000000000 \\ 7
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   656
     -9000000000 \\ 7
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
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   660
     900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   661
     -900 rem: 400
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
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   665
     9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   666
     -9000000000 rem: 4000000000
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
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   669
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   670
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   671
    "Modified: / 5.11.1996 / 17:10:10 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   672
    "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
   673
!
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   674
4946
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   675
abs
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   676
    "return my absolute value. redefined for speed."
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   677
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   678
    sign >= 0 ifTrue:[^ self].
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   679
    ^ self negated
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   680
d18052030990 fixed add/subtract for alpha;
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   681
    "Created: / 26.10.1999 / 21:28:06 / stefan"
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
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   684
divMod:aNumber
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   685
    "return an array filled with
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   686
	(self // aNumber) and (self \\ aNumber).
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   687
     The returned remainder has the same sign as aNumber.
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   688
     The following is always true:
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   689
	(receiver // something) * something + (receiver \\ something) = receiver
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   690
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   691
     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
   692
     Especially surprising:
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   693
	-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
   694
			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
   695
	-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
   696
			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
   697
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   698
     This is redefined here for more performance
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   699
     (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
   700
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   701
    |cls n|
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   702
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   703
    ((self < 0) or:[aNumber <= 0]) ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   704
	^ super divMod:aNumber
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   705
    ].
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   706
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   707
    "/ 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
   708
    cls := aNumber class.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   709
    (cls == SmallInteger) ifTrue:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   710
	"
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   711
	 this is the common case, dividing by a SmallInteger.
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   712
	 Use a special method for this case ...
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   713
	"
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   714
	(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
   715
	    ^ self absFastDivMod:aNumber abs.
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   716
	].
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   717
	n := aNumber asLargeInteger.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   718
    ] ifFalse:[
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   719
	(cls == self class) ifFalse:[
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   720
	    ^ super divMod:aNumber
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   721
	].
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
   722
	n := aNumber.
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   723
    ].
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   724
17035
4b6d41ac41b6 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16916
diff changeset
   725
    ^ self absDivMod:n abs.
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   726
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   727
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   728
     9000000000 // 4000000000   => 2
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   729
     9000000000 \\ 4000000000   => 1000000000
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   730
     9000000000 divMod: 4000000000   => #(2 1000000000)
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   731
    "
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   732
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   733
    "Created: / 29.10.1996 / 21:22:05 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   734
    "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
   735
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   736
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   737
negated
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   738
    "return an integer with value negated from the receiver's value."
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   739
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   740
    |newNumber sz|
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   741
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   742
    (sign == 0) ifTrue:[^ 0].
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
    "
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   745
     special case for SmallInteger minVal
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
    sign == 1 ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   748
        sz := digitByteArray size.
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   749
%{
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   750
#ifdef __SCHTEAM__
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   751
#else /* not SCHTEAM */
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   752
        int idx;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   753
        unsigned char *bp;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   754
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   755
        bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   756
        idx = __intVal(sz);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   757
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   758
        while ((idx > 1) && (bp[idx-1] == 0)) idx--;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   759
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   760
        if (idx == sizeof(INT)) {
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   761
# if defined(__LSBFIRST__)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   762
#  if __POINTER_SIZE__ == 8
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   763
            if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   764
#  else
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   765
            if ( ((unsigned INT *)bp)[0] == 0x40000000)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   766
#  endif
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   767
# else
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   768
            /*
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   769
             * generic code
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   770
             */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   771
            if ((bp[idx-1] == 0x40)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   772
             && (bp[idx-2] == 0)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   773
             && (bp[idx-3] == 0)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   774
             && (bp[idx-4] == 0)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   775
#  if __POINTER_SIZE__ == 8
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   776
             && (bp[idx-5] == 0)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   777
             && (bp[idx-6] == 0)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   778
             && (bp[idx-7] == 0)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   779
             && (bp[idx-8] == 0)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   780
#  endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   781
            )
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   782
# endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   783
            {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   784
                RETURN (__mkSmallInteger(_MIN_INT));
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   785
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   786
        }
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   787
#endif
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   788
%}.
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   789
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   790
        sz == SmallInteger maxBytes ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   791
          (digitByteArray at:1) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   792
           (digitByteArray at:2) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   793
            (digitByteArray at:3) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   794
                SmallInteger maxBytes == 8 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   795
                  (digitByteArray at:4) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   796
                   (digitByteArray at:5) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   797
                    (digitByteArray at:6) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   798
                     (digitByteArray at:7) == 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   799
                      (digitByteArray at:8) == 16r40 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   800
                        ^ SmallInteger minVal
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   801
                      ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   802
                     ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   803
                    ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   804
                   ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   805
                  ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   806
                ] ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   807
                  (digitByteArray at:4) == 16r40 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   808
                    ^ SmallInteger minVal
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   809
                  ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   810
                ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   811
            ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   812
           ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   813
          ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
   814
        ].
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   815
    ].
5023
5e98a2a3ea6f slightly faster #negated
Claus Gittinger <cg@exept.de>
parents: 5022
diff changeset
   816
    "/ cg - can share the digits ...
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   817
    newNumber := self class digitBytes:digitByteArray sign:(sign negated).
1210
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   818
    ^ newNumber
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   819
!
b2fbf119dbbd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   820
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   821
quo:aNumber
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   822
    "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
   823
     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
   824
     truncates toward negative infinity).
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   825
     The results sign is negative if the receiver has a sign
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   826
     different from the args sign.
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   827
     The following is always true:
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   828
	(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   829
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   830
2521
43b9d4fafb35 removed unused locals
Claus Gittinger <cg@exept.de>
parents: 2495
diff changeset
   831
    |otherSign quo abs "{ Class: SmallInteger }" |
927
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
    otherSign := aNumber sign.
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   834
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
     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
   837
     Use a special method for this case ...
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
    (aNumber class == SmallInteger) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   840
	abs := aNumber.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   841
	abs := abs abs.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   842
	(abs between:1 and:16r00ffffff) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   843
	    quo := (self absFastDivMod:abs) at:1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   844
	    (sign == otherSign) ifTrue:[^ quo].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   845
	    ^ quo setSign:-1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   846
	]
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   847
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   848
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   849
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   850
     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
   851
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   852
    (aNumber class == self class) ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   853
	^ self retry:#quo: coercing:aNumber
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   854
    ].
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   855
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   856
    sign < 0 ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   857
	(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
   858
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   859
	(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
   860
    ].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
   861
    ^ ((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
   862
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   863
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   864
     900 // 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   865
     -900 // 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   866
     900 // -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   867
     -900 // -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   868
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   869
     9000000000 // 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   870
     -9000000000 // 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   871
     9000000000 // -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   872
     -9000000000 // -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   873
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   874
     900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   875
     -900 quo: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   876
     900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   877
     -900 quo: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   878
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   879
     9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   880
     -9000000000 quo: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   881
     9000000000 quo: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   882
     -9000000000 quo: -4000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   883
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   884
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   885
    "Modified: / 5.11.1996 / 14:14:17 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   886
    "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
   887
!
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   888
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   889
rem:aNumber
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   890
    "return the remainder of division of the receiver by the argument, aNumber.
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   891
     The returned remainder has the same sign as the receiver.
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   892
     The following is always true:
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   893
	(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
6067
e4c03d14a5de comment
Claus Gittinger <cg@exept.de>
parents: 6051
diff changeset
   894
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   895
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   896
    |rem abs "{ Class: SmallInteger }" |
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   897
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   898
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   899
     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
   900
     Use special code for this case ...
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   901
    "
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   902
    (aNumber class == SmallInteger) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   903
	abs := aNumber.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   904
	abs := abs abs.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   905
	(abs between:1 and:16r00ffffff) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   906
	    rem := (self absFastDivMod:abs) at:2.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   907
	] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   908
	    rem := self absMod:abs asLargeInteger
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   909
	].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   910
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   911
	"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   912
	 if the argument is not a largeInteger, coerce
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   913
	"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   914
	(aNumber class == self class) ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   915
	    ^ self retry:#rem coercing:aNumber
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   916
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   917
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   918
	rem := self absMod:aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   919
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   920
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   921
    sign < 0 ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
   922
	^ rem setSign:-1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   923
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
   924
    ^ rem
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   925
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   926
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   927
     900 \\ 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   928
     -900 \\ 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   929
     900 \\ -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   930
     -900 \\ -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   931
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   932
     9000000000 \\ 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   933
     -9000000000 \\ 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   934
     9000000000 \\ -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   935
     -9000000000 \\ -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   936
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   937
     900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   938
     -900 rem: 400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   939
     900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   940
     -900 rem: -400
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   941
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   942
     9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   943
     -9000000000 rem: 4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   944
     9000000000 rem: -4000000000
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   945
     -9000000000 rem: -4000000000
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   946
    "
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
   947
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
   948
    "Modified: / 5.11.1996 / 14:02:59 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
   949
    "Modified: / 29.4.1999 / 11:26:51 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   950
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   951
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
   952
!LargeInteger methodsFor:'bit operators'!
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
   953
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   954
bitAnd:anInteger
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   955
    "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
   956
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   957
%{  /* NOCONTEXT */
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   958
    if (__isSmallInteger(anInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   959
	INT v2 = __intVal(anInteger);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   960
	INT v1;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
   961
#if defined(__LSBFIRST__)
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   962
	v1 = *(INT *)(__byteArrayVal(__INST(digitByteArray)));
4663
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
   963
#else
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   964
	unsigned char *digits = (unsigned char *)(__byteArrayVal(__INST(digitByteArray)));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   965
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   966
	v1 = digits[0] & 0xFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   967
	v1 = v1 | ((digits[1] & 0xFF)<<8);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   968
	v1 = v1 | ((digits[2] & 0xFF)<<16);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   969
	v1 = v1 | ((digits[3] & 0xFF)<<24);
11605
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
   970
# if (__POINTER_SIZE__ == 8)
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   971
	v1 = v1 | ((digits[4] & 0xFF)<<32);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   972
	v1 = v1 | ((digits[5] & 0xFF)<<40);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   973
	v1 = v1 | ((digits[6] & 0xFF)<<48);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   974
	v1 = v1 | ((digits[7] & 0xFF)<<56);
11605
adff1007beb2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11604
diff changeset
   975
 #endif
4663
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
   976
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   977
	RETURN ( __mkSmallInteger(v1 & v2) );
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
   978
    }
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   979
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
   980
    if (__isLargeInteger(anInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   981
	OBJ _myDigitByteArray = __INST(digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   982
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(anInteger)->l_digits;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   983
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   984
	if (__isByteArray(_myDigitByteArray)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   985
	 && __isByteArray(_otherDigitByteArray)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   986
	    unsigned char *pDigits1, *pDigits2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   987
	    int size1, size2, minSize;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   988
	    union {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   989
		double d;                    // force align
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   990
		unsigned char chars[2048+8];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   991
	    } buffer;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   992
	    unsigned char *pRslt;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   993
	    OBJ newDigits, newLarge;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   994
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   995
	    pDigits1 = (unsigned char *)(__byteArrayVal(_myDigitByteArray));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   996
	    pDigits2 = (unsigned char *)(__byteArrayVal(_otherDigitByteArray));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   997
	    pRslt = (void *)(buffer.chars);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   998
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
   999
	    size1 = __byteArraySize(_myDigitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1000
	    size2 = __byteArraySize(_otherDigitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1001
	    minSize = (size1 < size2) ? size1 : size2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1002
	    if (minSize <= sizeof(buffer.chars)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1003
		int n = minSize;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1004
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1005
	    /* 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
  1006
#define x__USE_MMX__
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1007
#ifdef __USE_MMX__
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1008
#ifdef __VISUALC__
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1009
		if (((INT)pRslt & 7) == 0) {    // 8-byte aligned
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1010
		    if (((INT)pDigits1 & 7) == ((INT)pDigits2 & 7)) {   // same align
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1011
			while (((INT)pDigits1 & 7) && (n >= sizeof(int))) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1012
			    ((int *)pRslt)[0] = ((int *)pDigits1)[0] & ((int *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1013
			    pRslt += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1014
			    pDigits1 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1015
			    pDigits2 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1016
			    pDigits2 += sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1017
			    n -= sizeof(int);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1018
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1019
			for (; n >= 8; n -= 8) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1020
			    __asm {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1021
				mov eax, pDigits1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1022
				movq mm0, [eax]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1023
				mov eax, pDigits2
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1024
				movq mm1, [eax]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1025
				pand mm0, mm1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1026
				mov eax, pRslt
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1027
				movq [eax], mm0
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1028
			    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1029
			    pDigits1 += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1030
			    pDigits2 += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1031
			    pRslt += 8;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1032
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1033
			__asm {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1034
			    emms ; switch back to FPU state.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1035
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1036
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1037
		}
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1038
#endif /* __VISUALC__ */
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1039
#endif /* __USE_MMX__ */
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1040
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1041
		for (; n >= sizeof(INT)*4; n -= sizeof(INT)*4) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1042
		    ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1043
		    ((INT *)pRslt)[1] = ((INT *)pDigits1)[1] & ((INT *)pDigits2)[1];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1044
		    ((INT *)pRslt)[2] = ((INT *)pDigits1)[2] & ((INT *)pDigits2)[2];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1045
		    ((INT *)pRslt)[3] = ((INT *)pDigits1)[3] & ((INT *)pDigits2)[3];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1046
		    pRslt += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1047
		    pDigits1 += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1048
		    pDigits2 += sizeof(INT)*4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1049
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1050
		for (; n >= sizeof(INT); n -= sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1051
		    ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1052
		    pRslt += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1053
		    pDigits1 += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1054
		    pDigits2 += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1055
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1056
		for (; n > 0; n--) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1057
		    *pRslt = *pDigits1 & *pDigits2;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1058
		    pRslt++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1059
		    pDigits1++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1060
		    pDigits2++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1061
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1062
		// normalize
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1063
		while ((pRslt[-1]==0) && (pRslt > buffer.chars)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1064
		    pRslt--;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1065
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1066
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1067
		// allocate result
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1068
		n = pRslt-buffer.chars;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1069
		if (n <= sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1070
		    INT val = 0;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1071
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1072
		    // make it a smallInteger / 32bitInteger
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1073
		    while (n > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1074
			val = (val << 8) + buffer.chars[--n];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1075
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1076
		    RETURN (__MKUINT(val));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1077
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1078
		newDigits = __MKBYTEARRAY(buffer.chars, n);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1079
		if (newDigits) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1080
		    __PROTECT__(newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1081
		    newLarge = __STX___new(sizeof(struct __LargeInteger));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1082
		    __UNPROTECT__(newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1083
		    if (newLarge) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1084
			__InstPtr(newLarge)->o_class = LargeInteger; __STORE(newLarge, LargeInteger);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1085
			__LargeIntegerInstPtr(newLarge)->l_digits = newDigits; __STORE(newLarge, newDigits);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1086
			__LargeIntegerInstPtr(newLarge)->l_sign = __MKSMALLINT(1);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1087
			RETURN (newLarge);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1088
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1089
		}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1090
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1091
	}
11959
1ff0e6f908d9 fast bitAnd
Claus Gittinger <cg@exept.de>
parents: 11927
diff changeset
  1092
    }
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1093
%}.
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1094
    ^ super bitAnd:anInteger
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1095
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1096
    "
6051
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1097
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFF) hexPrintString
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1098
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1099
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1100
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1101
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1102
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1103
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFFFF) hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1104
     (16rFFEEDDCCBBAA998877665544332211 bitAnd:16rFFFFFFFFFFFFFFFFFF) hexPrintString
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1105
    "
6051
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1106
e86757447e81 comment
Claus Gittinger <cg@exept.de>
parents: 6049
diff changeset
  1107
    "Modified: / 26.9.2001 / 17:34:03 / cg"
4612
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1108
!
724572ee0390 fast and: with a smallInteger
Claus Gittinger <cg@exept.de>
parents: 4454
diff changeset
  1109
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1110
bitXor:anInteger
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1111
    "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
  1112
     Here, a specially tuned version for largeInteger arguments
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1113
     (to speed up some cryptographic code)"
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1114
15626
d1cacaf45c0c class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15577
diff changeset
  1115
    |len1 len2 newBytes|
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1116
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1117
    anInteger class ~~ LargeInteger ifTrue:[^ super bitXor:anInteger].
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1118
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1119
    (len1 := anInteger digitLength) > (len2 := self digitLength) ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1120
	newBytes := anInteger digitBytes copy.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1121
	newBytes bitXorBytesFrom:1 to:len2 with:digitByteArray startingAt:1
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1122
    ] ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1123
	newBytes := digitByteArray copy.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1124
	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
  1125
    ].
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  1126
    ^ (self class digitBytes:newBytes) compressed
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1127
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1128
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1129
     (16r112233445566778899 bitXor:16rFF                ) printStringRadix:16 '112233445566778866'
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1130
     (16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799'
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1131
     (16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899'
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1132
     (16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 '99'
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1133
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1134
     |bigNum1 bigNum2|
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1135
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1136
     bigNum1 := 2 raisedToInteger:512.
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1137
     bigNum2 := 2 raisedToInteger:510.
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1138
     Time millisecondsToRun:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1139
	1000000 timesRepeat:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1140
	   bigNum1 bitXor:bigNum2.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1141
	]
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1142
     ]
4920
802099ba0ac0 big speedup for bitXor:
Claus Gittinger <cg@exept.de>
parents: 4831
diff changeset
  1143
    "
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
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1146
lowBit
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1147
    "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
  1148
     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
  1149
     Returns 0 if no bit is set.
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1150
     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
  1151
     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
  1152
     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
  1153
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1154
    |sz   "{ Class: SmallInteger }"
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1155
     idx0 "{ Class: SmallInteger }"
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1156
     byte|
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1157
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1158
    idx0 := 1.
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1159
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1160
%{
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1161
    OBJ __digitByteArray = __INST(digitByteArray);
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1162
7544
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1163
    /*
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1164
     * quickly advance over full 0-words
dcde4788d7f0 comment
Claus Gittinger <cg@exept.de>
parents: 7481
diff changeset
  1165
     */
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1166
    if (__isByteArray(__digitByteArray)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1167
	int __sz = __byteArraySize(__digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1168
	unsigned char *__bP = __byteArrayVal(__digitByteArray);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1169
	unsigned char *__bP0 = __bP;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1170
15262
5047292c9107 all stx macros begin with double underline (eg. __qClass instead of _qClass)
Claus Gittinger <cg@exept.de>
parents: 15242
diff changeset
  1171
	sz = __MKSMALLINT(__sz);
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1172
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1173
#ifdef __UNROLL_LOOPS__
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1174
	while (__sz > (sizeof(INT) * 4)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1175
	    if (( ((INT *)__bP)[0]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1176
		 | ((INT *)__bP)[1]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1177
		 | ((INT *)__bP)[2]
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1178
		 | ((INT *)__bP)[3] ) != 0) break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1179
	    __sz -= sizeof(INT) * 4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1180
	    __bP += sizeof(INT) * 4;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1181
	}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1182
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1183
	while (__sz > sizeof(INT)) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1184
	    if ( ((INT *)__bP)[0] != 0 ) break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1185
	    __sz -= sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1186
	    __bP += sizeof(INT);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1187
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1188
	while (__sz > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1189
	    unsigned int c;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1190
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1191
	    if ( (c = *__bP) != 0 ) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1192
		int bitIdx = (__bP - __bP0) * 8;
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1193
#ifdef __BSF
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1194
		{
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1195
		    int index;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1196
		    int t = c;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1197
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1198
		    index = __BSF(t);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1199
		    RETURN ( __mkSmallInteger(index + 1 + bitIdx) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1200
		}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1201
#else
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1202
		if (c & 0x0F) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1203
		    if (c & 0x03) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1204
			if (c & 0x01) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1205
			    RETURN ( __mkSmallInteger( bitIdx + 1) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1206
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1207
			    RETURN ( __mkSmallInteger( bitIdx + 2) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1208
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1209
		    } else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1210
			if (c & 0x04) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1211
			    RETURN ( __mkSmallInteger( bitIdx + 3) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1212
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1213
			    RETURN ( __mkSmallInteger( bitIdx + 4) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1214
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1215
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1216
		} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1217
		    if (c & 0x30) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1218
			if (c & 0x10) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1219
			    RETURN ( __mkSmallInteger( bitIdx + 5) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1220
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1221
			    RETURN ( __mkSmallInteger( bitIdx + 6) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1222
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1223
		    } else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1224
			if (c & 0x40) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1225
			    RETURN ( __mkSmallInteger( bitIdx + 7) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1226
			} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1227
			    RETURN ( __mkSmallInteger( bitIdx + 8) );
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1228
			}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1229
		    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1230
		}
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1231
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1232
		break;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1233
	    }
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1234
	    __sz--;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1235
	    __bP++;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1236
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1237
	idx0 = __mkSmallInteger( __bP - __bP0 + 1 );
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1238
    }
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1239
%}.
12746
10866527a1ab changed: #lowBit
Claus Gittinger <cg@exept.de>
parents: 12121
diff changeset
  1240
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1241
    "/ never actually reached
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1242
    idx0 to:sz do:[:digitIndex |
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1243
	(byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1244
	    ^ (digitIndex-1)*8 + (byte lowBit)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1245
	]
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1246
    ].
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1247
    ^ 0 "/ should not happen
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1248
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1249
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1250
     (1 bitShift:0) lowBit
8929
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1251
     (1 bitShift:10) lowBit
cdddcc1c470e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1252
     (1 bitShift:20) lowBit
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1253
     (1 bitShift:30) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1254
     (1 bitShift:30) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1255
     (1 bitShift:31) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1256
     (1 bitShift:31) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1257
     (1 bitShift:32) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1258
     (1 bitShift:32) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1259
     (1 bitShift:33) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1260
     (1 bitShift:33) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1261
     (1 bitShift:64) lowBit
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1262
     (1 bitShift:64) highBit
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1263
     (1 bitShift:1000) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1264
     (1 bitShift:1000) highBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1265
     ((1 bitShift:64)-1) lowBit
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1266
     ((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
  1267
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1268
     1 to:1000 do:[:idx |
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1269
	self assert:(( 1 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1270
	self assert:(( 1 bitShift:idx) lowBit = ( 1 bitShift:idx) highBit).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1271
	self assert:(( 3 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1272
	self assert:(( 7 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1273
	self assert:(( 15 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1274
	self assert:(( 31 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1275
	self assert:(( 63 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1276
	self assert:(( 127 bitShift:idx) lowBit = (idx+1)).
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1277
	self assert:(( 255 bitShift:idx) lowBit = (idx+1)).
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1278
     ]
7478
2f7430bfd7ac tuned lowBit (req'd for gcd)
Claus Gittinger <cg@exept.de>
parents: 7471
diff changeset
  1279
6480
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1280
     |num|
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1281
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1282
     num := (1 bitShift:1000).
4ff7f2af25fc lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents: 6359
diff changeset
  1283
     Time millisecondsToRun:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1284
	1000000 timesRepeat:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1285
	    num lowBit
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1286
	]
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1287
     ]
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1288
    "
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1289
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1290
    "Modified: 14.8.1997 / 11:55:34 / cg"
19052
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1291
! !
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1292
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1293
!LargeInteger methodsFor:'bit operators - indexed'!
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1294
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1295
bitAt:anIntegerIndex
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1296
    "return the value of the index's bit (index starts at 1) as 0 or 1.
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1297
     Notice: the result of bitAt: on negative receivers is not
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1298
	     defined in the language standard (since the implementation
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1299
	     is free to choose any internal representation for integers)"
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1300
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1301
%{  /* NOCONTEXT */
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1302
    if (__isSmallInteger(anIntegerIndex)) {
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1303
	INT idx = __smallIntegerVal(anIntegerIndex) - 1;
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1304
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1305
	if (idx >= 0) {
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1306
	    int v1;
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1307
	    int byteOffset = idx / 8;
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1308
	    int digitLen   = __byteArraySize(__INST(digitByteArray));
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1309
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1310
	    if (digitLen < byteOffset) {
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1311
		RETURN(__mkSmallInteger(0));
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1312
	    }
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1313
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1314
	    v1 = (__byteArrayVal(__INST(digitByteArray)))[byteOffset];
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1315
	    if (v1 & (1 << (idx % 8))) {
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1316
		RETURN(__mkSmallInteger(1));
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1317
	    } else {
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1318
		RETURN(__mkSmallInteger(0));
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1319
	    }
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1320
	}
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1321
    }
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1322
%}.
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1323
    ^ super bitAt:anIntegerIndex
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1324
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1325
    "
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1326
     TestCase should:[ 16rFFFFFFFFFF01 bitAt:0 ] raise:Error
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1327
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:49 ) == 0
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1328
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:1  ) == 1
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1329
     TestCase assert:( 16rFFFFFFFFFF01 bitAt:2  ) == 0
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1330
     TestCase assert:( 16rFFFFFFFFFF02 bitAt:2  ) == 1
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1331
     TestCase assert:( 16rFFFFFFFF01FF bitAt:8  ) == 1
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1332
     TestCase assert:( 16rFFFFFFFF01FF bitAt:9  ) == 1
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1333
     TestCase assert:( 16rFFFFFFFF01FF bitAt:10 ) == 0
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1334
    "
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1335
3981835eca68 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18903
diff changeset
  1336
    "Modified: / 10-08-2010 / 12:33:04 / cg"
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1337
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1338
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1339
setBit:index
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1340
    "return a new integer, where the specified bit is on.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1341
     Bits are counted from 1 starting with the least significant.
15132
83d908af0aa1 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 14766
diff changeset
  1342
     The methods name may be misleading: the receiver is not changed,
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1343
     but a new number is returned. Should be named #withBitSet:"
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1344
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1345
    |myDigitLength newDigitLength newDigitBytes byteIndexOfBitToSet|
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1346
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1347
    index <= 0 ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1348
	^ SubscriptOutOfBoundsSignal
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1349
		raiseRequestWith:index
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1350
		errorString:'index out of bounds'
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1351
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1352
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1353
    myDigitLength := digitByteArray size.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1354
    byteIndexOfBitToSet := ((index-1)//8)+1.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1355
    byteIndexOfBitToSet > myDigitLength ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1356
	newDigitLength := myDigitLength max:byteIndexOfBitToSet.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1357
	newDigitBytes := ByteArray new:newDigitLength.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1358
	newDigitBytes replaceFrom:1 to:myDigitLength with:digitByteArray startingAt:1.
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1359
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1360
	newDigitBytes := digitByteArray copy
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1361
    ].
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1362
    newDigitBytes
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1363
	at:byteIndexOfBitToSet
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  1364
	put:((newDigitBytes at:byteIndexOfBitToSet) setBit:(((index-1)\\8)+1)).
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  1365
    ^ self class digitBytes:newDigitBytes sign:sign
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1366
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1367
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1368
     TestCase assert:( 16r80000000 setBit:3  ) = 16r80000004
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  1369
     TestCase assert:( 16r80000000 setBit:33 ) = 16r180000000
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  1370
    "
2855
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1371
! !
9c26fec68549 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2850
diff changeset
  1372
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1373
!LargeInteger methodsFor:'bit operators-32bit'!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1374
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1375
bitInvert32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1376
    "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
  1377
     (changes the sign)"
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
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1380
    unsigned INT v;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1381
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1382
    v = __unsignedLongIntVal(self);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1383
    v = ~v;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1384
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1385
    v &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1386
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1387
    RETURN ( __MKUINT(v) );
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
    ^ self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1390
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
     16r80000000 bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1393
     16r7FFFFFFF bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1394
     16rFFFFFFFF bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1395
     0 bitInvert32 hexPrintString
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1396
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1397
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1398
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1399
bitRotate32:shiftCount
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1400
    "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
  1401
     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
  1402
     Rotates through the sign bit.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1403
     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
  1404
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1405
%{  /* NOCONTEXT */
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
    unsigned INT bits;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1408
    int count;
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
    if (__isSmallInteger(shiftCount)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1411
	count = __intVal(shiftCount);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1412
	count = count % 32;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1413
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1414
	bits = __unsignedLongIntVal(self);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1415
	if (count > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1416
	    bits = (bits << count) | (bits >> (32-count));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1417
	} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1418
	    bits = (bits >> (-count)) | (bits << (32-(-count)));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1419
	}
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1420
#if __POINTER_SIZE__ == 8
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1421
	bits &= 0xFFFFFFFFL;
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1422
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1423
	RETURN (__MKUINT(bits));
12877
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
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1426
    ^ self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1427
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1428
    "
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1429
     (1 bitShift32:31) rotate32:0
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1430
     (1 bitShift32:31) rotate32:1
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1431
     (1 bitShift32:31) rotate32:-1
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1432
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1433
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1434
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1435
bitShift32:shiftCount
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1436
    "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
  1437
     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
  1438
     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
  1439
     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
  1440
     or to emulate C/Java semantics.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1441
     The shift is unsigned"
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1442
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1443
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1444
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1445
    unsigned INT bits;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1446
    int count;
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
    if (__isSmallInteger(shiftCount)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1449
	count = __intVal(shiftCount);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1450
	if (count >= 32) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1451
	    RETURN (__mkSmallInteger(0));
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1452
	}
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1453
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1454
	bits = __unsignedLongIntVal(self);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1455
	if (count > 0) {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1456
	    bits = bits << count;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1457
	} else {
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1458
	    bits = bits >> (-count);
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1459
	}
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1460
#if __POINTER_SIZE__ == 8
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1461
	bits &= 0xFFFFFFFFL;
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1462
#endif
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1463
	RETURN (__MKUINT(bits));
12877
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
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1466
    ^ self primitiveFailed
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
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1469
     128 bitShift:24
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1470
     128 bitShift32:24
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1471
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1472
     1 bitShift:31
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1473
     1 bitShift32:31
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1474
    "
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
bitXor32:aNumber
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1478
    "return the xor of the receiver and the argument.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1479
     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
  1480
     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
  1481
     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
  1482
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1483
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1484
    INT rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1485
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1486
    rslt =  __unsignedLongIntVal(self) ^ __unsignedLongIntVal(aNumber);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1487
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1488
    rslt &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1489
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1490
    RETURN ( __MKUINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1491
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1492
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1493
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1494
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1495
     16r7FFFFFFF bitXor: 16r80000000          4294967295
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1496
     16r7FFFFFFF bitXor32: 16r80000000
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1497
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1498
! !
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1499
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1500
!LargeInteger methodsFor:'byte access'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1501
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1502
byteSwapped
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1503
    "lsb -> msb;
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1504
     i.e. a.b ... y.z -> z.y. ... b.a"
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1505
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1506
    ^ self class digitBytes:digitByteArray MSB:true
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1507
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1508
    "
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1509
     (LargeInteger value:16r11223344) byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1510
     (LargeInteger value:16r44332211) byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1511
     16r88776655 byteSwapped hexPrintString
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1512
     16r11223344 byteSwapped hexPrintString
13992
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1513
    "
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1514
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1515
    "Created: / 31-01-2012 / 11:07:42 / cg"
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1516
!
459aea99d16b added: byteSwapped
Claus Gittinger <cg@exept.de>
parents: 13371
diff changeset
  1517
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1518
byteSwapped32
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1519
    "byte swap a 32bit value; lsb -> msb;
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1520
     i.e. a.b.c.d -> d.c.b.a
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1521
     Useful for communication protocols"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1522
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1523
%{
15909
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1524
    unsigned INT swapped;
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1525
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1526
    swapped = ( (__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1527
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1528
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1529
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1530
    RETURN (__MKUINT(swapped));
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1531
%}.
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1532
    ^ super byteSwapped32
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1533
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1534
    "
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1535
     (LargeInteger value:16r11223344) byteSwapped32 hexPrintString
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1536
     (LargeInteger value:16r44332211) byteSwapped32 hexPrintString
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1537
     16r88776655 byteSwapped32 hexPrintString
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1538
     16r11223344 byteSwapped32 hexPrintString
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1539
    "
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1540
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1541
    "Created: / 31-01-2012 / 11:07:42 / cg"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1542
!
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1543
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1544
byteSwapped64
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1545
    "byte swap a 64bit value; lsb -> msb;
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1546
     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
  1547
     Useful for communication protocols"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1548
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1549
%{
15909
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1550
    unsigned INT swappedLO = 0;
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1551
    unsigned INT swappedHI;
9ffe9886b91b class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15908
diff changeset
  1552
    unsigned INT swapped;
15908
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
    swappedHI = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1555
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1556
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1557
              | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1558
    if (__byteArraySize(__INST(digitByteArray)) > 4) {
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1559
        swappedLO = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[4]) << 24)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1560
                  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[5]) << 16)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1561
                  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[6]) << 8)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1562
                  | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[7]));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1563
    }
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
#if __POINTER_SIZE__ == 8
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1566
    swapped = (swappedHI<<32) | swappedLO;
16520
a1a1d558cfa3 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 16124
diff changeset
  1567
    RETURN(__MKUINT( swapped ));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1568
#else
16520
a1a1d558cfa3 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 16124
diff changeset
  1569
    RETURN(__MKLARGEINT64(1, swappedLO, swappedHI));
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1570
#endif
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1571
%}.
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  1572
    ^ super byteSwapped64
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1573
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1574
    "
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1575
     (LargeInteger value:16r11223344) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1576
     (LargeInteger value:16r44332211) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1577
     (LargeInteger value:16r1122334455667788) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1578
     (LargeInteger value:16r8877665544332211) byteSwapped64 hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1579
     16r88776655 byteSwapped hexPrintString
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  1580
     16r11223344 byteSwapped hexPrintString
15908
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1581
    "
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1582
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1583
    "Created: / 31-01-2012 / 11:07:42 / cg"
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1584
!
35c0bb53fba1 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15838
diff changeset
  1585
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1586
digitAt:index
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1587
    "return 8 bits of value, starting at byte index"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1588
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1589
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1590
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1591
    return context._RETURN( ((STLargeInteger)self).digitAt( index.intValue() ) );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1592
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1593
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1594
    index > digitByteArray size ifTrue:[^ 0].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1595
    ^ digitByteArray at:index
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1596
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1597
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1598
digitAt:index put:aByte
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1599
    "set the 8 bits, index is a byte index"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1600
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1601
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1602
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1603
    ERROR("cannot modify the digits of a LargeInteger");
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1604
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1605
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1606
    digitByteArray at:index put:aByte
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1607
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1608
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1609
digitByteAt:index
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1610
    "return 8 bits of my signed value, starting at byte index.
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1611
     For positive receivers, this is the same as #digitAt:;
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1612
     for negative ones, the actual bit representation is returned."
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1613
18900
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1614
    |digits|
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1615
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1616
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1617
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1618
    return context._RETURN( ((STLargeInteger)self).digitByteAt( index.intValue() ) );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1619
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1620
%}.
6359
fcb206734265 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6106
diff changeset
  1621
    sign >= 0 ifTrue:[
18900
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1622
        index > digitByteArray size ifTrue:[
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1623
            ^ 0
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1624
        ].
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1625
        ^ digitByteArray at:index.
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1626
    ].
6359
fcb206734265 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6106
diff changeset
  1627
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1628
    "/ negative int - do 2's complement here
18900
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1629
    digits := (self bitInvert + 1) digitBytes.
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1630
    index > digits size ifTrue:[
18900
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1631
        ^ 16rFF
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1632
    ].
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1633
    ^ digits at:index.
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1634
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1635
    "
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1636
     16r11111111111111111111 negated digitByteAt:1
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1637
     0 asLargeInteger digitByteAt:1
18900
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1638
     -1 asLargeInteger digitByteAt:1
ace3f7e03da3 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18899
diff changeset
  1639
     -1 digitByteAt:1
3891
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1640
    "
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1641
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1642
    "Created: / 25.10.1998 / 14:12:21 / cg"
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1643
!
1bbab65996ef added #digitByteAt:
Claus Gittinger <cg@exept.de>
parents: 3440
diff changeset
  1644
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1645
digitBytes
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  1646
    "return a byteArray filled with the receiver's bits
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1647
     (8 bits of the absolute value per element).
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1648
     Least significant byte is first!!"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1649
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1650
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1651
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1652
    return context._RETURN( ((STLargeInteger)self).digitBytes() );
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1653
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1654
%}.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1655
    ^ digitByteArray
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1656
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1657
    "Modified: / 5.5.1999 / 14:57:03 / stefan"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1658
!
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1659
15569
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1660
digitBytesMSB
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  1661
    "return a byteArray filled with the receiver's bits
15569
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1662
     (8 bits of the absolute value per element),
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1663
     most significant byte first"
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1664
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1665
     ^ digitByteArray copyReverse.
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1666
!
8a82c6a65677 new: #digitBytesMSB
Stefan Vogel <sv@exept.de>
parents: 15326
diff changeset
  1667
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1668
digitBytesMSB:msbFlag
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  1669
    "return a byteArray filled with the receiver's bits
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1670
     (8 bits of the absolute value per element),
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1671
     if msbflag = true, most significant byte is first,
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1672
     otherwise least significant byte is first"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1673
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1674
    msbFlag ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  1675
        ^ digitByteArray copyReverse.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1676
    ].
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1677
    ^ digitByteArray
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1678
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1679
    "Modified: / 5.5.1999 / 14:57:03 / stefan"
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1680
!
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  1681
18899
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1682
digitBytesSigned
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1683
    "return a byteArray filled with the receiver's bits
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1684
     (8 bits of the value (which may be negative) per element).
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1685
     Least significant byte is first!!"
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1686
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1687
    sign > 0 ifTrue:[
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1688
        ^ digitByteArray
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1689
    ].
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1690
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1691
    "answer the 2's complement"
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1692
    ^ (self bitInvert + 1) digitBytes.
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1693
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1694
    " 
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1695
        16r12345678901234567890 digitBytesSigned
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1696
        -16r12345678901234567890 digitBytesSigned
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1697
    "
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1698
!
df3418f55649 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18855
diff changeset
  1699
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1700
digitLength
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1701
    "return the number bytes used by this Integer.
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1702
     For negative receivers, the digitLength of its absolute value
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1703
     is returned."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1704
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1705
    "
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1706
     check if there is a 0-byte ...
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1707
     this allows to ask unnormalized LargeIntegers
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1708
     for their digitLength
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1709
    "
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1710
    |l "{ Class: SmallInteger }" |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1711
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1712
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  1713
#ifdef __SCHTEAM__
18709
Claus Gittinger <cg@exept.de>
parents: 18621
diff changeset
  1714
    return context._RETURN( STInteger._new( ((STLargeInteger)self).digitLength() ));
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1715
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  1716
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1717
    l := digitByteArray size.
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1718
    [l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  1719
	l := l - 1.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1720
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1721
    ^ l
927
8d8edf9df0ae fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
  1722
2816
62edd11b5e66 commentary
Claus Gittinger <cg@exept.de>
parents: 2793
diff changeset
  1723
    "Modified: 31.7.1997 / 13:18:28 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1724
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1725
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1726
digits
7288
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1727
    "obsolete, use #digitBytes"
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1728
36b9824c75df Mark obsolete methods
Stefan Vogel <sv@exept.de>
parents: 7141
diff changeset
  1729
    <resource:#obsolete>
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1730
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1731
    ^ digitByteArray
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1732
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1733
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1734
!LargeInteger methodsFor:'coercing & converting'!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1735
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1736
asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1737
    "return a LargeInteger with same value as myself - thats me"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1738
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1739
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1740
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1741
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1742
asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1743
    "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
  1744
     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
  1745
     convert C/Java numbers."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1746
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1747
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1748
    int rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1749
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1750
    rslt =  (int)(__unsignedLongIntVal(self));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1751
    RETURN ( __MKINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1752
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1753
    self primitiveFailed
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
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1756
     16r80000000 asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1757
     16r40000000 asSigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1758
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1759
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1760
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1761
asSmallInteger
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1762
    "return a SmallInteger with same value as myself -
18845
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1763
     the result is invalid if the receiver's value cannot
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1764
     be represented as a SmallInteger.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1765
     Q: should we raise an exception if this happens ?"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1766
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1767
    |value|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1768
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1769
    value := 0.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1770
    (sign == 0) ifFalse:[
18845
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1771
        digitByteArray reverseDo:[:aDigit |
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1772
            value := (value times:256) + aDigit
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1773
        ].
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1774
        (sign < 0) ifTrue:[
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1775
            value := value negated
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1776
        ]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1777
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1778
    ^ value
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
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1781
asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1782
    "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
  1783
     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
  1784
     convert C/Java numbers."
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1785
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1786
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1787
    unsigned int rslt;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1788
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1789
    rslt =  (int)(__unsignedLongIntVal(self));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1790
    RETURN ( __MKUINT(rslt));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1791
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1792
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1793
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1794
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1795
     16r80000000 asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1796
     16r40000000 asUnsigned32
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1797
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1798
!
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  1799
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1800
coerce:aNumber
18845
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  1801
    "convert the argument aNumber into an instance of the receiver's class and return it."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1802
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1803
    ^ aNumber asLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1804
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1805
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  1806
compressed
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1807
    "if the receiver can be represented as a SmallInteger, return
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1808
     a SmallInteger with my value;
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1809
     otherwise remove leading zero bytes in the digitByte array
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1810
     and return the receiver"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1811
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1812
    |index "{ Class: SmallInteger }"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1813
     val   "{ Class: SmallInteger }"
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1814
     index0
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  1815
    |
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1816
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1817
%{  /* NOCONTEXT */
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1818
    OBJ t;
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1819
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1820
    if (__INST(sign) == __mkSmallInteger(0)) {
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1821
	RETURN (__mkSmallInteger(0));
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1822
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1823
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1065
diff changeset
  1824
    t = __INST(digitByteArray);
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1825
    if (__isByteArray(t)) {
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1826
	unsigned char *__digitBytes = __ByteArrayInstPtr(t)->ba_element;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1827
	int _idx, _idx0;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1828
	INT _val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1829
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1830
	_idx = _idx0 = __byteArraySize(t);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1831
	while ((_idx > 0) && (__digitBytes[_idx - 1] == 0)) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1832
	    _idx--;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1833
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  1834
#if __POINTER_SIZE__ == 8
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1835
	switch (_idx) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1836
	    case 8:
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1837
		_val = __digitBytes[7];
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1838
		if (_val <= 0x40) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1839
		    _val = (_val<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1840
		    _val = (_val + __digitBytes[6]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1841
		    _val = (_val + __digitBytes[5]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1842
		    _val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1843
		    _val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1844
		    _val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1845
		    _val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1846
		    _val += __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1847
		    if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1848
			_val = -_val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1849
		    if (__ISVALIDINTEGER(_val)) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1850
			RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1851
		    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1852
		}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1853
		break;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1854
	    case 7:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1855
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1856
		_val = ((INT *)__digitBytes)[0] & 0x00FFFFFFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1857
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1858
		_val = (__digitBytes[6]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1859
		_val = (_val + __digitBytes[5]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1860
		_val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1861
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1862
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1863
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1864
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1865
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1866
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1867
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1868
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1869
	    case 6:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1870
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1871
		_val = ((INT *)__digitBytes)[0] & 0x0000FFFFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1872
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1873
		_val = (__digitBytes[5]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1874
		_val = (_val + __digitBytes[4]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1875
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1876
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1877
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1878
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1879
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1880
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1881
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1882
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1883
	    case 5:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1884
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1885
		_val = ((INT *)__digitBytes)[0] & 0x000000FFFFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1886
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1887
		_val = (__digitBytes[4]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1888
		_val = (_val + __digitBytes[3]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1889
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1890
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1891
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1892
# endif
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 4:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1897
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1898
		_val = ((INT *)__digitBytes)[0] & 0x00000000FFFFFFFFL;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1899
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1900
		_val = (__digitBytes[3]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1901
		_val = (_val + __digitBytes[2]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1902
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1903
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1904
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1905
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1906
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1907
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1908
	    case 3:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1909
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1910
		_val = ((int *)__digitBytes)[0] & 0x00FFFFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1911
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1912
		_val = (__digitBytes[2]<<8);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1913
		_val = (_val + __digitBytes[1]) << 8;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1914
		_val += __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1915
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1916
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1917
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1918
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1919
	    case 2:
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1920
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1921
		_val = ((int *)__digitBytes)[0] & 0x0000FFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1922
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1923
		_val = (__digitBytes[1]<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1924
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1925
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1926
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1927
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1928
	    case 1:
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1929
		_val = __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1930
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1931
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1932
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1933
	    case 0:
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1934
		RETURN (__mkSmallInteger(0));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1935
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1936
	}
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  1937
#else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1938
	if (_idx <= 4) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1939
	    if (_idx <= 2) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1940
		if (_idx == 0) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1941
		    RETURN (__mkSmallInteger(0));
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
		if (_idx == 1) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1944
		    _val = __digitBytes[0];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1945
		    if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1946
			_val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1947
		    RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1948
		}
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1949
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1950
		_val = ((int *)__digitBytes)[0] & 0x0000FFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1951
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1952
		_val = (__digitBytes[1]<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1953
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1954
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1955
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1956
		RETURN (__mkSmallInteger(_val));
6665
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
	    if (_idx == 3) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1959
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1960
		_val = ((int *)__digitBytes)[0] & 0x00FFFFFF;
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1961
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1962
		_val = (((__digitBytes[2]<<8) + __digitBytes[1])<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1963
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1964
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1965
		    _val = -_val;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1966
		RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1967
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1968
	    _val = __digitBytes[3];
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1969
	    if (_val <= 0x40) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1970
# if defined(__LSBFIRST__)
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1971
		_val = ((int *)__digitBytes)[0];
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1972
# else
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1973
		_val = (((((_val<<8) + __digitBytes[2])<<8) + __digitBytes[1])<<8) + __digitBytes[0];
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1974
# endif
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1975
		if (__INST(sign) == __mkSmallInteger(-1))
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1976
		    _val = -_val;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1977
		if (__ISVALIDINTEGER(_val)) {
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  1978
		    RETURN (__mkSmallInteger(_val));
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1979
		}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1980
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1981
	}
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  1982
#endif
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1983
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1984
	if (_idx == _idx0) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1985
	    RETURN (self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1986
	}
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1987
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1988
	/*
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  1989
	 * must copy & cut off some (zero)bytes
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1990
	 */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1991
	{
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1992
	    OBJ newDigits;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1993
	    OBJ oldDigits;
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1994
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1995
	    /*
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1996
	     * careful - there is no context here to protect
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1997
	     * the receiver ...
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1998
	     */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  1999
	    __PROTECT__(self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2000
	    __PROTECT__(__INST(digitByteArray));
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2001
	    newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2002
	    __UNPROTECT__(oldDigits);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2003
	    __UNPROTECT__(self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2004
	    if (newDigits) {
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2005
		bcopy(__ByteArrayInstPtr(oldDigits)->ba_element,
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2006
		      __ByteArrayInstPtr(newDigits)->ba_element,
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2007
		      _idx);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2008
		__INST(digitByteArray) = newDigits; __STORE(self, newDigits);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2009
		RETURN (self);
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2010
	    }
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2011
	    /*
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2012
	     * allocation failed ...
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2013
	     * ... fall through to trigger the error
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2014
	     */
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2015
	}
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2016
    }
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2017
%}.
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  2018
    index0 := index := digitByteArray size.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2019
    [(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2020
	index := index - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2021
    ].
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2022
"/    ((index < SmallInteger maxBytes)
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2023
"/    or:[(index == SmallInteger maxBytes)
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2024
"/            and:[(digitByteArray at:index) < 16r20]])
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2025
"/    ifTrue:[
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2026
"/        val := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2027
"/        1 to:index do:[:i |
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2028
"/            val := val bitShift:8.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2029
"/            val := val bitOr:(digitByteArray at:i).
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2030
"/        ].
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2031
"/        ^ val * sign
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  2032
"/    ].
2267
123ec897aca5 tuned #compressed
Claus Gittinger <cg@exept.de>
parents: 2001
diff changeset
  2033
    (index ~~ index0) ifTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2034
	digitByteArray := digitByteArray copyFrom:1 to:index
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2035
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2036
    ^ self
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2037
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2038
7446
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2039
generality
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2040
    "return the generality value - see ArithmeticValue>>retry:coercing:"
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2041
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2042
    ^ 40
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2043
!
81d4bbeb830e double dispatching
Claus Gittinger <cg@exept.de>
parents: 7405
diff changeset
  2044
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2045
nilAllInstvars
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2046
    digitByteArray nilAllInstvars.
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2047
    digitByteArray := sign := nil.
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2048
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2049
    "
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2050
      100 factorial nilAllInstvars
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2051
    "
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2052
!
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2053
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2054
value:aSmallInteger
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2055
    "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
  2056
     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
  2057
     This should only be used internally,
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2058
     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
  2059
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2060
    |absValue
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2061
     b1 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2062
     b2 "{ Class: SmallInteger }"
2786
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2063
     b3 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2064
     b4 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2065
     b5 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2066
     b6 "{ Class: SmallInteger }"
8f05ce0a14e1 alpha64 changes
Claus Gittinger <cg@exept.de>
parents: 2521
diff changeset
  2067
     b7 "{ Class: SmallInteger }"|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2068
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2069
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2070
     could have simply created a 4-byte largeinteger and normalize it.
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2071
     The code below does the normalize right away, avoiding the
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2072
     overhead of producing any intermediate byte-arrays (and the scanning)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2073
    "
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2074
    (aSmallInteger == 0) ifTrue: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2075
	digitByteArray := ByteArray with:0.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2076
	sign := 0.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2077
	^ self
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2078
    ].
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2079
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2080
    (aSmallInteger < 0) ifTrue: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2081
	sign := -1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2082
	absValue := aSmallInteger negated
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2083
    ] ifFalse: [
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2084
	sign := 1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2085
	absValue := aSmallInteger
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2086
    ].
3431
ae8f96921fcc tuning & comments
Claus Gittinger <cg@exept.de>
parents: 3428
diff changeset
  2087
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2088
    b1 := absValue bitAnd:16rFF.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2089
    absValue := absValue bitShift:-8.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2090
    absValue == 0 ifTrue:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2091
	digitByteArray := ByteArray with:b1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2092
    ] ifFalse:[
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2093
	b2 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2094
	absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2095
	absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2096
	    digitByteArray := ByteArray with:b1 with:b2
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2097
	] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2098
	    b3 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2099
	    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2100
	    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2101
		digitByteArray := ByteArray with:b1 with:b2 with:b3
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2102
	    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2103
		b4 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2104
		absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2105
		absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2106
		    digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2107
		] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2108
		    b5 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2109
		    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2110
		    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2111
			digitByteArray := ByteArray new:5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2112
			digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2113
			digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2114
			digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2115
			digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2116
			digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2117
		    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2118
			b6 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2119
			absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2120
			absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2121
			    digitByteArray := ByteArray new:6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2122
			    digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2123
			    digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2124
			    digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2125
			    digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2126
			    digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2127
			    digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2128
			] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2129
			    b7 := absValue bitAnd:16rFF.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2130
			    absValue := absValue bitShift:-8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2131
			    absValue == 0 ifTrue:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2132
				digitByteArray := ByteArray new:7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2133
				digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2134
				digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2135
				digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2136
				digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2137
				digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2138
				digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2139
				digitByteArray at:7 put:b7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2140
			    ] ifFalse:[
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2141
				digitByteArray := ByteArray new:8.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2142
				digitByteArray at:1 put:b1.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2143
				digitByteArray at:2 put:b2.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2144
				digitByteArray at:3 put:b3.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2145
				digitByteArray at:4 put:b4.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2146
				digitByteArray at:5 put:b5.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2147
				digitByteArray at:6 put:b6.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2148
				digitByteArray at:7 put:b7.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2149
				digitByteArray at:8 put:absValue.
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2150
			    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2151
			]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2152
		    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2153
		]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2154
	    ]
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  2155
	]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2156
    ]
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2157
4230
e1cfaecf453c alpha64 bug fixes
Claus Gittinger <cg@exept.de>
parents: 4229
diff changeset
  2158
    "Modified: / 26.5.1999 / 22:18:14 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2159
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2160
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2161
!LargeInteger methodsFor:'comparing'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2162
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2163
< aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2164
    "return true, if the argument, aNumber is greater than the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2165
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2166
    |otherSign|
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
    (aNumber class == self class) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2169
	otherSign := aNumber sign.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2170
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2171
	(sign > 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2172
	    "I am positive"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2173
	    (otherSign > 0) ifTrue:[^ self absLess:aNumber].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2174
	    ^ false "aNumber is <= 0"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2175
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2176
	"I am negative"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2177
	(otherSign > 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2178
	(otherSign == 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2179
	^ (aNumber absLess:self)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2180
    ].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2181
    (aNumber class == SmallInteger) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2182
	otherSign := aNumber sign.
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2183
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2184
	(sign > 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2185
	    "I am positive"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2186
	    ^ false "aNumber is <= 0"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2187
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2188
	(sign == 0) ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2189
	    (otherSign > 0) ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2190
	    ^ false
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2191
	].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2192
	"I am negative"
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2193
	^ true
14766
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2194
    ].
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2195
    "/ hack for epsilon tests
ae12897bef55 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 13992
diff changeset
  2196
    (aNumber class == Float) ifTrue:[
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2197
	self negative ifTrue:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2198
	    "/ I am a large negative; so my value is definitely below SmallInteger minVal
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2199
	    aNumber >= SmallInteger minVal asFloat ifTrue:[^ true].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2200
	] ifFalse:[
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2201
	    "/ I am a large positive; so my value is definitely above SmallInteger maxVal
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2202
	    aNumber <= SmallInteger maxVal asFloat ifTrue:[^ false].
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2203
	].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2204
    ].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2205
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2206
    ^ aNumber lessFromInteger:self
3178
649320240430 fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents: 3161
diff changeset
  2207
6677
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2208
    "Modified: / 31.7.2002 / 10:08:19 / cg"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2209
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2210
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2211
= aNumber
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2212
    "return true, if the argument represents the same numeric value
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2213
     as the receiver, false otherwise"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2214
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2215
    "/ speed up compare to 0
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2216
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2217
    (aNumber == 0) ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2218
	^ sign == 0
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2219
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2220
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2221
    (aNumber class == self class) ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2222
	"/
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2223
	"/ here, we depend on the fact, that largeinteger
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2224
	"/ results are always converted to smallInts, if possible.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2225
	"/ therefore, a largeInt in the smallInt range is not allowed (possible)
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2226
	"/
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2227
	aNumber class == SmallInteger ifTrue:[^ false ].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2228
	^ aNumber equalFromInteger:self
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2229
    ].
7356
fe8fb0a571f2 double dispatching fixed;
Claus Gittinger <cg@exept.de>
parents: 7288
diff changeset
  2230
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2231
    (aNumber sign == sign) ifFalse:[^ false].
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2232
    ^ 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
  2233
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2234
    "Modified: / 13.2.1998 / 11:43:15 / stefan"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2235
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2236
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2237
> aNumber
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2238
    "return true, if the argument, aNumber is less than the receiver"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2239
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2240
    |otherSign|
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2241
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2242
    (aNumber class == self class) ifFalse:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2243
	^ (aNumber < self)
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2244
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2245
    otherSign := aNumber sign.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2246
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2247
    (sign > 0) ifTrue:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2248
	"I am positive"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2249
	(otherSign > 0) ifTrue:[^ aNumber absLess:self].
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2250
	^ true "aNumber is <= 0"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2251
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2252
    (sign == 0) ifTrue:[
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2253
	"I am zero"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2254
	(otherSign > 0) ifTrue:[^ false].
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2255
	^ true
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2256
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2257
    "I am negative"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2258
    (otherSign > 0) ifTrue:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2259
    (otherSign == 0) ifTrue:[^ false].
6677
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2260
    ^ (self absLess:aNumber)
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2261
6467fed79219 double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents: 6665
diff changeset
  2262
    "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
  2263
!
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2264
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2265
hash
6055c1b2fc3b better bitAnd: for non-LSB machines;
Claus Gittinger <cg@exept.de>
parents: 4612
diff changeset
  2266
    "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
  2267
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2268
    |l h m|
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2269
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2270
    h := self bitAnd:16r3FFFFFFF.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2271
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2272
    l := digitByteArray size.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2273
    l >= 8 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2274
	h := h bitXor:(digitByteArray at:l).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2275
	h := h bitXor:((digitByteArray at:l-1) bitShift:8).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2276
	h := h bitXor:((digitByteArray at:l-2) bitShift:16).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2277
	h := h bitXor:((digitByteArray at:l-3) bitShift:22).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2278
	l >= 12 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2279
	    m := l // 2.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2280
	    h := h bitXor:(digitByteArray at:m-1).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2281
	    h := h bitXor:((digitByteArray at:m) bitShift:8).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2282
	    h := h bitXor:((digitByteArray at:m+1) bitShift:16).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2283
	    h := h bitXor:((digitByteArray at:m+2) bitShift:22).
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2284
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2285
	^ h
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2286
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2287
    ^ (h bitShift:3) + l
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2288
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2289
    "
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2290
     16r80000000 hash
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2291
     16r80000008 hash
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2292
     16r8000000000008 hash
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2293
    "
17246
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2294
!
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2295
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2296
hashMultiply
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2297
    "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
  2298
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2299
    "Truncate to 28 bits and try again"
0e609e06e051 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 17128
diff changeset
  2300
    ^(self bitAnd: 16rFFFFFFF) hashMultiply
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2301
! !
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2302
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2303
!LargeInteger methodsFor:'double dispatching'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2304
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2305
differenceFromInteger:anInteger
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2306
    "sent, when anInteger does not know how to subtract the receiver.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2307
     Return the result of 'anInteger - self'. The argument must be a SmallInteger."
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2308
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2309
    anInteger > 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2310
	sign > 0 ifTrue:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2311
	    ^ self absFastMinus:anInteger sign:-1
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2312
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2313
	^ self absFastPlus:anInteger sign:1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2314
    ].
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2315
    anInteger == 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2316
	^ self negated
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2317
    ].
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2318
    sign > 0 ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2319
	^ self absFastPlus:anInteger negated sign:-1
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2320
    ].
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2321
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2322
    self > anInteger ifTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2323
	^ self absFastMinus:anInteger asLargeInteger sign:-1
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2324
    ] ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2325
	^ anInteger asLargeInteger absFastMinus:self sign:-1
10728
ec5790d03608 another difference bug
sr
parents: 10726
diff changeset
  2326
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2327
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2328
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2329
     12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2330
     -12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2331
     12345678901234567890 differenceFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2332
     12345678901234567890 differenceFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2333
     12345678901234567890 differenceFromInteger:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2334
     -12345678901234567890 differenceFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2335
     -12345678901234567890 differenceFromInteger:-1
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2336
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2337
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2338
    "Modified: 20.10.1996 / 18:41:54 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2339
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2340
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2341
equalFromInteger:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2342
    "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
  2343
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2344
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2345
    "/ here, we depend on the fact, that largeinteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2346
    "/ results are always converted to smallInts, if possible.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2347
    "/ therefore, a largeInt in the smallInt range is not allowed (possible)
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2348
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2349
    anInteger class == SmallInteger ifTrue:[^ false ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2350
    anInteger class == self class ifFalse:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2351
	^ super equalFromInteger:anInteger
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2352
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2353
    (anInteger sign == sign) ifFalse:[^ false].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2354
    ^ self absEq:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2355
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2356
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2357
lessFromInteger:anInteger
18855
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2358
    "sent when an integer does not know how to compare to the receiver, a largeInt.
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2359
     Return true if anInteger < self"
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2360
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2361
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2362
    "/ here, we depend on the fact, that largeinteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2363
    "/ results are always converted to smallInts, if possible.
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2364
    "/ therefore, a largeInt in the smallInt range is not allowed (possible)
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2365
    "/
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2366
    (anInteger class == SmallInteger) ifTrue:[
18855
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2367
        (sign > 0) ifTrue:[
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2368
            "I am positive - any largeInteger is larger than any smallInteger"
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2369
            ^ true "anInteger is <= 0"
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2370
        ].
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2371
        "I am negative - any negative largeInteger is smaller than any smallInteger"
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  2372
        ^ false
11507
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2373
    ].
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2374
    ^ super lessFromInteger:anInteger
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2375
!
111d9a470036 tuned < and =;
Claus Gittinger <cg@exept.de>
parents: 11501
diff changeset
  2376
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2377
productFromInteger:anInteger
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2378
    "sent, when anInteger does not know how to multiply the receiver.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2379
     Return the product of the receiver and the argument, aSmallInteger"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2380
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2381
    |num result
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2382
     resultDigitByteArray
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2383
     val     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2384
     len     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2385
     carry   "{ Class: SmallInteger }"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2386
     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
  2387
     ok lResult|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2388
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2389
    "multiplying by a small integer is done here"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2390
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2391
    "trivial cases"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2392
    anInteger == 0 ifTrue:[^ 0].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2393
    anInteger == 1 ifTrue:[^ self].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2394
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2395
    num := anInteger abs.
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2396
    SmallInteger maxBytes == 8 ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2397
        (num > 16r3FFFFFFF) ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2398
            "if num is too big (so that multiplying by a byte could create a Large)"
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2399
            ^ anInteger retry:#* coercing:self
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2400
        ].
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2401
    ] ifFalse:[    
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2402
        (num > 16r3FFFFF) ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2403
            "if num is too big (so that multiplying by a byte could create a Large)"
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2404
            ^ anInteger retry:#* coercing:self
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2405
        ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2406
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2407
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2408
    len := digitByteArray size.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2409
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2410
    val := num.
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
  2411
    val <= 16rFF ifTrue:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2412
        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
  2413
    ] ifFalse:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2414
        val <= 16rFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2415
            lResult := len + 2
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2416
        ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2417
            val <= 16rFFFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2418
                lResult := len + 4.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2419
            ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2420
                val <= 16rFFFFFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2421
                    lResult := len + 6.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2422
                ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2423
                    val <= 16rFFFFFFFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2424
                        lResult := len + 8.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2425
                    ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2426
                        val <= 16rFFFFFFFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2427
                            lResult := len + 10.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2428
                        ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2429
                            val <= 16rFFFFFFFFFFFF ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2430
                                lResult := len + 12.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2431
                            ] ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2432
                                lResult := len + 14.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2433
                            ]
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2434
                        ]
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2435
                    ]
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2436
                ]
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2437
            ]
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2438
        ]
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2439
    ].
2933
8b65cb642d91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2923
diff changeset
  2440
    resultDigitByteArray := ByteArray uninitializedNew:lResult.
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2855
diff changeset
  2441
    result := self class basicNew setDigits:resultDigitByteArray.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2442
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2443
    anInteger < 0 ifTrue:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2444
        sign > 0 ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2445
            result setSign:-1
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2446
        ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2447
    ] ifFalse:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2448
        sign < 0 ifTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2449
            result setSign:sign
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2450
        ]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2451
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2452
85
claus
parents: 77
diff changeset
  2453
    ok := false.
claus
parents: 77
diff changeset
  2454
%{
2471
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2455
    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
  2456
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2457
    if (__isSmallInteger(len)
035dbf8db9f3 faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents: 2267
diff changeset
  2458
     && __isByteArray(__digitByteArray)
85
claus
parents: 77
diff changeset
  2459
     && __isByteArray(resultDigitByteArray)) {
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2460
        INT _l = __intVal(len);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2461
        INT _v = __intVal(val);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2462
        unsigned INT _carry = 0;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2463
        unsigned INT _prod;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2464
        unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2465
        unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2466
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2467
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2468
         * skipping zeros does not help much (a few percent) on
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2469
         * a P5 or other CPUS with a fast multiplier.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2470
         * It may make more of a difference on CPUs with slower 0-multiply.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2471
         * Late news: it actually hurts modern x86_64 cpus.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2472
         * So only reenable for specific CPUs after concrete benchmarks.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2473
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2474
#if 0
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2475
        while ((_l >= sizeof(INT)) && (((unsigned INT *)digitP)[0] == 0)) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2476
            ((unsigned INT *)resultP)[0] = 0;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2477
            digitP += sizeof(INT);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2478
            resultP += sizeof(INT);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2479
            _l -= sizeof(INT);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2480
        }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2481
#endif
2923
fbb470bc7e49 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2897
diff changeset
  2482
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  2483
#if defined(__LSBFIRST__)
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  2484
# if defined (__GNUC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2485
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2486
         * can do it long-word-wise;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2487
         * 32*32 -> 64 multiplication
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2488
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2489
        while (_l > 3) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2490
            unsigned __pHi, __pLow;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2491
            unsigned __digit;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2492
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2493
            /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2494
             * max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2495
             * + maxCarry (0xFFFF.FFFF)  -> 0xFFFF.FFFF.0000.0000
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2496
             */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2497
            __digit = ((unsigned long *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2498
            asm ("mull %3               \n\
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2499
                  addl %4,%%eax         \n\
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2500
                  adcl $0,%%edx"
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2501
                    : "=a"  (__pLow),
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2502
                      "=d"  (__pHi)
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2503
                    : "0"   (__digit),
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2504
                      "1"   ((unsigned long)(_v)),
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2505
                      "rm"  ((unsigned long)(_carry)) );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2506
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2507
            ((unsigned long *)resultP)[0] = __pLow;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2508
            _carry = __pHi;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2509
            digitP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2510
            resultP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2511
            _l -= 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2512
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2513
# else /* not GNU-i386 */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  2514
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2515
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2516
         * can do it long-word-wise;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2517
         * 32*32 -> 64 multiplication
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2518
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2519
        while (_l > 3) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2520
            unsigned __pLow;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2521
            unsigned digit;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2522
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2523
            /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2524
             * max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2525
             * + maxCarry (0xFFFF.FFFF)  -> 0xFFFF.FFFF.0000.0000
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2526
             */
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2527
/*
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2528
            digit = ((unsigned long *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2529
            edx::eax = (digit * _v);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2530
            edx::eax += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2531
            ((unsigned long *)resultP)[0] = eax; -- pLow
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2532
            _carry = edx; -- pHigh
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2533
            digitP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2534
            resultP += 4;
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2535
*/
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2536
            digit = ((unsigned long *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2537
            asm {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2538
                mov   eax, digit
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2539
                mov   edx, _v
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2540
                mul   edx
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2541
                add   eax, _carry
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2542
                adc   edx, 0
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2543
                mov   __pLow, eax
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2544
                mov   _carry, edx
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2545
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2546
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2547
            ((unsigned long *)resultP)[0] = __pLow;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2548
            digitP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2549
            resultP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2550
            _l -= 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2551
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2552
#  else /* not WIN32-i386 */
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2553
#   if defined(INT64)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2554
        if (_v <= 0xFFFFFFFFL) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2555
            /* have 64bit ints; can do it int-wise
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2556
             *
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2557
             * max: 0xFFFFFFFF * 0xFFFFFFFF -> 0xFFFFFFFE.0001
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2558
             * + maxCarry (0xFFFFFFFF)  -> 0xFFFFFFFF.0000
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2559
             */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2560
            while (_l >= (4+4+4+4)) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2561
                unsigned __t1, __t2, __t3, __t4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2562
                UINT64 _prod64a, _prod64b, _prod64c, _prod64d;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2563
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2564
                __t1 = ((unsigned *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2565
                _prod64a = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2566
                _prod64a *= __t1;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2567
                _prod64a += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2568
                ((unsigned *)resultP)[0] = _prod64a /* & 0xFFFFFFFFL */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2569
                _carry = _prod64a >> 32;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2570
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2571
                __t2 = ((unsigned *)digitP)[1];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2572
                _prod64b = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2573
                _prod64b *= __t2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2574
                _prod64b += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2575
                ((unsigned *)resultP)[1] = _prod64b /* & 0xFFFFFFFFL */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2576
                _carry = _prod64b >> 32;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2577
                
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2578
                __t3 = ((unsigned *)digitP)[2];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2579
                _prod64c = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2580
                _prod64c *= __t3;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2581
                _prod64c += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2582
                ((unsigned *)resultP)[2] = _prod64c /* & 0xFFFFFFFFL */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2583
                _carry = _prod64c >> 32;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2584
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2585
                __t4 = ((unsigned *)digitP)[3];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2586
                _prod64d = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2587
                _prod64d *= __t4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2588
                _prod64d += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2589
                ((unsigned *)resultP)[3] = _prod64d /* & 0xFFFFFFFFL */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2590
                _carry = _prod64d >> 32;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2591
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2592
                digitP += (4+4+4+4);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2593
                resultP += (4+4+4+4);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2594
                _l -= (4+4+4+4);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2595
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2596
            while (_l >= 4) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2597
                unsigned __t;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2598
                UINT64 _prod64;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2599
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2600
                __t = ((unsigned *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2601
                digitP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2602
                _prod64 = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2603
                _prod64 *= __t;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2604
                _prod64 += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2605
                ((unsigned *)resultP)[0] = _prod64 /* & 0xFFFFFFFFL */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2606
                _carry = _prod64 >> 32;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2607
                resultP += 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2608
                _l -= 4;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2609
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2610
            if (_l >= 2) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2611
                unsigned short __t;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2612
                UINT64 _prod64;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2613
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2614
                __t = ((unsigned short *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2615
                digitP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2616
                _prod64 = (INT64)_v;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2617
                _prod64 *= __t;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2618
                _prod64 += _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2619
                ((unsigned short *)resultP)[0] = _prod64 /* & 0xFFFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2620
                _carry = _prod64 >> 16;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2621
                resultP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2622
                _l -= 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2623
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2624
            if (_l > 0) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2625
                UINT64 _prod64;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2626
                _prod64 = *digitP++ * _v + _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2627
                *resultP++ = _prod64 /* & 0xFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2628
                _carry = _prod64 >> 8;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2629
                _l--;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2630
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2631
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2632
#   else /* no INT64 type */
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2633
        if (_v <= 0xFFFF) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2634
            /* can do it short-wise
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2635
             *
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2636
             * max: 0xFFFF * 0xFFFF -> 0xFFFE.0001
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2637
             * + maxCarry (0xFFFF)  -> 0xFFFF.0000
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2638
             */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2639
            while (_l > 1) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2640
                _prod = ((unsigned short *)digitP)[0] * _v + _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2641
                ((unsigned short *)resultP)[0] = _prod /* & 0xFFFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2642
                _carry = _prod >> 16;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2643
                digitP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2644
                resultP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2645
                _l -= 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2646
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2647
        }
4170
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2648
#   endif /* no INT64 */
4b7dee48537e inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents: 4164
diff changeset
  2649
#  endif /* not WIN32-i386 */
3040
ea839412d642 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2964
diff changeset
  2650
# endif /* not GNU-i386 */
3099
9b76bb13bc5e 16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents: 3040
diff changeset
  2651
#else /* not LSB_FIRST */
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2652
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  2653
# ifdef __mips__
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2654
#  define LOAD_WORD_WISE
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2655
   /* 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
  2656
# endif
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2657
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2658
        if (_v <= 0xFFFF) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2659
            /* can do it short-wise
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2660
             *
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2661
             * max: 0xFFFF * 0xFFFF -> 0xFFFE.0001
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2662
             * + maxCarry (0xFFFF)  -> 0xFFFF.0000
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2663
             */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2664
            while (_l > 1) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2665
                unsigned int t;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2666
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2667
#if defined(LOAD_WORD_WISE)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2668
                /* better fetch short-wise */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2669
                t = ((unsigned short *)digitP)[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2670
                digitP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2671
                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
  2672
#else
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2673
                t = (digitP[1]<<8) + digitP[0];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2674
                digitP += 2;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2675
#endif
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2676
                _prod = t * _v + _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2677
                _carry = _prod >> 16;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2678
#if defined(STORE_WORD_WISE)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2679
                /* better store short-wise */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2680
                _prod = ((_prod >> 8) | (_prod << 8)) & 0xFFFF;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2681
                ((unsigned short *)resultP)[0] = _prod;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2682
#else
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2683
                resultP[0] = _prod /* & 0xFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2684
                resultP[1] = (_prod>>8) /* & 0xFF */;
4192
8ee270c413a0 slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents: 4191
diff changeset
  2685
#endif
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2686
                resultP += 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2687
                _l -= 2;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2688
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2689
        }
3099
9b76bb13bc5e 16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents: 3040
diff changeset
  2690
3040
ea839412d642 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2964
diff changeset
  2691
#endif /* LSB_FIRST */
1797
8dafe47b33fd faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents: 1794
diff changeset
  2692
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2693
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2694
         * rest is done byte-wise
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2695
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2696
        while (_l > 0) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2697
            _prod = *digitP++ * _v + _carry;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2698
            *resultP++ = _prod /* & 0xFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2699
            _carry = _prod >> 8;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2700
            _l--;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2701
        }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2702
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2703
        _l = __intVal(lResult) - __intVal(len);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2704
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2705
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2706
         * remaining carry
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2707
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2708
        while (_carry) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2709
            *resultP++ = _carry /* & 0xFF */;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2710
            _carry >>= 8;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2711
            _l--;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2712
        }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2713
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2714
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2715
         * remaining zeros
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2716
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2717
        while (_l--) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2718
            *resultP++ = 0;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2719
        }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2720
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2721
        /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2722
         * need compress ?
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2723
         */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2724
        if (resultP[-1]) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2725
            /*
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2726
             * no
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2727
             */
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2728
            RETURN(result);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2729
        }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2730
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2731
        ok = true;
85
claus
parents: 77
diff changeset
  2732
    }
claus
parents: 77
diff changeset
  2733
%}.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2734
    "
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2735
     fall back - normally not reached
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2736
     (could make it a primitive-failure as well)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2737
    "
85
claus
parents: 77
diff changeset
  2738
    ok ifFalse:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2739
        carry := 0.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2740
        1 to:len do:[:i |
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2741
            prod := (digitByteArray basicAt:i) * val + carry.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2742
            resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF).
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2743
            carry := prod bitShift:-8.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2744
        ].
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2745
        [carry ~~ 0] whileTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2746
            len := len + 1.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2747
            resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF).
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2748
            carry := carry bitShift:-8
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2749
        ].
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2750
        [len < lResult] whileTrue:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2751
            len := len + 1.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2752
            resultDigitByteArray basicAt:len put:0
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  2753
        ]
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2754
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2755
    ^ result compressed
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2756
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2757
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2758
sumFromInteger:anInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2759
    "sent, when anInteger does not know how to add the receiver.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2760
     Return the sum of the receiver and the argument, (which must be a SmallInteger)"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2761
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2762
    anInteger > 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2763
	sign > 0 ifTrue:[
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2764
	    ^ self absFastPlus:anInteger sign:1
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2765
	].
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2766
	^ self absFastMinus:anInteger sign:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2767
    ].
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2768
    anInteger == 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2769
	^ self
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2770
    ].
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2771
    sign > 0 ifTrue:[
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  2772
	^ self absFastMinus:anInteger sign:1
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2773
    ].
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  2774
    ^ self absFastPlus:anInteger sign:-1
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2775
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2776
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2777
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2778
     12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2779
     -12345678901234567890
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2780
     12345678901234567890 sumFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2781
     -12345678901234567890 sumFromInteger:0
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2782
     12345678901234567890 sumFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2783
     12345678901234567890 sumFromInteger:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2784
     -12345678901234567890 sumFromInteger:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2785
     -12345678901234567890 sumFromInteger:-1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2786
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  2787
3161
923a6931dded avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents: 3119
diff changeset
  2788
    "Modified: / 9.1.1998 / 13:27:37 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2789
! !
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2790
15577
cc63cd495a01 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15569
diff changeset
  2791
!LargeInteger methodsFor:'modulo arithmetic'!
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2792
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2793
plus32:aNumber
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2794
    "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
  2795
     The argument must be another SmallInteger.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2796
     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
  2797
     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
  2798
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2799
%{  /* NOCONTEXT */
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2800
    INT sum;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2801
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2802
    sum =  __unsignedLongIntVal(self) + __unsignedLongIntVal(aNumber);
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2803
#if __POINTER_SIZE__ == 8
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2804
    sum &= 0xFFFFFFFFL;
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2805
#endif
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2806
    RETURN ( __MKUINT(sum));
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2807
%}.
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2808
    self primitiveFailed
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2809
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2810
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2811
     16r7FFFFFFF + 1       ->  2147483648
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2812
     16r7FFFFFFF plus32: 1 ->  -2147483648
12877
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2813
    "
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2814
! !
be732e8f6914 more 32 bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents: 12747
diff changeset
  2815
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2816
!LargeInteger methodsFor:'printing & storing'!
345
claus
parents: 250
diff changeset
  2817
5682
a1a516779f72 remove storeOn: use insteat the default printString
ca
parents: 5437
diff changeset
  2818
xxxstoreOn:aStream
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2819
    "append a representation of the receiver to aStream, which can
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2820
     be used to reconstruct the receiver."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2821
5682
a1a516779f72 remove storeOn: use insteat the default printString
ca
parents: 5437
diff changeset
  2822
    aStream nextPutAll:'('.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2823
    self printOn:aStream.
5954
d706efecb134 ZeroDivide has a parameter
Claus Gittinger <cg@exept.de>
parents: 5682
diff changeset
  2824
    aStream nextPutAll:' asLargeInteger)'
d706efecb134 ZeroDivide has a parameter
Claus Gittinger <cg@exept.de>
parents: 5682
diff changeset
  2825
! !
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2826
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2827
!LargeInteger methodsFor:'private'!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2828
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2829
absDivMod:anInteger
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2830
    "return an array with two LargeIntegers representing
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2831
     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
  2832
     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
  2833
     This method needs a rewrite."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2834
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2835
    |dividend divisor
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2836
     quo digits
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2837
     shift "{ Class: SmallInteger }" |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2838
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2839
    anInteger == 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2840
        ^ ZeroDivide raiseRequestWith:thisContext
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2841
    ].
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2842
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  2843
    self = anInteger ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2844
        ^ Array with:1 with:0
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2845
    ].
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2846
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2847
    shift := self highBit - anInteger highBit.
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  2848
    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
  2849
    shift < 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2850
        ^ Array with:0 with:dividend compressed.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2851
    ].
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  2852
    shift == 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2853
        divisor := self class digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy.
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  2854
    ] ifFalse:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2855
        divisor := anInteger bitShift:shift.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2856
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2857
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2858
    quo := self class basicNew numberOfDigits:((shift // 8) + 1).
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  2859
    digits := quo digitBytes.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2860
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2861
    shift := shift + 1.
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2862
    [shift > 0] whileTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2863
        (dividend absLess:divisor) ifFalse:[
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2864
            digits bitSetAt:shift.
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2865
            (dividend absSubtract: divisor) ifFalse:[ "result == 0"
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2866
                ^ Array with:quo compressed with:0
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2867
            ].
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2868
        ].
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2869
        shift := shift - 1.
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2870
        divisor div2.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2871
    ].
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2872
    ^ 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
  2873
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2874
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2875
     Time millisecondsToRun:[ 10000 timesRepeat:[  16000000000 absDivMod:4000000000] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2876
     Time millisecondsToRun:[ 10000 timesRepeat:[  16000000000 absDivMod:3000000000] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2877
     16000000000 absDivMod:4000000000
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2878
     16000000000 absDivMod:3000000000
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  2879
     160000000000000000000000 absDivMod:160000000000000000000000
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2880
    "
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2881
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  2882
    "Modified: / 5.11.1996 / 18:40:24 / cg"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2883
    "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
  2884
    "Modified: / 26.7.1999 / 10:46:36 / stefan"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2885
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2886
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2887
absEq:aLargeInteger
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2888
    "return true, if abs(self) = abs(theArgument)"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2889
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2890
    |len1 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2891
     len2 "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2892
     d1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2893
     d2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2894
     otherDigitByteArray |
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2895
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2896
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2897
    if (__isLargeInteger(aLargeInteger)) {
15240
adde35a78b78 compression
Claus Gittinger <cg@exept.de>
parents: 15132
diff changeset
  2898
	OBJ _digitByteArray = __INST(digitByteArray);
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2899
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2900
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2901
	if (__isByteArray(_digitByteArray)
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2902
	 && __isByteArray(_otherDigitByteArray)) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2903
	    INT _myLen = __byteArraySize(_digitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2904
	    INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2905
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2906
	    unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2907
	    unsigned char *_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2908
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2909
	    if (_myLen == _otherLen) {
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2910
tryAgain:
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2911
		while (_myLen >= (sizeof(INT)*4)) {
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2912
		    if ( ((unsigned INT *)_myDigits)[0] != ((unsigned INT *)_otherDigits)[0]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2913
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2914
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2915
		    if ( ((unsigned INT *)_myDigits)[1] != ((unsigned INT *)_otherDigits)[1]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2916
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2917
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2918
		    if ( ((unsigned INT *)_myDigits)[2] != ((unsigned INT *)_otherDigits)[2]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2919
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2920
		    }
12878
7aa4debd20eb Fix primitive code in #absEq:
Stefan Vogel <sv@exept.de>
parents: 12877
diff changeset
  2921
		    if ( ((unsigned INT *)_myDigits)[3] != ((unsigned INT *)_otherDigits)[3]) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2922
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2923
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2924
		    _myDigits += sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2925
		    _otherDigits += sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2926
		    _myLen -= sizeof(INT)*4;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2927
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2928
		while (_myLen >= (sizeof(INT))) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2929
		    if ( *(unsigned INT *)_myDigits != *(unsigned INT *)_otherDigits) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2930
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2931
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2932
		    _myDigits += sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2933
		    _otherDigits += sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2934
		    _myLen -= sizeof(INT);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2935
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2936
		while (_myLen > 0) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2937
		    if ( *_myDigits != *_otherDigits) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2938
			RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2939
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2940
		    _myDigits++;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2941
		    _otherDigits++;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2942
		    _myLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2943
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2944
		RETURN(true);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2945
	    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2946
	    /* care for unnormalized ints */
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2947
	    while ((_myLen > 0) && (_myDigits[_myLen-1] == 0)) _myLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2948
	    while ((_otherLen > 0) && (_otherDigits[_otherLen-1] == 0)) _otherLen--;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2949
	    if (_myLen == _otherLen) goto tryAgain;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2950
	    RETURN(false);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2951
	}
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2952
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2953
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  2954
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2955
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  2956
    otherDigitByteArray := aLargeInteger digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2957
    len2 := otherDigitByteArray size.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2958
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2959
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2960
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2961
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2962
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2963
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2964
    [(digitByteArray basicAt:len1) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2965
	len1 := len1 - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2966
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2967
    [(otherDigitByteArray basicAt:len2) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2968
	len2 := len2 - 1
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2969
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2970
    (len1 ~~ len2) ifTrue:[^ false].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2971
    [len1 > 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2972
	d1 := digitByteArray basicAt:len1.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2973
	d2 := otherDigitByteArray basicAt:len1.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2974
	(d1 ~~ d2) ifTrue:[^ false].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  2975
	len1 := len1 - 1
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2976
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2977
    ^ true
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2978
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  2979
    "Modified: / 8.5.1999 / 18:37:02 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2980
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2981
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  2982
absFastDivMod:aPositiveSmallInteger
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2983
    "return an array with two LargeIntegers representing
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  2984
     abs(self) // aPositiveSmallInteger and
4180
cdfbc649b10d slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents: 4179
diff changeset
  2985
     abs(self) \\ aPositiveSmallInteger."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2986
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2987
    |tmp1     "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2988
     prevRest "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2989
     count    "{ Class: SmallInteger }"
85
claus
parents: 77
diff changeset
  2990
     newDigitByteArray result
claus
parents: 77
diff changeset
  2991
     ok|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2992
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2993
    aPositiveSmallInteger == 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2994
	^ ZeroDivide raiseRequestWith:thisContext
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2995
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2996
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2997
"This cannot happen (if always normalized)
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  2998
    self < aPositiveSmallInteger ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  2999
	^ Array with:0 with:self
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3000
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3001
"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3002
    count := digitByteArray size.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3003
    result := self class basicNew numberOfDigits:count.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3004
    newDigitByteArray := result digitBytes.
85
claus
parents: 77
diff changeset
  3005
    ok := false.
claus
parents: 77
diff changeset
  3006
%{
1842
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  3007
    OBJ __digits;
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  3008
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  3009
    __digits = __INST(digitByteArray);
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  3010
b28609c73839 added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents: 1797
diff changeset
  3011
    if (__isByteArray(__digits)
85
claus
parents: 77
diff changeset
  3012
     && __isByteArray(newDigitByteArray)
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3013
     && __bothSmallInteger(count, aPositiveSmallInteger)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3014
	unsigned INT rest = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3015
	int index = __intVal(count);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3016
	int index0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3017
	unsigned INT divisor = __intVal(aPositiveSmallInteger);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3018
	unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3019
	unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element;
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
	index0 = index - 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3022
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3023
# if (__POINTER_SIZE__ == 8)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3024
	if (sizeof(int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3025
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3026
	     * divide int-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3027
	     */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3028
	    if (divisor <= 0xFFFFFFFF) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3029
		if ((index & 3) == 0) { /* even number of int32's */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3030
		    while (index > 3) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3031
			unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3032
			unsigned INT div;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3033
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3034
			index -= 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3035
# if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3036
			t = *((unsigned int *)(&digitBytes[index]));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3037
# else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3038
			t = digitBytes[index+3];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3039
			t = (t << 8) | digitBytes[index+2];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3040
			t = (t << 8) | digitBytes[index+1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3041
			t = (t << 8) | digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3042
# endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3043
			t = t | (rest << 32);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3044
			div = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3045
			rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3046
# if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3047
			*((unsigned int *)(&resultBytes[index])) = (div & 0xFFFFFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3048
# else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3049
			resultBytes[index+3] = div >> 24;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3050
			resultBytes[index+2] = div >> 16;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3051
			resultBytes[index+1] = div >> 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3052
			resultBytes[index] = div /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3053
# endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3054
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3055
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3056
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3057
	}
5014
fa22b711e749 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5008
diff changeset
  3058
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3059
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3060
	 * divide short-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3061
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3062
	if (divisor <= 0xFFFF) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3063
	    if ((index & 1) == 0) { /* even number of bytes */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3064
		while (index > 1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3065
		    unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3066
		    unsigned INT div;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3067
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3068
		    index -= 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3069
#if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3070
		    t = *((unsigned short *)(&digitBytes[index]));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3071
#else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3072
		    t = digitBytes[index+1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3073
		    t = (t << 8) | digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3074
#endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3075
		    t = t | (rest << 16);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3076
		    div = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3077
		    rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3078
#if defined(__LSBFIRST__)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3079
		    *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3080
#else
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3081
		    resultBytes[index+1] = div >> 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3082
		    resultBytes[index] = div /* & 0xFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3083
#endif
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3084
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3085
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3086
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3087
	while (index > 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3088
	    unsigned INT t;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3089
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3090
	    index--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3091
	    t = digitBytes[index];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3092
	    t = t | (rest << 8);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3093
	    resultBytes[index] = t / divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3094
	    rest = t % divisor;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3095
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3096
	prevRest = __mkSmallInteger(rest);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3097
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3098
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3099
	 * no need to normalize ?
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3100
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3101
	index = index0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3102
	while ((index >= sizeof(INT)) && (resultBytes[index]==0)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3103
	    index--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3104
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3105
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3106
	if (index == index0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3107
	    if (index > sizeof(INT)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3108
		RETURN ( __ARRAY_WITH2(result, prevRest));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3109
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3110
	    if ((index == sizeof(INT))
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3111
	    && resultBytes[index0] >= 0x40) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3112
		RETURN ( __ARRAY_WITH2(result, prevRest));
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3113
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3114
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3115
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3116
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3117
	 * must compress
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3118
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3119
	ok = true;
85
claus
parents: 77
diff changeset
  3120
    }
claus
parents: 77
diff changeset
  3121
%}.
claus
parents: 77
diff changeset
  3122
    "
claus
parents: 77
diff changeset
  3123
     slow code - not normally reached
claus
parents: 77
diff changeset
  3124
     (could also do a primitiveFailure here)
claus
parents: 77
diff changeset
  3125
    "
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  3126
    ok ifFalse:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  3127
	^ self absDivMod:(self class value:aPositiveSmallInteger).
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3128
    ].
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3129
2933
8b65cb642d91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2923
diff changeset
  3130
    ^ 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
  3131
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3132
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3133
     16r123412341234 asLargeInteger absDivMod:(LargeInteger value:10)
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  3134
     ((16r1234 asLargeInteger absFastDivMod:16rffff) at:2) printStringRadix:16
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3135
     ((16r00123456 asLargeInteger absFastDivMod:16rffffff) at:2) printStringRadix:16
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3136
     ((666666666666666666 asLargeInteger absFastDivMod:16r3))
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3137
     ((1666666666 asLargeInteger absFastDivMod:16r3))
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3138
    "
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3139
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  3140
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3141
absFastMinus:aSmallInteger sign:newSign
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3142
    "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
  3143
     with sign: newSign.
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3144
     The result is normalized.
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3145
     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
  3146
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3147
    |result resultDigitByteArray lastDigit ok
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3148
     len     "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3149
     rsltLen "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3150
     index   "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3151
     borrow  "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3152
     diff    "{ Class: SmallInteger }" |
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3153
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3154
    "/ the following code only works with
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3155
    "/ 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
  3156
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3157
    ((aSmallInteger < (SmallInteger minVal + 255))
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3158
    or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3159
	^ self absMinus:(self class value:aSmallInteger) sign:newSign.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3160
    ].
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3161
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3162
    len := digitByteArray size.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3163
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3164
    rsltLen := len "+ 1".
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3165
    result := self class basicNew numberOfDigits:rsltLen sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3166
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3167
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3168
    borrow := aSmallInteger abs.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3169
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3170
%{
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3171
    if (__isByteArray(__INST(digitByteArray))
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3172
     && __isByteArray(resultDigitByteArray)) {
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3173
	unsigned INT __borrow = __intVal(borrow);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3174
	INT __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3175
	int __index = 1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3176
	int __len = __intVal(len);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3177
	unsigned char *__digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3178
	unsigned char *__resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3179
	int __len3;
4230
e1cfaecf453c alpha64 bug fixes
Claus Gittinger <cg@exept.de>
parents: 4229
diff changeset
  3180
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3181
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3182
# if (__POINTER_SIZE__ == 8)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3183
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3184
	 * subtract int-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3185
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3186
	__len3 = __len - 3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3187
	while (__index < __len3) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3188
	    /* do not make this into one expression - ask cg why */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3189
	    __diff = ((unsigned int *)(__digitP + __index-1))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3190
	    __diff -= (__borrow & 0xFFFFFFFFL);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3191
	    __borrow >>= 32;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3192
	    if (__diff < 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3193
		/* __diff += 0x100000000; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3194
		__borrow++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3195
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3196
	    ((unsigned int *)(__resultP+__index-1))[0] = __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3197
	    __index += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3198
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3199
# endif
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3200
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3201
	 * subtract short-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3202
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3203
	while (__index < __len) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3204
	    /* do not make this into one expression - ask cg why */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3205
	    __diff = ((unsigned short *)(__digitP+__index-1))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3206
	    __diff -= (__borrow & 0xFFFF);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3207
	    __borrow >>= 16;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3208
	    if (__diff < 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3209
		/* __diff += 0x10000; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3210
		__borrow++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3211
	    } else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3212
		if (__borrow == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3213
		    ((unsigned short *)(__resultP+__index-1))[0] = __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3214
		    __index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3215
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3216
		    /* nothing more to subtract .. */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3217
		    while (__index < __len) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3218
			((unsigned short *)(__resultP+__index-1))[0] = ((unsigned short *)(__digitP+__index-1))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3219
			__index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3220
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3221
		    if (__index <= __len) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3222
			__resultP[__index-1] = __digitP[__index-1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3223
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3224
		    break;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3225
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3226
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3227
	    ((unsigned short *)(__resultP+__index-1))[0] = __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3228
	    __index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3229
	}
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3230
#endif
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3231
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3232
	 * subtract byte-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3233
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3234
	while (__index <= __len) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3235
	    __diff = __digitP[__index-1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3236
	    __diff -= (__borrow & 0xFF);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3237
	    __borrow >>= 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3238
	    if (__diff < 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3239
		/* __diff += 0x100; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3240
		__borrow++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3241
	    } else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3242
		if (__borrow == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3243
		    __resultP[__index-1] = __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3244
		    __index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3245
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3246
		    /* nothing more to subtract .. */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3247
		    while (__index <= __len) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3248
			__resultP[__index-1] = __digitP[__index-1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3249
			__index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3250
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3251
		    break;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3252
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3253
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3254
	    __resultP[__index-1] = __diff;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3255
	    __index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3256
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3257
	lastDigit = __mkSmallInteger( __resultP[__index-1-1] );
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3258
	ok = true;
4181
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3259
    }
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3260
%}.
94ac48d3a3c8 tuned absFastMinus - buggy
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
  3261
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3262
    ok == true ifFalse:[        "/ cannot happen
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3263
	index := 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3264
	[borrow ~~ 0] whileTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3265
	    (index <= len) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3266
		diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3267
		borrow := borrow bitShift:-8.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3268
		diff < 0 ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3269
		    diff := diff + 256.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3270
		    borrow := borrow + 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3271
		]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3272
	    ] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3273
		diff := borrow bitAnd:255.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3274
		borrow := borrow bitShift:-8.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3275
	    ].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3276
	    resultDigitByteArray basicAt:index put:(lastDigit := diff).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3277
	    index := index + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3278
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3279
	[index <= len] whileTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3280
	    resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3281
	    index := index + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3282
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3283
	(index <= rsltLen) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3284
	    lastDigit := 0.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3285
	]
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3286
    ].
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3287
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3288
    (lastDigit == 0 or:[rsltLen <= SmallInteger maxBytes]) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3289
	^ result compressed.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3290
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3291
    ^ result
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3292
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3293
    "
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3294
     12345678900000000000 absFastMinus:1 sign:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3295
     12345678900000000000 absFastMinus:1 sign:-1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3296
     12345678900000000000 absFastMinus:1000000 sign:1
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3297
     12345678900000000000 absFastMinus:255 sign:1
4191
d6376725d99d tuned largeInt - smallInt
Claus Gittinger <cg@exept.de>
parents: 4190
diff changeset
  3298
     (SmallInteger maxVal + 1) absFastMinus:1 sign:1
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3299
     (SmallInteger minVal - 1) absFastMinus:1 sign:1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3300
    "
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  3301
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3302
    "Modified: 24.3.1997 / 21:33:25 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3303
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3304
4173
0e74ef3ec49d pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents: 4172
diff changeset
  3305
absFastPlus:aSmallInteger sign:newSign
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3306
    "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
  3307
     with sign: newSign.
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
  3308
     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
  3309
     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
  3310
     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
  3311
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  3312
    |result resultDigitByteArray lastDigit
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3313
     ok
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3314
     len     "{ Class: SmallInteger }"
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3315
     rsltLen "{ Class: SmallInteger }"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3316
     index "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3317
     carry "{ Class: SmallInteger }" |
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3318
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3319
    "/ the following code only works with
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3320
    "/ 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
  3321
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3322
    ((aSmallInteger < (SmallInteger minVal + 255))
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3323
    or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3324
	^ self absPlus:(self class value:aSmallInteger) sign:newSign.
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3325
    ].
3440
ac93772ac2ca oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents: 3438
diff changeset
  3326
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3327
    len := rsltLen := digitByteArray size.
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3328
    "/
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3329
    "/ 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
  3330
    "/ 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
  3331
    "/
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3332
    (digitByteArray at:len) == 16rFF ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3333
	rsltLen := len + 1.
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3334
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3335
	"/ 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
  3336
%{
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3337
#if __POINTER_SIZE__ == 8
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3338
	if (__intVal(aSmallInteger) & 0xFF00000000000000L) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3339
	    rsltLen = __mkSmallInteger(__intVal(len) + 1);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3340
	}
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3341
#else
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3342
	if (__intVal(aSmallInteger) & 0xFF000000) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3343
	    rsltLen = __mkSmallInteger(__intVal(len) + 1);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3344
	}
4950
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3345
#endif
c16f86f9f098 oops - bug when adding a largeInt
Claus Gittinger <cg@exept.de>
parents: 4948
diff changeset
  3346
%}
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3347
    ].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3348
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3349
    result := self class basicNew numberOfDigits:rsltLen sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3350
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3351
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3352
%{
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3353
    if (__isByteArray(__INST(digitByteArray))
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3354
     && __isByteArray(resultDigitByteArray)
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3355
     && __isSmallInteger(aSmallInteger)) {
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3356
	/* carry is NOT unsigned (see negation below) */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3357
	INT __carry = __intVal(aSmallInteger);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3358
	int __index = 1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3359
	int __len = __intVal(len);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3360
	unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3361
	unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3362
	INT __ptrDelta = __dst - __src;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3363
	unsigned char *__srcLast = __src + __len - 1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3364
	int __rsltLen = __intVal(rsltLen);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3365
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3366
	if (__carry < 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3367
	    __carry = -__carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3368
	}
4171
afd926573419 tuned largeInt + smallInt
Claus Gittinger <cg@exept.de>
parents: 4170
diff changeset
  3369
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3370
#if defined(__LSBFIRST__)
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  3371
# if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4)
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3372
#  if 0 /* NOTICE - the code below is 20% slower ... - why */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3373
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3374
	 * add long-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3375
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3376
	asm("  jecxz nothingToDo                                      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3377
	       movl  %%eax, %%esi      /* __src input */              \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3378
	       movl  %%ebx, %%edi      /* __dst input */              \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3379
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3380
	       /* the first 4-byte int */                             \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3381
	       lodsl                   /* fetch */                    \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3382
	       addl  %%edx, %%eax      /* add */                      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3383
	       stosl                   /* store */                    \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3384
	       leal  -1(%%ecx),%%ecx   /* do not clobber carry */     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3385
	       jecxz doneLoop          /* any more ? */               \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3386
	       /* remaining 4-byte ints */                            \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3387
	       jmp   addLoop                                          \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3388
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3389
	       .align 8                                               \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3390
	     addLoop:                                                 \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3391
	       movl  0(%%esi), %%ebx   /* fetch  */                   \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3392
	       jnc   copyLoop2                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3393
	       movl  $0, %%eax                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3394
	       leal  4(%%esi), %%esi                                  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3395
	       adcl  %%ebx, %%eax      /* & add carry from prev int */\n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3396
	       leal  8(%%edi), %%edi                                  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3397
	       movl  %%eax, -8(%%edi)  /* store */                    \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3398
	       leal  -1(%%ecx),%%ecx   /* do not clobber carry */     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3399
	       jecxz doneLoop          /* any more ? */               \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3400
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3401
	       movl  0(%%esi), %%ebx   /* fetch  */                   \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3402
	       movl  $0, %%eax                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3403
	       leal  4(%%esi), %%esi                                  \
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3404
	       adcl  %%ebx, %%eax      /* & add carry from prev int */\n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3405
	       movl  %%eax, -4(%%edi)  /* store */                    \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3406
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3407
	       loop  addLoop                                          \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3408
	       jmp   doneLoop                                         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3409
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3410
	       .align 8                                               \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3411
	     copyLoop:                                                \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3412
	       movl  0(%%esi), %%ebx                                  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3413
	     copyLoop2:                                               \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3414
	       add   $4, %%esi                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3415
	       add   $4, %%edi                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3416
	       movl  %%ebx, -4(%%edi)                                 \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3417
	       loop  copyLoop                                         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3418
								      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3419
	     doneLoop:                                                \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3420
	       movl  $0, %%edx         /* do not clobber carry (xorl clears it) */   \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3421
	       adcl  $0, %%edx                                        \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3422
	       movl  %%esi, %%eax      /* __src output */             \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3423
	     nothingToDo:                                             \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3424
	    " : "=d"  ((unsigned long)(__carry)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3425
		"=a"  (__src)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3426
	      : "1"   (__src),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3427
		"b"   (__dst),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3428
		"c"   (__len / 4),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3429
		"0"   (__carry)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3430
	      : "esi", "edi");
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3431
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3432
#  else
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3433
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3434
	    unsigned char *__srcLastX;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3435
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3436
	    __srcLastX = __srcLast - 3 - 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3437
	    while (__src <= __srcLastX) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3438
		unsigned int __sum, __sum2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3439
		unsigned __digit1, __digit2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3440
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3441
		__digit1 = ((unsigned *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3442
		__digit2 = ((unsigned *)__src)[1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3443
		asm ("addl %%edx,%%ecx          \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3444
		      adcl $0, %%eax            \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3445
		      movl $0, %%edx            \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3446
		      adcl $0, %%edx"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3447
			: "=d"  ((unsigned long)(__carry)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3448
			  "=c"  ((unsigned long)(__sum)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3449
			  "=a"  ((unsigned long)(__sum2))
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3450
			: "0"   ((unsigned long)(__carry)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3451
			  "1"   (__digit1),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3452
			  "2"   (__digit2));
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3453
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3454
		((unsigned int *)(__src + __ptrDelta))[0] = __sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3455
		((unsigned int *)(__src + __ptrDelta))[1] = __sum2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3456
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3457
		__src += 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3458
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3459
		if (__carry == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3460
		    while (__src <= __srcLastX) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3461
			/* copy over words */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3462
			((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3463
			((unsigned int *)(__src + __ptrDelta))[1] = ((unsigned int *)__src)[1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3464
			__src += 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3465
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3466
		    while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3467
			/* copy over bytes */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3468
			__src[__ptrDelta] = __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3469
			__src ++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3470
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3471
		    goto doneSource;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3472
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3473
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3474
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3475
	    __srcLastX = __srcLastX + 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3476
	    if (__src <= __srcLastX) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3477
		unsigned int __sum, __digit;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3478
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3479
		__digit = ((unsigned *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3480
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3481
		asm ("addl %%eax,%%edx  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3482
		      movl $0,%%eax     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3483
		      adcl $0,%%eax"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3484
			: "=a"  ((unsigned long)(__carry)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3485
			  "=d"  ((unsigned long)(__sum))
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3486
			: "0"   ((unsigned long)(__carry)),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3487
			  "1"   (__digit) );
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3488
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3489
		((unsigned int *)(__src + __ptrDelta))[0] = __sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3490
		__src += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3491
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3492
		if (__carry == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3493
		    while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3494
			/* copy over bytes */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3495
			__src[__ptrDelta] = __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3496
			__src ++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3497
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3498
		    goto doneSource;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3499
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3500
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3501
	}
4281
2acfd4c8b638 large + small
Claus Gittinger <cg@exept.de>
parents: 4276
diff changeset
  3502
#  endif
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3503
# else /* not i386-GNUC */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  3504
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3505
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3506
	    unsigned char *__srcLast4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3507
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3508
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3509
	     * add long-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3510
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3511
	    __srcLast4 = __srcLast - 3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3512
	    while (__src <= __srcLast4) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3513
		unsigned int __sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3514
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3515
		__sum = ((unsigned int *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3516
		asm {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3517
		      mov eax, __sum
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3518
		      add eax, __carry
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3519
		      mov edx, 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3520
		      adc edx, 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3521
		      mov __sum, eax
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3522
		      mov __carry, edx
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3523
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3524
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3525
		((unsigned int *)(__src + __ptrDelta))[0] = __sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3526
		__src += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3527
		if (__carry == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3528
		    while (__src <= __srcLast4) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3529
			/* copy over words */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3530
			((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3531
			__src += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3532
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3533
		    while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3534
			/* copy over bytes */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3535
			__src[__ptrDelta] = __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3536
			__src ++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3537
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3538
		    goto doneSource;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3539
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3540
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3541
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3542
#  else /* not i386-WIN32 */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3543
#   if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3544
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3545
	    unsigned char *__srcLast4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3546
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3547
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3548
	     * add long-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3549
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3550
	    __srcLast4 = __srcLast - 3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3551
	    while (__src <= __srcLast4) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3552
		unsigned INT __sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3553
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3554
		__sum = ((unsigned int *)__src)[0] + __carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3555
		((unsigned int *)(__src + __ptrDelta))[0] = __sum /* & 0xFFFF */;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3556
		__src += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3557
		__carry = __sum >> 32;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3558
		if (__carry == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3559
		    while (__src <= __srcLast4) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3560
			/* copy over words */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3561
			((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3562
			__src += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3563
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3564
		    while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3565
			/* copy over bytes */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3566
			__src[__ptrDelta] = __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3567
			__src ++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3568
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3569
		    goto doneSource;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3570
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3571
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3572
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3573
#   endif /* LSB+64bit */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3574
#  endif /* __i386__ & WIN32 */
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3575
# endif /* __i386__ & GNUC */
4178
e7121285caa8 inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents: 4175
diff changeset
  3576
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3577
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3578
	 * add short-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3579
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3580
	while (__src < __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3581
	    __carry += ((unsigned short *)__src)[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3582
	    ((unsigned short *)(__src + __ptrDelta))[0] = __carry /* & 0xFFFF */;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3583
	    __carry >>= 16;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3584
	    __src += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3585
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3586
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3587
	 * last (odd) byte
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3588
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3589
	if (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3590
	    __carry += __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3591
	    __src[__ptrDelta] = __carry /* & 0xFF */;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3592
	    __carry >>= 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3593
	    __src++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3594
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3595
#else /* not __LSBFIRST__ */
4292
1a12f88acb8f sparc tuning (large + small)
Claus Gittinger <cg@exept.de>
parents: 4291
diff changeset
  3596
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3597
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3598
	 * add byte-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3599
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3600
	while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3601
	    __carry += __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3602
	    __src[__ptrDelta] = __carry /* & 0xFF */;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3603
	    __src++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3604
	    __carry >>= 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3605
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3606
	    if (__carry == 0) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3607
		while (__src <= __srcLast) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3608
		    /* copy over rest */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3609
		    __src[__ptrDelta] = __src[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3610
		    __src++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3611
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3612
		goto doneSource;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3613
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3614
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3615
#endif /* __LSBFIRST__ */
4185
375439993eb7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4182
diff changeset
  3616
4255
73212f63b6a6 slight speedup in large + small
Claus Gittinger <cg@exept.de>
parents: 4254
diff changeset
  3617
    doneSource: ;
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3618
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3619
	 * now, at most one other byte is to be stored ...
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3620
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3621
	if (__len < __rsltLen) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3622
	    __src[__ptrDelta] = __carry /* & 0xFF */;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3623
	    __src++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3624
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3625
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3626
	if (__src[__ptrDelta-1]) {      /* lastDigit */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3627
	    RETURN (result);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3628
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3629
	ok = true;
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3630
    }
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3631
%}.
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3632
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3633
    ok ~~ true ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3634
	index := 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3635
	carry := aSmallInteger abs.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3636
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3637
	[carry ~~ 0] whileTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3638
	    (index <= len) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3639
		carry := (digitByteArray basicAt:index) + carry.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3640
	    ].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3641
	    resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3642
	    carry := carry bitShift:-8.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3643
	    index := index + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3644
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3645
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3646
	(index <= rsltLen) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3647
	    [index <= len] whileTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3648
		resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3649
		index := index + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3650
	    ].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3651
	    lastDigit := 0.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3652
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3653
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3654
	(lastDigit ~~ 0 and:[rsltLen > SmallInteger maxBytes]) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3655
	    ^ result
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  3656
	].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3657
    ].
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3658
2952
2cf2ae894c4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
  3659
    ^ result compressed
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  3660
2495
0e6c781fd370 care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
  3661
    "Modified: 24.3.1997 / 21:32:41 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3662
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3663
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3664
absLess:aLargeInteger
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3665
    "return true, if abs(self) < abs(theArgument).
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3666
     This handles unnormalized largeIntegers."
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3667
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3668
    |myLen "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3669
     otherLen "{ Class: SmallInteger }"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3670
     d1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3671
     d2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3672
     otherDigitByteArray |
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3673
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3674
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3675
#if defined(__LSBFIRST__)
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3676
    if (__isByteArray(__INST(digitByteArray))
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3677
     && __isLargeInteger(aLargeInteger)) {
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3678
        OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3679
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3680
        if (__isByteArray(_otherDigitByteArray)) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3681
            unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3682
            unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3683
            INT _myLen = __byteArraySize(__INST(digitByteArray));
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3684
            INT _otherLen = __byteArraySize(_otherDigitByteArray);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3685
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3686
            if (_myLen == __POINTER_SIZE__) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3687
                if (_otherLen == __POINTER_SIZE__) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3688
                    unsigned INT _myVal = *((unsigned INT *)_myDigits);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3689
                    unsigned INT _otherVal = *((unsigned INT *)_otherDigits);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3690
                    RETURN( (_myVal < _otherVal) ? true : false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3691
                }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3692
            }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3693
# if defined(UINT64) && (__POINTER_SIZE__ != 8)
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3694
            if (_myLen == __POINTER_SIZE__) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3695
                if (_otherLen <= 8) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3696
                    UINT64 _myVal = (UINT64)(*((UINT *)_myDigits));
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3697
                    UINT64 _otherVal = *((UINT64 *)_otherDigits);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3698
                    RETURN( (_myVal < _otherVal) ? true : false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3699
                }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3700
            } else {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3701
                if (_myLen <= 8) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3702
                    if (_otherLen <= 8) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3703
                        UINT64 _myVal = (*((UINT64 *)_myDigits));
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3704
                        UINT64 _otherVal = *((UINT64 *)_otherDigits);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3705
                        RETURN( (_myVal < _otherVal) ? true : false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3706
                    }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3707
                    if (_otherLen == __POINTER_SIZE__) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3708
                        UINT64 _myVal = (*((UINT64 *)_myDigits));
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3709
                        UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits);
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3710
                        RETURN( (_myVal < _otherVal) ? true : false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3711
                    }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3712
                }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3713
            }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3714
# endif /* UINT64 */
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3715
            while ((_myLen > 0) && (_myDigits[_myLen-1] == 0)) _myLen--;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3716
            while ((_otherLen > 0) && (_otherDigits[_otherLen-1] == 0)) _otherLen--;
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3717
            if (_myLen < _otherLen) { RETURN( true ); }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3718
            if (_myLen > _otherLen) { RETURN (false ); }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3719
            while (_myLen-- > 0) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3720
                unsigned char _d1 = _myDigits[_myLen];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3721
                unsigned char _d2 = _otherDigits[_myLen];
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3722
                
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3723
                if (_d1 != _d2) {
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3724
                    if (_d1 < _d2) { RETURN( true ); }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3725
                    RETURN (false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3726
                }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3727
            }
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3728
            RETURN (false );
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3729
        }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3730
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3731
#endif /* LSBFIRST */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3732
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3733
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3734
    myLen := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3735
    otherDigitByteArray := aLargeInteger digitBytes.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3736
    otherLen := otherDigitByteArray size.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3737
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3738
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3739
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3740
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3741
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3742
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3743
    [myLen > 0 and:[(digitByteArray basicAt:myLen) == 0]] whileTrue:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3744
        myLen := myLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3745
    ].
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3746
    [otherLen > 0 and:[(otherDigitByteArray basicAt:otherLen) == 0]] whileTrue:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3747
        otherLen := otherLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3748
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3749
    (myLen < otherLen) ifTrue:[^ true].
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3750
    (myLen > otherLen) ifTrue:[^ false].
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3751
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3752
    [myLen > 0] whileTrue:[
19132
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3753
        d1 := digitByteArray basicAt:myLen.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3754
        d2 := otherDigitByteArray basicAt:myLen.
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3755
        d1 == d2 ifFalse:[
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3756
            (d1 < d2) ifTrue:[^ true].
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3757
            ^ false
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3758
        ].
0c0046873268 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19126
diff changeset
  3759
        myLen := myLen - 1
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3760
    ].
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3761
    ^ false
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  3762
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3763
    "
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3764
     |a b|
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3765
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3766
     a := 16rFEFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3767
     b := 16rFFFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3768
     Time millisecondsToRun:[
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3769
       10000000 timesRepeat:[ a absLess_old:b ]
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3770
     ]    1185 1233 1185
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3771
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3772
     |a b|
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3773
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3774
     a := 16rFEFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3775
     b := 16rFFFFFFFF.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3776
     Time millisecondsToRun:[
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3777
       10000000 timesRepeat:[ a absLess_n:b ]
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3778
     ] 686 655 702 702
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3779
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3780
     16rEFFFFFFF  absLess_n: 16rFFFFFFFF
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3781
     16rEFFFFFFF  absLess: 16rFFFFFFFF
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3782
    "
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3783
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3784
    "Modified: / 3.5.1999 / 08:06:28 / stefan"
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3785
    "Modified: / 8.5.1999 / 18:37:11 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3786
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3787
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3788
absLessEq:aLargeInteger
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3789
    "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
  3790
     This handles unnormalized largeIntegers."
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3791
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3792
    |myLen "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3793
     otherLen "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3794
     d1   "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3795
     d2   "{ Class: SmallInteger }"
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3796
     otherDigitByteArray |
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3797
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3798
%{  /* NOCONTEXT */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3799
#if defined(__LSBFIRST__)
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3800
    if (__isByteArray(__INST(digitByteArray))
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3801
     && __isLargeInteger(aLargeInteger)) {
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3802
	OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3803
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3804
	if (__isByteArray(_otherDigitByteArray)) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3805
	    unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3806
	    unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element;
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3807
	    INT _myLen = __byteArraySize(__INST(digitByteArray));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3808
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3809
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3810
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3811
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3812
		if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3813
		    unsigned INT _myVal = *((unsigned INT *)_myDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3814
		    unsigned INT _otherVal = *((unsigned INT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3815
		    RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3816
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3817
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3818
# if defined(UINT64) && (__POINTER_SIZE__ != 8)
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3819
	    if (_myLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3820
		INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3821
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3822
		if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3823
		    UINT64 _myVal = (UINT64)(*((UINT *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3824
		    UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3825
		    RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3826
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3827
	    } else {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3828
		if (_myLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3829
		    INT _otherLen = __byteArraySize(_otherDigitByteArray);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3830
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3831
		    if (_otherLen <= 8) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3832
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3833
			UINT64 _otherVal = *((UINT64 *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3834
			RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3835
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3836
		    if (_otherLen == __POINTER_SIZE__) {
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3837
			UINT64 _myVal = (*((UINT64 *)_myDigits));
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3838
			UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits);
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3839
			RETURN( (_myVal <= _otherVal) ? true : false );
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3840
		    }
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3841
		}
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3842
	    }
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3843
# endif /* UINT64 */
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3844
	}
11604
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3845
    }
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3846
#endif /* LSBFIRST */
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3847
%}.
64df3632e526 tuned absEq, absLess and absLessEq
Claus Gittinger <cg@exept.de>
parents: 11564
diff changeset
  3848
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3849
    myLen := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3850
    otherDigitByteArray := aLargeInteger digitBytes.
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3851
    otherLen := otherDigitByteArray size.
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3852
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3853
    "/ the highest digit(s) should not be zero
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3854
    "/ when properly normalized;
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3855
    "/ but we are tolerant here, to allow for unnormalized
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3856
    "/ numbers to be compared ...
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3857
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3858
    [(digitByteArray basicAt:myLen) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3859
	myLen := myLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3860
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3861
    [(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3862
	otherLen := otherLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3863
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3864
    (myLen < otherLen) ifTrue:[^ true].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3865
    (myLen > otherLen) ifTrue:[^ false].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3866
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3867
    [myLen > 0] whileTrue:[
11927
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3868
	d1 := digitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3869
	d2 := otherDigitByteArray basicAt:myLen.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3870
	d1 == d2 ifFalse:[
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3871
	    (d1 < d2) ifTrue:[^ true].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3872
	    ^ false.
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3873
	].
75c9deec4a70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11731
diff changeset
  3874
	myLen := myLen - 1
3984
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3875
    ].
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3876
    ^ true
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3877
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3878
    "Created: / 13.2.1998 / 12:19:45 / stefan"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  3879
    "Modified: / 30.4.1999 / 12:46:31 / stefan"
4157
f9a673441ba5 comments
Claus Gittinger <cg@exept.de>
parents: 4151
diff changeset
  3880
    "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
  3881
!
45a76e2f4236 Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents: 3910
diff changeset
  3882
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3883
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
  3884
    "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
  3885
     with sign: newSign.
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  3886
     The result is normalized. The argument must be a largeInteger
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3887
     with a byteSize smaller than the receiver's byteSize.
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  3888
     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
  3889
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3890
    |result done
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3891
     otherDigitByteArray resultDigitByteArray
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3892
     len1   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3893
     len2   "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3894
     index  "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3895
     borrow "{ Class: SmallInteger }"
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3896
     diff   "{ Class: SmallInteger }"
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3897
     carry  "{ Class: SmallInteger }"
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  3898
     lastDigit
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3899
     lResult ok|
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3900
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3901
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3902
    otherDigitByteArray := aLargeInteger digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3903
    len2 := otherDigitByteArray size.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3904
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3905
    len1 > len2 ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3906
        lResult := len1
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3907
    ] ifFalse:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3908
        lResult := (len1 max: len2) + 1.
4251
6f215cab1ea9 oops - absMinus error.
Claus Gittinger <cg@exept.de>
parents: 4231
diff changeset
  3909
    ].
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  3910
    result := self class basicNew numberOfDigits:lResult sign:newSign.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  3911
    resultDigitByteArray := result digitBytes.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  3912
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3913
    lastDigit := 0.
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  3914
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3915
%{
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3916
    OBJ _digitByteArray = __INST(digitByteArray);
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3917
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3918
    if (__isByteArray(_digitByteArray)
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3919
     && __isByteArray(otherDigitByteArray)
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  3920
     && __isByteArray(resultDigitByteArray)) {
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3921
        int __len1 = __intVal(len1);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3922
        int __len2 = __intVal(len2);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3923
        int __minLen = __len1 < __len2 ? __len1 : __len2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3924
        int __index, __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3925
        INT __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3926
        unsigned char *__myDigits, *__otherDigits, *__resultDigits;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3927
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3928
        ok = true;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3929
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3930
        __resultDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3931
        __otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3932
        __myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3933
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3934
        __index = 1;
4942
de4ae3110e9b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4920
diff changeset
  3935
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3936
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3937
# if __POINTER_SIZE__ == 8
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3938
        /*
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3939
         * subtract int-wise
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3940
         */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3941
        while ((__index+3) <= __minLen) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3942
            /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3943
            __diff = ((unsigned int *)(__myDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3944
            __diff -= ((unsigned int *)(__otherDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3945
            __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3946
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3947
            if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3948
                __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3949
            } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3950
                __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3951
                /* __diff += 0x10000; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3952
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3953
            ((unsigned int *)(__resultDigits+__index-1))[0] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3954
            __index += 4;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3955
        }
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  3956
# endif /* 64bit */
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  3957
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3958
        /*
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3959
         * subtract short-wise
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3960
         */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3961
        while (__index < __minLen) {   /* i.e. index+1 <= minLen */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3962
            /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3963
            __diff = ((unsigned short *)(__myDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3964
            __diff -= ((unsigned short *)(__otherDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3965
            __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3966
            if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3967
                __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3968
            } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3969
                __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3970
                /* __diff += 0x10000; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3971
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3972
            ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3973
            __index += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3974
        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3975
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3976
        if (__index == __minLen) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3977
            /* one of the operands has odd length - cannot continue short-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3978
        } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3979
            if (__len1 > __len2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3980
                while (__index < __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3981
                    /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3982
                    __diff = ((unsigned short *)(__myDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3983
                    __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3984
                    if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3985
                        __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3986
                        ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3987
                        __index += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3988
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3989
                        /* copy over rest */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3990
                        while (__index < __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3991
                            ((unsigned short *)(__resultDigits+__index-1))[0] = ((unsigned short *)(__myDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3992
                            __index+=2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3993
                        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3994
                        if (__index <= __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3995
                            __resultDigits[__index-1] = __myDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3996
                            __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3997
                        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3998
                        break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  3999
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4000
                    __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4001
                    /* __diff += 0x10000; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4002
                    ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4003
                    __index += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4004
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4005
            } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4006
                if (__len2 > __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4007
                    while (__index < __len2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4008
                        /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4009
                        __diff = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4010
                        __diff -= ((unsigned short *)(__otherDigits+__index-1))[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4011
                        __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4012
                        if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4013
                            __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4014
                        } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4015
                            __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4016
                            /* __diff += 0x10000; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4017
                        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4018
                        ((unsigned short *)(__resultDigits+__index-1))[0] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4019
                        __index += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4020
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4021
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4022
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4023
        }
4214
ba20de3793e2 slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents: 4213
diff changeset
  4024
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4025
        /*
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4026
         * subtract byte-wise
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4027
         */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4028
        while (__index <= __minLen) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4029
            /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4030
            __diff = __myDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4031
            __diff -= __otherDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4032
            __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4033
            if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4034
                __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4035
            } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4036
                __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4037
                /* __diff += 0x100; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4038
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4039
            __resultDigits[__index-1] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4040
            __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4041
        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4042
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4043
        if (__len1 > __len2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4044
            while (__index <= __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4045
                /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4046
                __diff = __myDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4047
                __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4048
                if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4049
                    __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4050
                    /* copy over rest */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4051
                    __resultDigits[__index-1] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4052
                    __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4053
                    while (__index <= __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4054
                        __resultDigits[__index-1] = __myDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4055
                        __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4056
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4057
                    break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4058
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4059
                __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4060
                /* __diff += 0x100; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4061
                __resultDigits[__index-1] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4062
                __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4063
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4064
        } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4065
            if (__len2 > __len1) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4066
                while (__index <= __len2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4067
                    /* do not make this into one expression - ask cg why */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4068
                    __diff = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4069
                    __diff -= __otherDigits[__index-1];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4070
                    __diff -= __borrow;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4071
                    if (__diff >= 0) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4072
                        __borrow = 0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4073
                    } else {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4074
                        __borrow = 1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4075
                        /* __diff += 0x100; */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4076
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4077
                    __resultDigits[__index-1] = __diff;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4078
                    __index++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4079
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4080
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4081
        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4082
        borrow = __mkSmallInteger(__borrow);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4083
        index = __mkSmallInteger(__index);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4084
        lastDigit = __mkSmallInteger(__resultDigits[__intVal(lResult)-1]);
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4085
    }
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4086
%}.
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4087
    ok == true ifFalse:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4088
        index := 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4089
        borrow := 0.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4090
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4091
        done := false.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4092
        [done] whileFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4093
            diff := borrow.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4094
            (index <= len1) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4095
                diff := diff + (digitByteArray basicAt:index).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4096
                (index <= len2) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4097
                    diff := diff - (otherDigitByteArray basicAt:index)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4098
                ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4099
            ] ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4100
                (index <= len2) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4101
                    diff := diff - (otherDigitByteArray basicAt:index)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4102
                ] ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4103
                    "end reached"
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4104
                    done := true
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4105
                ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4106
            ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4107
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4108
            "/ workaround for
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4109
            "/ gcc code generator bug
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4110
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4111
            (diff >= 0) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4112
                borrow := 0
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4113
            ] ifFalse:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4114
                borrow := -1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4115
                diff := diff + 16r100
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4116
            ].
4190
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4117
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4118
    "/        (diff < 0) ifTrue:[
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4119
    "/            borrow := -1.
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4120
    "/            diff := diff + 16r100
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4121
    "/        ] ifFalse:[
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4122
    "/            borrow := 0
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4123
    "/        ].
9fdc42eea567 tuned largeInt - largeInt
Claus Gittinger <cg@exept.de>
parents: 4189
diff changeset
  4124
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4125
            resultDigitByteArray basicAt:index put:diff.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4126
            index := index + 1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4127
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4128
        lastDigit := resultDigitByteArray basicAt:lResult.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4129
    ].
1408
e9db184b34ee oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  4130
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4131
    (borrow ~~ 0) ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4132
        "/ must generate 255's complement
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4133
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4134
        result sign:newSign negated.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4135
        [index <= lResult] whileTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4136
            resultDigitByteArray basicAt:index put:16rFF.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4137
            index := index + 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4138
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4139
        index := lResult.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4140
        [index > 0] whileTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4141
            resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4142
            index := index - 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4143
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4144
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4145
        index := 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4146
        carry := 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4147
        [carry ~~ 0] whileTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4148
            (index <= lResult) ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4149
                carry := (resultDigitByteArray basicAt:index) + carry.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4150
            ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4151
            resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4152
            carry := carry bitShift:-8.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4153
            index := index + 1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4154
        ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4155
        lastDigit := resultDigitByteArray basicAt:lResult.
4213
a66f711d86df slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents: 4211
diff changeset
  4156
    ].
15242
4539185c87ed class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15240
diff changeset
  4157
    (lastDigit == 0 or:[lResult <= SmallInteger maxBytes]) ifTrue:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4158
        ^ result compressed.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4159
    ].
15242
4539185c87ed class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15240
diff changeset
  4160
    ^ result
1408
e9db184b34ee oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
  4161
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4162
    "Modified: 5.11.1996 / 14:09:25 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4163
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  4164
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4165
absMod:anInteger
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4166
    "return a LargeIntegers representing
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4167
     abs(self) \\ abs(theArgument).
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4168
     Used as a helper for \\ and rem:"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4169
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4170
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4171
    |dividend divisor
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4172
     shift "{ Class: SmallInteger }" |
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4173
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4174
    anInteger == 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4175
        ^ ZeroDivide raiseRequestWith:thisContext
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4176
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4177
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4178
    self = anInteger ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4179
        ^ 0
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4180
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4181
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4182
    shift := self highBit - anInteger highBit.
16124
23f576b31095 class: LargeInteger
Stefan Vogel <sv@exept.de>
parents: 15909
diff changeset
  4183
    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
  4184
    shift < 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4185
        ^ dividend compressed.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4186
    ].
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  4187
    shift == 0 ifTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4188
        divisor := LargeInteger digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy
4139
2942f4bd2862 Fix absMod: and absDivMod:
Stefan Vogel <sv@exept.de>
parents: 4137
diff changeset
  4189
    ] ifFalse:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4190
        divisor := anInteger bitShift:shift.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4191
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4192
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4193
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4194
    shift := shift + 1.
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4195
    [shift > 0] whileTrue:[
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4196
        (dividend absLess:divisor) ifFalse:[
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4197
            (dividend absSubtract: divisor) ifFalse:[ "result == 0"
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4198
                ^ 0
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4199
            ].
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4200
        ].
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4201
        shift := shift - 1.
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4202
        divisor div2.
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4203
    ].
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4204
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4205
    ^ dividend compressed
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4206
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4207
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4208
Time millisecondsToRun:[   10000 timesRepeat:[  16000000001 absMod:4000000001] ]
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4209
Time millisecondsToRun:[   10000 timesRepeat:[  16000000001 absDivMod:4000000001] ]
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4210
     16000000000 absMod:4000000000
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4211
     16000000000 absMod:3000000000
19126
98adb7dc1950 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19102
diff changeset
  4212
     16000000000000000000 absMod:16000000000000000000
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4213
    "
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4214
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4215
    "Modified: / 5.11.1996 / 18:40:24 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4216
    "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
  4217
    "Modified: / 26.7.1999 / 10:46:18 / stefan"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4218
!
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  4219
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4220
absMul:aLargeInteger
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4221
    "return a LargeInteger representing abs(self) * abs(theArgument)"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4222
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4223
    |result otherDigitByteArray resultDigitByteArray ok
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4224
     idx      "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4225
     carry    "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4226
     len1     "{ Class: SmallInteger }"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4227
     len2     "{ Class: SmallInteger }"
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4228
     lenRslt  "{ Class: SmallInteger }"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4229
     dstIndex "{ Class: SmallInteger }"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  4230
     prod     "{ Class: SmallInteger }"
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  4231
     v        "{ Class: SmallInteger }"|
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4232
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4233
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4234
    otherDigitByteArray := aLargeInteger digitBytes.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4235
    len2 := otherDigitByteArray size.
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4236
    lenRslt := len1 + len2.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4237
    UseKarazuba ~~ false ifTrue:[ 
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4238
        lenRslt > 400 ifTrue:[ ^ self absMulKarazuba:aLargeInteger ].
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4239
    ].    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4240
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4241
    result := self class basicNew numberOfDigits:lenRslt.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4242
    resultDigitByteArray := result digitBytes.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4243
    ok := false.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4244
%{
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 1065
diff changeset
  4245
    if (__isByteArray(__INST(digitByteArray))
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4246
     && __isByteArray(otherDigitByteArray)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4247
     && __isByteArray(resultDigitByteArray)
250
a5deb61ffdac *** empty log message ***
claus
parents: 175
diff changeset
  4248
     && __bothSmallInteger(len1, len2)) {
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4249
        unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4250
        unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4251
        unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4252
        unsigned char *_p1, *_pResult0, *_p1Last, *_p2Last, *_pResultLast;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4253
        unsigned char *_pResultLastShort, *_pResultLastInt;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4254
        unsigned INT _v;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4255
        int _len1 = __intVal(len1);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4256
        int _len2 = __intVal(len2);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4257
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4258
        _p1Last = myBytes    + _len1 - 1;  /* the last byte */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4259
        _p2Last = otherBytes + _len2 - 1;  /* the last byte */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4260
        _pResult0 = resultBytes;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4261
        _pResultLast = resultBytes + _len1 + _len2 - 1; /* the last byte */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4262
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4263
        /*
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4264
         *        aaa...aaa      f1[0] * f2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4265
         *       bbb...bbb       f1[1] * f2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4266
         *      ccc...ccc        f1[2] * f2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4267
         *     ...
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4268
         *    xxx...xxx          f1[high] * f2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4269
         *
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4270
         * start short-wise
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4271
         * bounds: (16rFFFF * 16rFFFF) + 16rFFFF -> FFFF0000
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4272
         */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4273
        _p1 = myBytes;
4231
5e9e9319a9de alpha large*large speedup
Claus Gittinger <cg@exept.de>
parents: 4230
diff changeset
  4274
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4275
#if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4276
        _pResultLastInt = _pResultLast - 3;   /* the last int */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4277
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4278
        /* loop over ints of f1 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4279
        for (; _p1 <= (_p1Last-3); _p1 += 4, _pResult0 += 4) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4280
            unsigned char *_pResult, *_p2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4281
            unsigned INT word1 = ((unsigned int *)_p1)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4282
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4283
            _pResult = _pResult0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4284
            _p2 = otherBytes;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4285
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4286
            /* loop over ints of f2 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4287
            while (_p2 <= (_p2Last-3)) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4288
                _v = (word1 * (unsigned INT)(((unsigned int *)_p2)[0])) + ((unsigned INT)((unsigned int *)_pResult)[0]);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4289
                ((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4290
                _v >>= 32; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4291
                _pResult += 4;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4292
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4293
                    unsigned char *_pResult1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4294
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4295
                    /* distribute carry - int-wise, then byte-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4296
                    for (_pResult1 = _pResult; _v; _pResult1 += 4) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4297
                        if (_pResult1 > _pResultLastInt) break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4298
                        _v += (unsigned INT)(((unsigned int *)_pResult1)[0]);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4299
                        ((unsigned int *)_pResult1)[0] = _v /* & 0xFFFFFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4300
                        _v >>= 32;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4301
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4302
                    for (; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4303
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4304
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4305
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4306
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4307
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4308
                _p2 += 4;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4309
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4310
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4311
            /* possible odd highByte of f2 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4312
            while (_p2 <= _p2Last) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4313
                _v = (word1 * (unsigned INT)((unsigned char *)_p2[0])) + (unsigned INT)((unsigned char *)_pResult[0]);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4314
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4315
                ((unsigned char *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4316
                _v >>= 8; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4317
                _pResult++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4318
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4319
                    unsigned char *_pResult1 = _pResult;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4320
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4321
                    /* distribute carry byte-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4322
                    for (; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4323
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4324
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4325
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4326
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4327
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4328
                _p2++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4329
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4330
        }
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4331
#endif /* 64bit */
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4332
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4333
        /* possible odd high short of f1 (or shortLoop, if not 64bit) */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4334
        _pResultLastShort = _pResultLast - 1; /* the last short */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4335
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4336
        for (; _p1 < _p1Last; _p1 += 2, _pResult0 += 2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4337
            unsigned char *_pResult, *_p2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4338
            unsigned int short1 = ((unsigned short *)_p1)[0];
4231
5e9e9319a9de alpha large*large speedup
Claus Gittinger <cg@exept.de>
parents: 4230
diff changeset
  4339
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4340
#if !defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4341
            short1 = ((short1 >> 8) & 0xFF) | ((short1 & 0xFF) << 8);
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4342
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4343
            _pResult = _pResult0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4344
            _p2 = otherBytes;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4345
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4346
            /* loop over shorts of f2 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4347
            while (_p2 < _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4348
#if !defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4349
                unsigned int _short2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4350
                unsigned int _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4351
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4352
                _short2 = ((unsigned short *)_p2)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4353
                _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4354
                _short3 = ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4355
                _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4356
                _v = (short1 * _short2) + _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4357
                _pResult[0] = _v;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4358
                _pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4359
#else /* __LSBFIRST__ */
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4360
                _v = (short1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4361
                ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4362
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4363
                _v >>= 16; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4364
                _pResult += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4365
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4366
                    unsigned char *_pResult1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4367
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4368
                    /* distribute carry - short-wise, then byte-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4369
                    _pResult1 = _pResult;
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  4370
#if defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4371
                    for (; _v; _pResult1 += 2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4372
                        if (_pResult1 > _pResultLastShort) break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4373
                        _v += ((unsigned short *)_pResult1)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4374
                        ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4375
                        _v >>= 16;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4376
                    }
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4377
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4378
                    for (; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4379
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4380
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4381
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4382
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4383
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4384
                _p2 += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4385
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4386
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4387
            /* possible odd highByte of f2 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4388
            if (_p2 <= _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4389
#if !defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4390
                unsigned int _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4391
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4392
                _short3 = ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4393
                _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4394
                _v = (short1 * _p2[0]) + _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4395
                _pResult[0] = _v;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4396
                _pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4397
#else /* __LSBFIRST__ */
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4398
                _v = (short1 * _p2[0]) + ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4399
                ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4400
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4401
                _v >>= 16; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4402
                _pResult += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4403
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4404
                    unsigned char *_pResult1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4405
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4406
                    /* distribute carry - short-wise, then byte-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4407
                    _pResult1 = _pResult;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4408
#if defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4409
                    for (; _v; _pResult1 += 2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4410
                        if (_pResult1 > _pResultLastShort) break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4411
                        _v += ((unsigned short *)_pResult1)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4412
                        ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4413
                        _v >>= 16;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4414
                    }
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4415
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4416
                    for (; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4417
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4418
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4419
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4420
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4421
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4422
                _p2++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4423
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4424
        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4425
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4426
        /* possible odd highByte of f1 (or byteLoop, if above is ever disabled) */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4427
        for (; _p1 <= _p1Last; _p1++, _pResult0++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4428
            unsigned char *_pResult, *_p2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4429
            unsigned int byte1 = _p1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4430
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4431
            _pResult = _pResult0;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4432
            _p2 = otherBytes;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4433
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4434
            /* loop over shorts of f2 */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4435
            while (_p2 < _p2Last) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4436
#if !defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4437
                unsigned int _short2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4438
                unsigned int _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4439
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4440
                _short2 = ((unsigned short *)_p2)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4441
                _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4442
                _short3 = ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4443
                _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4444
                _v = (byte1 * _short2) + _short3;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4445
                _pResult[0] = _v;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4446
                _pResult[1] = _v >> 8;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4447
#else /* __LSBFIRST__ */
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4448
                _v = (byte1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4449
                ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
4266
de62998499ce checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4265
diff changeset
  4450
#endif
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4451
                _v >>= 16; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4452
                _pResult += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4453
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4454
                    unsigned char *_pResult1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4455
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4456
                    /* distribute carry - short-wise, then byte-wise */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4457
                    _pResult1 = _pResult;
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4458
#if defined(__LSBFIRST__)
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4459
                    for (_pResult1 = _pResult; _v; _pResult1 += 2) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4460
                        if (_pResult1 > _pResultLastShort) break;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4461
                        _v += ((unsigned short *)_pResult1)[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4462
                        ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4463
                        _v >>= 16;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4464
                    }
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4465
#endif /* __LSBFIRST__ */
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4466
                    for (; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4467
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4468
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4469
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4470
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4471
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4472
                _p2 += 2;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4473
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4474
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4475
            /* possible odd highByte of f2 (or byteLoop, if not __LSBFIRST__) */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4476
            while (_p2 <= _p2Last) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4477
                _v = (byte1 * _p2[0]) + _pResult[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4478
                _pResult[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4479
                _v >>= 8; /* now _v contains the carry*/
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4480
                _pResult++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4481
                if (_v) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4482
                    unsigned char *_pResult1;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4483
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4484
                    /* distribute carry */
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4485
                    for (_pResult1 = _pResult; _v; _pResult1++) {
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4486
                        _v += _pResult1[0];
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4487
                        _pResult1[0] = _v /* & 0xFF */;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4488
                        _v >>= 8;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4489
                    }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4490
                }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4491
                _p2++;
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4492
            }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4493
        }
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4494
        ok = true;
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4495
    }
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4496
%}.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  4497
    ok ifFalse:[
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4498
        1 to:len1 do:[:index1 |
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4499
            1 to:len2 do:[:index2 |
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4500
                dstIndex := index1 + index2 - 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4501
                prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4502
                prod := prod + (resultDigitByteArray basicAt:dstIndex).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4503
                resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4504
                carry := prod bitShift:-8.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4505
                carry ~~ 0 ifTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4506
                    idx := dstIndex + 1.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4507
                    [carry ~~ 0] whileTrue:[
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4508
                        v := (resultDigitByteArray basicAt:idx) + carry.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4509
                        resultDigitByteArray basicAt:idx put:(v bitAnd:255).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4510
                        carry := v bitShift:-8.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4511
                        idx := idx + 1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4512
                    ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4513
                ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4514
            ]
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4515
        ].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4516
    ].
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  4517
    ^ result compressed
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4518
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4519
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4520
absMulKarazuba:aLargeInteger
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4521
    "return a LargeInteger representing abs(self) * abs(theArgument) using the karazuba algorithm.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4522
        a = (2^n * p) + q
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4523
        b = (2^n * r) + s
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4524
        a * b   = ((2^n * p) + q) * ((2^n * r) + s)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4525
                = 2^(n+n)*p*r + 2^n*p*s + 2^n*q*r + q*s
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4526
                = 2^(n+n)*p*r + (p*r + q*s - (q-p)*(s-r))*2^n + q*s
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4527
                
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4528
        this is faster for sufficient large n1,n2 
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4529
        because regular multiplication is O(n1*n2) and karazuma multiplies much smaller numbers
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4530
        (half number of bits) but does more multiplations (on smaller numbers) and req's more
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4531
        additions and subtractions (on smaller numbers).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4532
        The break-even for when to use regular multiplication has been determined heuristically
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4533
        and is somewhere around 1600 bits (digitLength of 200).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4534
        (see test in absMul:)
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4535
        
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4536
        To disable karazuba, set UseKarazuba to false.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4537
    "
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4538
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4539
    "/ compute half-sized pi and qi...
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4540
    |l1 l2 n nh p q r s pr qs q_p s_r q_ps_r pr_raisedTo_n mdl_raisedTo_nh|
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4541
    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4542
    l1 := self digitLength.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4543
    l2 := aLargeInteger digitLength.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4544
    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4545
    n := l1 max:l2.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4546
    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4547
    nh := (n / 2) ceiling. n := nh * 2.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4548
    p := LargeInteger digitBytes:(digitByteArray copyFrom:nh+1) sign:1. "/ high bits of nr1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4549
    q := LargeInteger digitBytes:(digitByteArray copyToMax:nh) sign:1.   "/ low bits of nr1
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4550
    r := LargeInteger digitBytes:(aLargeInteger digitBytes copyFrom:nh+1) sign:1. "/ high bits of nr2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4551
    s := LargeInteger digitBytes:(aLargeInteger digitBytes copyToMax:nh).           "/ low bits of nr2
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4552
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4553
    p := p compressed.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4554
    q := q compressed.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4555
    r := r compressed.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4556
    s := s compressed.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4557
    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4558
    pr := p * r.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4559
    qs := q * s.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4560
    q_p := q - p.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4561
    s_r := s - r.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4562
    q_ps_r := q_p * s_r.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4563
    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4564
    pr_raisedTo_n := (pr bitShift:(n*8)).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4565
    mdl_raisedTo_nh := ((pr + qs - q_ps_r) bitShift:(nh*8)).
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4566
    ^ pr_raisedTo_n + mdl_raisedTo_nh + qs
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4567
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4568
    "
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4569
     #(100 500 1000 2500 5000 10000 25000 50000 100000 250000 500000 1000000) do:[:exp |
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4570
         |nr r1 r2|
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4571
         nr := (3 raisedTo:exp) asInteger.
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4572
         Transcript show:exp; show:' nbytes: '; showCR:nr digitLength;
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  4573
            show:'  normal: '; show:(Time microsecondsToRun:[ UseKarazuba := false. r1 := nr * nr ]); showCR:'us';
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  4574
            show:'  karazuba: '; show:(Time microsecondsToRun:[ UseKarazuba := true. r2 := nr absMulKarazuba: nr]); showCR:'us'.
18847
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4575
         self assert:(r1 = r2).    
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4576
     ] 
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4577
    "
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4578
!
bb459d34d125 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18845
diff changeset
  4579
4174
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4580
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
  4581
    "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
  4582
     with sign: newSign.
62498b719e87 pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents: 4173
diff changeset
  4583
     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
  4584
     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
  4585
     This is a helper for addition and subtraction - not for public use."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4586
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4587
    |result done otherDigitByteArray resultDigitByteArray
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4588
     len1   "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4589
     len2   "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4590
     newLen "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4591
     index  "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4592
     carry  "{ Class: SmallInteger }"
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4593
     sum    "{ Class: SmallInteger }" |
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  4594
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  4595
    otherDigitByteArray := aLargeInteger digitBytes.
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4596
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4597
%{
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4598
    OBJ _digitByteArray = __INST(digitByteArray);
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4599
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4600
    if (__isByteArray(_digitByteArray)
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  4601
     && __isByteArray(otherDigitByteArray)) {
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4602
	int _len1, _len2, _newLen;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4603
	unsigned char *_myDigits, *_otherDigits, *_newDigits;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4604
	int _index, _carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4605
	int _comLen;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4606
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4607
	_len1 = __byteArraySize(_digitByteArray);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4608
	_len2 = __byteArraySize(otherDigitByteArray);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4609
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4610
	_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4611
	_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4612
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4613
	if (_len1 < _len2) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4614
	    _comLen = _len1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4615
	    _newLen = _len2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4616
	    if (_otherDigits[_len2 - 1] == 0xFF) _newLen++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4617
	} else if (_len2 < _len1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4618
	    _comLen = _len2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4619
	    _newLen = _len1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4620
	    if (_myDigits[_len1 - 1] == 0xFF) _newLen++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4621
	} else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4622
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4623
	     * there can only be an overflow from the high bytes,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4624
	     * if their sum is >= 255
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4625
	     * (with sum==255, a carry could still occur from the next lower bytes)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4626
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4627
	    _newLen = _len1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4628
	    if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4629
		_newLen++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4630
	    } else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4631
		if (_newLen == sizeof(INT)) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4632
		    OBJ _uint;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4633
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4634
		    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4635
		     * two word-sized numbers, no carry - a very common case ...
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4636
		     */
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4637
#if defined(__LSB_FIRST__)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4638
		    unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits;
4216
65f14da89fd4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4215
diff changeset
  4639
#else
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4640
		    unsigned INT _sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aLargeInteger);
4216
65f14da89fd4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4215
diff changeset
  4641
#endif /* not LSB_FIRST */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4642
		    if (_sum <= _MAX_INT) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4643
			_uint = __mkSmallInteger(_sum * __intVal(newSign));
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4644
		    } else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4645
			_uint = __MKULARGEINT(_sum);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4646
			__LargeIntegerInstPtr(_uint)->l_sign = newSign;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4647
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4648
		    RETURN (_uint);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4649
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4650
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4651
	    _comLen = _len1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4652
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4653
	resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4654
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4655
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4656
	 * must refetch - GC could have been invoked
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4657
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4658
	_digitByteArray = __INST(digitByteArray);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4659
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4660
	_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4661
	_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4662
	_newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4663
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4664
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4665
	 * add them ...
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4666
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4667
	_index = 1;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4668
	_carry = 0;
4172
3fb2b94e7535 tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents: 4171
diff changeset
  4669
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4670
#if defined(__LSBFIRST__)
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4671
# if (__POINTER_SIZE__ == 8) && defined(__GNUC__)
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4672
#  if 0  /* not faster (on alpha) */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4673
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4674
	    int _comLen7;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4675
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4676
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4677
	     * have a 64bit integers;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4678
	     * add quad-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4679
	     * accessing bytes at: [index-1][index][index+1]..[index+6]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4680
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4681
	    _comLen7 = _comLen - 3 - 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4682
	    while (_index <= _comLen7) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4683
		UINT64 _sum, _t1, _t2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4684
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4685
		asm ("addq   %5,%6,%1         /* sum */                  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4686
		      addq   %0,%1,%1         /* plus carryIn */         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4687
		      cmpult %1,%5,%2         /* was there a carry ? */  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4688
		      cmpult %1,%6,%3         /* was there a carry ? */  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4689
		      bis    %2,%3,%0         /* carryOut */             \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4690
		     "
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4691
			: "=r"  (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4692
			  "=r"  (_sum),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4693
			  "r"   (_t1),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4694
			  "r"   (_t2)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4695
			: "r"   (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4696
			  "r"   (((unsigned long *)(&(_myDigits[_index - 1])))[0]),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4697
			  "r"   (((unsigned long *)(&(_otherDigits[_index - 1])))[0])
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4698
		    );
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4699
		/* _sum = _sum & 0xFFFFFFFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4700
		((unsigned long *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4701
		_index += 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4702
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4703
	}
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4704
#  endif
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4705
# endif /* 64bit */
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4706
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4707
# if (__POINTER_SIZE__ == 8)
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4708
# if 0  /* not faster (on alpha) */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4709
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4710
	    int _comLen7;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4711
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4712
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4713
	     * have a 64bit integers;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4714
	     * add quad-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4715
	     * accessing bytes at: [index-1][index][index+1]..[index+6]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4716
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4717
	    _comLen7 = _comLen - 3 - 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4718
	    while (_index <= _comLen7) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4719
		UINT64 _sum, _t1, _t2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4720
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4721
		_t1 = ((UINT64 *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4722
		_t2 = ((UINT64 *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4723
		_sum = _t1 + _t2 + _carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4724
		((UINT64 *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4725
		_carry = (_sum < _t1) | (_sum < _t2);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4726
		_index += 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4727
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4728
	}
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4729
#  endif
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8907
diff changeset
  4730
# endif /* 64bit */
4263
7f7caf39ebb4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  4731
7f7caf39ebb4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  4732
# ifdef UINT64
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4733
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4734
	    int _comLen3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4735
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4736
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4737
	     * have a 64bit integer type;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4738
	     * add int-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4739
	     * accessing bytes at: [index-1][index][index+1][index+2]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4740
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4741
	    _comLen3 = _comLen - 3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4742
	    while (_index <= _comLen3) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4743
		UINT64 _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4744
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4745
		/* do not merge the 3 lines below into one -
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4746
		 * (will do sign extension then, which is wrong here)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4747
		 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4748
		_sum = (unsigned)_carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4749
		_sum += ((unsigned int *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4750
		_sum += ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4751
		_carry = _sum >> 32;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4752
		/* _sum = _sum & 0xFFFFFFFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4753
		((unsigned int *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4754
		_index += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4755
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4756
	}
4172
3fb2b94e7535 tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents: 4171
diff changeset
  4757
# else
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
  4758
#  if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4759
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4760
	    int _comLen3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4761
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4762
	    _comLen3 = _comLen - 3 - 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4763
	    while (_index <= _comLen3) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4764
		unsigned int _sum, _sum2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4765
		unsigned int __in1A, __in1B, __in2A, __in2B;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4766
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4767
		__in1A = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4768
		__in2A = ((unsigned int *)(&(_myDigits[_index - 1])))[1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4769
		__in1B = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4770
		__in2B = ((unsigned int *)(&(_otherDigits[_index - 1])))[1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4771
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4772
		asm ("addl %%edx,%%eax  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4773
		      movl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4774
		      adcl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4775
		      addl %5,%%eax     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4776
		      adcl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4777
					\n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4778
		      addl %%edx,%%ecx  \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4779
		      movl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4780
		      adcl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4781
		      addl %7,%%ecx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4782
		      adcl $0,%%edx     \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4783
		     "
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4784
			: "=d"  (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4785
			  "=a"  (_sum),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4786
			  "=c"  (_sum2)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4787
			: "0"   (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4788
			  "1"   (__in1A),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4789
			  "rm"  (__in1B),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4790
			  "2"   (__in2A),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4791
			  "rm"  (__in2B)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4792
		    );
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4793
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4794
		((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4795
		((unsigned *)(&(_newDigits[_index - 1])))[1] = _sum2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4796
		_index += 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4797
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4798
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4799
	     * add int-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4800
	     * accessing bytes at: [index-1][index][index+1][index+2]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4801
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4802
	    _comLen3 = _comLen3 + 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4803
	    if (_index <= _comLen3) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4804
		unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4805
		unsigned int __inA, __inB;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4806
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4807
		__inA = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4808
		__inB = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4809
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4810
		asm ("addl %%edx,%%eax      \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4811
		      movl $0,%%edx         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4812
		      adcl $0,%%edx         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4813
		      addl %4,%%eax         \n\
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4814
		      adcl $0,%%edx"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4815
			: "=d"  (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4816
			  "=a"  (_sum)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4817
			: "0"   (_carry),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4818
			  "1"   (__inA),
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4819
			  "rm"  (__inB)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4820
		    );
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4821
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4822
		((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4823
		_index += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4824
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4825
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4826
#  endif /* __i386__ && GNUC */
10342
58ce3aabaa4b Visual-C support
Stefan Vogel <sv@exept.de>
parents: 8929
diff changeset
  4827
#  if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4828
	{
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4829
	    int _comLen3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4830
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4831
	    /*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4832
	     * add long-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4833
	     * accessing bytes at: [index-1][index][index+1][index+2]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4834
	     */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4835
	    _comLen3 = _comLen - 3;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4836
	    while (_index <= _comLen3) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4837
		unsigned int _sum, _v1, _v2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4838
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4839
		_v1 = ((unsigned int *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4840
		_v2 = ((unsigned int *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4841
		asm {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4842
		      mov eax, _v1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4843
		      add eax, _v2
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4844
		      mov edx, 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4845
		      adc edx, 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4846
		      add eax, _carry
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4847
		      adc edx, 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4848
		      mov _carry, edx
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4849
		      mov _sum, eax
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4850
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4851
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4852
		((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4853
		_index += 4;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4854
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4855
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4856
#  endif /* __i386__ && WIN32 */
6665
8698bfaf34e2 Fixes for gcc3
Stefan Vogel <sv@exept.de>
parents: 6480
diff changeset
  4857
# endif /* INT64 */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4858
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4859
	 * add short-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4860
	 * accessing bytes at: [index-1][index]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4861
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4862
	while (_index < _comLen) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4863
	    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4864
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4865
	    _sum = _carry
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4866
		   + ((unsigned short *)(&(_myDigits[_index - 1])))[0]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4867
		   + ((unsigned short *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4868
	    _carry = _sum >> 16;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4869
	    /* _sum = _sum & 0xFFFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4870
	    *(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4871
	    _index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4872
	}
4284
20f47ad19322 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4283
diff changeset
  4873
#else
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4874
# ifdef __sparc__
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4875
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4876
	 * add short-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4877
	 * accessing bytes at: [index-1][index]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4878
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4879
	while (_index < _comLen) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4880
	    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4881
	    unsigned short _v1, _v2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4882
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4883
	    _v1 = ((unsigned short *)(&(_myDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4884
	    _v2 = ((unsigned short *)(&(_otherDigits[_index - 1])))[0];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4885
	    _sum = _carry + (_v1>>8) + (_v2>>8);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4886
	    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4887
	    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4888
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4889
	    _sum = _carry + (_v1 & 0xFF) + (_v2 & 0xFF);
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4890
	    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4891
	    _newDigits[_index] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4892
	    _index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4893
	}
4284
20f47ad19322 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4283
diff changeset
  4894
# endif
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4895
#endif /* __LSBFIRST__ */
3119
65247b9d1201 oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents: 3099
diff changeset
  4896
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4897
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4898
	 * add byte-wise
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4899
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4900
	while (_index <= _comLen) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4901
	    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4902
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4903
	    _sum = _carry
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4904
		   + _myDigits[_index - 1]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4905
		   + _otherDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4906
	    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4907
	    /* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4908
	    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4909
	    _index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4910
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4911
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4912
	/*
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4913
	 * rest
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4914
	 */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4915
	if (_len1 > _len2) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4916
#if defined(__LSBFIRST__)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4917
	    if (_index <= _len1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4918
		if ((_index - 1) & 1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4919
		    /* odd byte */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4920
		    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4921
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4922
		    _sum = _carry + _myDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4923
		    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4924
		    /* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4925
		    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4926
		    _index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4927
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4928
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4929
		while (_index < _len1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4930
		    /* shorts */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4931
		    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4932
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4933
		    _sum = _carry + *(unsigned short *)(&(_myDigits[_index - 1]));
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4934
		    _carry = _sum >> 16;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4935
		    /* _sum = _sum & 0xFFFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4936
		    *(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4937
		    _index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4938
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4939
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4940
		if (_index <= _len1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4941
		    /* last byte */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4942
		    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4943
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4944
		    _sum = _carry + _myDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4945
		    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4946
		    /* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4947
		    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4948
		    _index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4949
		}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4950
	    }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4951
#else
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4952
	    while (_index <= _len1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4953
		unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4954
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4955
		_sum = _carry + _myDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4956
		_carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4957
		/* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4958
		_newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4959
		_index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4960
	    }
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4961
#endif /* not LSB */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4962
	} else {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4963
	    if (_len2 > _len1) {
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  4964
#if defined(__LSBFIRST__)
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4965
		if (_index <= _len2) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4966
		    if ((_index - 1) & 1) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4967
			/* odd byte */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4968
			unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4969
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4970
			_sum = _carry + _otherDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4971
			_carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4972
			/* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4973
			_newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4974
			_index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4975
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4976
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4977
		    while (_index < _len2) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4978
			/* shorts */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4979
			unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4980
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4981
			_sum = _carry + *(unsigned short *)(&(_otherDigits[_index - 1]));
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4982
			_carry = _sum >> 16;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4983
			/* _sum = _sum & 0xFFFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4984
			*(unsigned short *)(&(_newDigits[_index - 1])) = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4985
			_index += 2;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4986
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4987
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4988
		    if (_index <= _len2) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4989
			/* last byte */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4990
			unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4991
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4992
			_sum = _carry + _otherDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4993
			_carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4994
			/* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4995
			_newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4996
			_index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4997
		    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  4998
		}
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  4999
#else
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5000
		while (_index <= _len2) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5001
		    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5002
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5003
		    _sum = _carry + _otherDigits[_index - 1];
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5004
		    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5005
		    /* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5006
		    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5007
		    _index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5008
		}
4256
04905f1fa63f more fixes
Claus Gittinger <cg@exept.de>
parents: 4255
diff changeset
  5009
#endif /* not LSB */
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5010
	    }
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5011
	}
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5012
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5013
	while (_index <= _newLen) {
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5014
	    unsigned int _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5015
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5016
	    _sum = _carry;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5017
	    _carry = _sum >> 8;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5018
	    /* _sum = _sum & 0xFF; */
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5019
	    _newDigits[_index - 1] = _sum;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5020
	    _index++;
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5021
	}
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5022
    }
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5023
%}.
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5024
    resultDigitByteArray notNil ifTrue:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5025
	result := self class basicNew.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5026
	result setDigits:resultDigitByteArray.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5027
	result setSign:newSign.
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5028
    ] ifFalse:[
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5029
	len1 := digitByteArray size.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5030
	len2 := otherDigitByteArray size.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5031
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5032
	"/ earlier versions estimated the newLength as:
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5033
	"/ (len1 max:len2) + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5034
	"/ and reduced the result.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5035
	"/ however, if one of the addends is smaller,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5036
	"/ the result will never require another digit,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5037
	"/ if the highest digit of the larger addent is
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5038
	"/ not equal to 255. Therefore, in most cases,
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5039
	"/ we can avoid the computation and resizing
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5040
	"/ in #reduced.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5041
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5042
	len1 < len2 ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5043
	    newLen := len2.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5044
	    (otherDigitByteArray at:len2) == 16rFF ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5045
		newLen := newLen + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5046
	    ]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5047
	] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5048
	    len2 < len1 ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5049
		newLen := len1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5050
		(digitByteArray at:len1) == 16rFF ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5051
		    newLen := newLen + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5052
		]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5053
	    ] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5054
		newLen := len1 + 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5055
	    ]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5056
	].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5057
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5058
	result := self class basicNew numberOfDigits:newLen.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5059
	result sign:newSign.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5060
	resultDigitByteArray := result digitBytes.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5061
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5062
	index := 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5063
	carry := 0.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5064
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5065
	done := false.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5066
	[done] whileFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5067
	    sum := carry.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5068
	    (index <= len1) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5069
		sum := sum + (digitByteArray basicAt:index).
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5070
		(index <= len2) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5071
		    sum := sum + (otherDigitByteArray basicAt:index)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5072
		]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5073
	    ] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5074
		(index <= len2) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5075
		    sum := sum + (otherDigitByteArray basicAt:index)
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5076
		] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5077
		    "end reached"
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5078
		    done := true
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5079
		]
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5080
	    ].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5081
	    (sum >= 16r100) ifTrue:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5082
		carry := 1.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5083
		sum := sum - 16r100
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5084
	    ] ifFalse:[
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5085
		carry := 0
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5086
	    ].
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5087
	    resultDigitByteArray basicAt:index put:sum.
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5088
	    index := index + 1
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5089
	].
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5090
    ].
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5091
1883
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  5092
    ^ result compressed
d3fbf4b36be7 renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents: 1857
diff changeset
  5093
2848
8be5134b3679 tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents: 2816
diff changeset
  5094
    "Modified: 11.8.1997 / 03:23:37 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5095
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5096
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5097
absSubtract:aLargeInteger
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5098
    "private helper for division:
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5099
	destructively subtract aLargeInteger from myself
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5100
	AND return true, if the result is non-zero, false otherwise.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5101
	(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
  5102
	 on the receiver)
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5103
	Only allowed for positive receiver and argument
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5104
	The receiver must be >= the argument.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5105
	The receiver must be a temporary scratch-number"
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5106
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5107
    |otherDigitByteArray
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5108
     len1   "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5109
     len2   "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5110
     index  "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5111
     borrow "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5112
     diff   "{ Class: SmallInteger }"
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5113
     notZero
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5114
    |
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5115
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5116
    notZero := false.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5117
    len1 := digitByteArray size.
4828
796dcc9903cf New:#digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents: 4806
diff changeset
  5118
    otherDigitByteArray := aLargeInteger digitBytes.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5119
    len2 := otherDigitByteArray size.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5120
    len2 > len1 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5121
	[(otherDigitByteArray at:len2) == 0] whileTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5122
	    len2 := len2 - 1
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5123
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5124
	len2 > len1 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5125
	    self error:'operation failed' "/ may not be called that way
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5126
	].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5127
    ].
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5128
    "/ knowing that len2 is <= len1
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5129
%{
4189
Claus Gittinger <cg@exept.de>
parents: 4188
diff changeset
  5130
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5131
    OBJ _digitByteArray = __INST(digitByteArray);
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5132
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5133
    if (__isByteArray(_digitByteArray)
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5134
     && __isByteArray(otherDigitByteArray)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5135
	int _len1 = __intVal(len1),
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5136
	    _len2 = __intVal(len2);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5137
	unsigned char *_myDigits, *_otherDigits;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5138
	int _index = 1, _borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5139
	INT _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5140
	int anyBitNonZero = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5141
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5142
	_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5143
	_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5144
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  5145
#if defined(__LSBFIRST__)
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  5146
# if __POINTER_SIZE__ == 8
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5147
	{
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5148
	    int _len2Q;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5149
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5150
	     * subtract int-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5151
	     */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5152
	    _len2Q = _len2-2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5153
	    while (_index < _len2Q) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5154
		/* 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
  5155
		_diff = ((unsigned int *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5156
		_diff -= ((unsigned int *)(_otherDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5157
		_diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5158
		if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5159
		    _borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5160
		} else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5161
		    _borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5162
		    /* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5163
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5164
		((unsigned int *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5165
		anyBitNonZero |= (_diff & 0xFFFFFFFFL);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5166
		_index += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5167
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5168
	}
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  5169
# endif
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8628
diff changeset
  5170
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5171
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5172
	 * subtract short-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5173
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5174
	while (_index < _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5175
	    /* 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
  5176
	    _diff = ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5177
	    _diff -= ((unsigned short *)(_otherDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5178
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5179
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5180
		_borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5181
	    } else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5182
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5183
		/* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5184
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5185
	    ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5186
	    anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5187
	    _index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5188
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5189
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5190
	if (_index <= _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5191
	    /*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5192
	     * cannot continue with shorts - there is an odd number of
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5193
	     * bytes in the minuent
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
	} else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5196
	    while (_index < _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5197
		/* 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
  5198
		_diff = ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5199
		_diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5200
		if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5201
		    /* _borrow = 0; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5202
		    ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5203
		    anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5204
		    _index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5205
		    while (_index < _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5206
			anyBitNonZero |= ((unsigned short *)(_myDigits+_index-1))[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5207
			if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5208
			    RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5209
			}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5210
			_index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5211
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5212
		    /* last odd index */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5213
		    if (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5214
			anyBitNonZero |= _myDigits[_index - 1];;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5215
			if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5216
			    RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5217
			}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5218
			_index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5219
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5220
		    RETURN (anyBitNonZero ? true : false);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5221
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5222
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5223
		/* _diff += 0x10000; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5224
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5225
		((unsigned short *)(_myDigits+_index-1))[0] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5226
		anyBitNonZero |= (_diff & 0xFFFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5227
		_index += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5228
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5229
	}
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5230
#endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5231
	/*
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5232
	 * subtract byte-wise
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5233
	 */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5234
	while (_index <= _len2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5235
	    /* 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
  5236
	    _diff = _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5237
	    _diff -= _otherDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5238
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5239
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5240
		_borrow = 0;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5241
	    } else {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5242
		_borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5243
		/* _diff += 0x100; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5244
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5245
	    _myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5246
	    anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5247
	    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5248
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5249
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5250
	while (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5251
	    /* 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
  5252
	    _diff = _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5253
	    _diff -= _borrow;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5254
	    if (_diff >= 0) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5255
		/* _borrow = 0; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5256
		_myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5257
		anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5258
		_index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5259
		while (_index <= _len1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5260
		    anyBitNonZero |= _myDigits[_index - 1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5261
		    if (anyBitNonZero) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5262
			RETURN (true);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5263
		    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5264
		    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5265
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5266
		break;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5267
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5268
	    _borrow = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5269
	    /* _diff += 0x100; */
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5270
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5271
	    _myDigits[_index - 1] = _diff;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5272
	    anyBitNonZero |= (_diff & 0xFF);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5273
	    _index++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5274
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5275
	RETURN (anyBitNonZero ? true : false);
4187
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5276
    }
3dc575bd3915 tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents: 4185
diff changeset
  5277
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5278
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5279
    index := 1.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5280
    borrow := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5281
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5282
    [index <= len1] whileTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5283
	diff := borrow.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5284
	diff := diff + (digitByteArray basicAt:index).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5285
	index <= len2 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5286
	    diff := diff - (otherDigitByteArray basicAt:index).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5287
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5288
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5289
	"/ workaround for
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5290
	"/ gcc code generator bug
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5291
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5292
	(diff >= 0) ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5293
	    borrow := 0
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5294
	] ifFalse:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5295
	    borrow := -1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5296
	    diff := diff + 16r100
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5297
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5298
	diff ~~ 0 ifTrue:[
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5299
	    notZero := true
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5300
	].
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5301
	digitByteArray basicAt:index put:diff.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5302
	index := index + 1
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5303
    ].
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5304
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5305
    ^ notZero
4137
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5306
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5307
    "Created: / 5.11.1996 / 16:23:47 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5308
    "Modified: / 5.11.1996 / 18:56:50 / cg"
f85d341a13e4 Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents: 3984
diff changeset
  5309
    "Modified: / 27.4.1999 / 18:08:23 / stefan"
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5310
!
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5311
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5312
div2
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5313
    "private helper for division:
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5314
       destructively divide the receiver by 2.
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5315
       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
  5316
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5317
    |prevBit|
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5318
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5319
%{  /* NOCONTEXT */
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5320
    OBJ __digits = __INST(digitByteArray);
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5321
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5322
    if (__isByteArray(__digits)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5323
	int __nBytes = __byteArraySize(__digits);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5324
	unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5325
	unsigned INT __this, __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5326
	int __idx;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5327
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5328
	if (__nBytes == 1) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5329
	    __bp[0] >>= 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5330
	    RETURN (self);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5331
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5332
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5333
	__idx = 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5334
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5335
#if defined(__LSBFIRST__)
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5336
# if (__POINTER_SIZE__ == 8)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5337
	if (sizeof(unsigned INT) == 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5338
	    int __endIndex = __nBytes - 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5339
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5340
	    if (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5341
		__this = ((unsigned INT *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5342
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5343
		while (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5344
		    __next = ((unsigned INT *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5345
		    __this = (__this >> 1) /* & 0x7FFFFFFFFFFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5346
		    __this |= __next << 63;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5347
		    ((unsigned INT *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5348
		    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5349
		    __bp += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5350
		    __idx += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5351
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5352
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5353
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5354
	    if (__idx < (__nBytes - 4)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5355
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5356
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5357
		__next = ((unsigned int *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5358
		__this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5359
		__this |= __next << 31;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5360
		((unsigned int *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5361
		__this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5362
		__bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5363
		__idx += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5364
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5365
	    if (__idx < (__nBytes - 2)) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5366
		__this = ((unsigned short *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5367
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5368
		__next = ((unsigned short *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5369
		__this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5370
		__this |= __next << 15;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5371
		((unsigned short *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5372
		__this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5373
		__bp += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5374
		__idx += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5375
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5376
	}
15326
7cc303092044 class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 15312
diff changeset
  5377
# else
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5378
	if (sizeof(unsigned int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5379
	    int __endIndex = __nBytes - 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5380
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5381
	    if (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5382
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5383
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5384
		while (__idx < __endIndex) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5385
		    __next = ((unsigned int *)__bp)[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5386
		    __this = (__this >> 1) /* & 0x7FFFFFF */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5387
		    __this |= __next << 31;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5388
		    ((unsigned int *)__bp)[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5389
		    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5390
		    __bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5391
		    __idx += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5392
		}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5393
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5394
	}
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5395
# endif
3119
65247b9d1201 oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents: 3099
diff changeset
  5396
#endif
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5397
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5398
	__this = __bp[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5399
	while (__idx < __nBytes) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5400
	    __next = __bp[1];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5401
	    __this >>= 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5402
	    __this |= __next << 7;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5403
	    __bp[0] = __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5404
	    __this = __next;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5405
	    __bp++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5406
	    __idx++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5407
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5408
	__bp[0] = __this >> 1;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5409
	RETURN (self);
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5410
    }
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5411
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5412
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5413
    prevBit := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5414
    digitByteArray size to:1 by:-1 do:[:idx |
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5415
	|thisByte|
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5416
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5417
	thisByte := digitByteArray at:idx.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5418
	digitByteArray at:idx put:((thisByte bitShift:-1) bitOr:prevBit).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5419
	prevBit := (thisByte bitAnd:1) bitShift:7.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5420
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5421
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5422
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5423
     100000 asLargeInteger div2
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5424
     1000000000000000000000000000 div2
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5425
     10000000000000000000000000000000000000000000 div2
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5426
    "
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5427
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5428
    "Modified: 5.11.1996 / 16:12:56 / cg"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5429
!
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5430
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5431
mul2
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5432
    "private helper for division:
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5433
       destructively multiply the receiver by 2."
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5434
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5435
    |nBytes "{ Class: SmallInteger }"
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5436
     b      "{ Class: SmallInteger }"
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5437
     t prevBit|
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5438
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5439
    nBytes := digitByteArray size.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5440
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5441
    b := digitByteArray at:nBytes.
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5442
    (b bitAnd:16r80) ~~ 0 ifTrue:[
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5443
	"/ need another byte
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5444
	nBytes := nBytes + 1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5445
	t := ByteArray uninitializedNew:nBytes.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5446
	t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5447
	t at:nBytes put:0.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5448
	digitByteArray := t.
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5449
    ].
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5450
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5451
%{
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5452
    OBJ __digits = __INST(digitByteArray);
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5453
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5454
    if (__isByteArray(__digits)) {
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5455
	int __nBytes = __intVal(nBytes);
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5456
	unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5457
	unsigned INT __carry = 0, __newCarry;
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5458
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5459
#if defined(__LSBFIRST__)
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5460
# if (__POINTER_SIZE__ == 8)
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5461
	if (sizeof(unsigned INT) == 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5462
	    while (__nBytes >= 8) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5463
		unsigned INT __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5464
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5465
		__this = ((unsigned INT *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5466
		__newCarry = (__this >> 63) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5467
		((unsigned INT *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5468
		__carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5469
		__bp += 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5470
		__nBytes -= 8;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5471
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5472
	}
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5473
# endif
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5474
	if (sizeof(unsigned int) == 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5475
	    while (__nBytes >= 4) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5476
		unsigned int __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5477
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5478
		__this = ((unsigned int *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5479
		__newCarry = (__this >> 31) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5480
		((unsigned int *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5481
		__carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5482
		__bp += 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5483
		__nBytes -= 4;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5484
	    }
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5485
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5486
	if (__nBytes >= 2) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5487
	    unsigned short __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5488
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5489
	    __this = ((unsigned short *)__bp)[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5490
	    __newCarry = (__this >> 15) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5491
	    ((unsigned short *)__bp)[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5492
	    __carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5493
	    __bp += 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5494
	    __nBytes -= 2;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5495
	}
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5496
#endif /* LSBFIRST */
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5497
	while (__nBytes) {
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5498
	    unsigned char __this;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5499
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5500
	    __this = __bp[0];
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5501
	    __newCarry = (__this >> 7) /* & 1 */;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5502
	    __bp[0] = (__this << 1) | __carry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5503
	    __carry = __newCarry;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5504
	    __bp++;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5505
	    __nBytes--;
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5506
	}
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5507
	RETURN (self);
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5508
    }
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5509
%}.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5510
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5511
    prevBit := 0.
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5512
    1 to:digitByteArray size do:[:idx |
16916
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5513
	|thisByte|
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5514
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5515
	thisByte := digitByteArray at:idx.
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5516
	digitByteArray at:idx put:(((thisByte bitShift:1) bitAnd:16rFF) bitOr:prevBit).
62312c3220bf class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 16520
diff changeset
  5517
	prevBit := (thisByte bitShift:-7) bitAnd:1.
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5518
    ].
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5519
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5520
    "
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5521
     100000 asLargeInteger mul2
5437
4d49a24f861b added fallback st-code (for rel5 migration)
Claus Gittinger <cg@exept.de>
parents: 5382
diff changeset
  5522
     16r7FFFFFFFFFFF copy mul2 hexPrintString
8905
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5523
     16rFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5524
     16r7FFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5525
     16rFFFFFFFFFFFFFF copy mul2 hexPrintString
303421b7345a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  5526
     10000000000000000000000000000 mul2
1890
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5527
    "
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5528
aca066e06680 division, again
Claus Gittinger <cg@exept.de>
parents: 1883
diff changeset
  5529
    "Modified: 5.11.1996 / 16:37:32 / cg"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5530
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5531
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5532
mul256
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5533
    "private helper for division:
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5534
       destructively multiply the receiver by 256."
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5535
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5536
    |newDigits newSize|
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5537
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5538
    newSize := digitByteArray size + 1.
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5539
    newDigits := ByteArray uninitializedNew:newSize.
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5540
    newDigits replaceBytesFrom:2 to:newSize with:digitByteArray startingAt:1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5541
    newDigits at:1 put:0.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5542
    digitByteArray := newDigits
4160
5fb87a530e29 tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents: 4157
diff changeset
  5543
4215
d3d8a954cd3c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4214
diff changeset
  5544
    "Modified: / 20.5.1999 / 09:16:42 / cg"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5545
!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5546
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5547
numberOfDigits:nDigits
2001
8c2b957f0cb3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1983
diff changeset
  5548
"/    digitByteArray := ByteArray uninitializedNew:nDigits.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5549
    digitByteArray := ByteArray new:nDigits.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5550
    sign := 1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5551
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5552
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5553
numberOfDigits:nDigits sign:newSign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5554
"/    digitByteArray := ByteArray uninitializedNew:nDigits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5555
    digitByteArray := ByteArray new:nDigits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5556
    sign := newSign.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5557
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5558
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5559
setDigits:digits
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5560
    digitByteArray := digits.
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5561
    sign := 1.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5562
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5563
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5564
setDigits:digits sign:newSign
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5565
    digitByteArray := digits.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5566
    sign := newSign.
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5567
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5568
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5569
setSign:aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5570
    "destructively change the sign of the receiver"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5571
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5572
    sign := aNumber
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5573
!
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5574
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5575
sign:aNumber
18381
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5576
    <resource: #obsolete>
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5577
    "destructively change the sign of the receiver"
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5578
7614c149c77e class: LargeInteger
Claus Gittinger <cg@exept.de>
parents: 18240
diff changeset
  5579
    ^ self setSign:aNumber
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5580
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5581
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5582
!LargeInteger methodsFor:'queries'!
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5583
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5584
nextPowerOf2
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5585
    "return the power of 2 at or above the receiver.
19059
687547192a05 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19052
diff changeset
  5586
     Notice, that for a powerOf2, the receiver is returned.
687547192a05 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19052
diff changeset
  5587
     Also notice, that (because it is used for padding), 
687547192a05 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19052
diff changeset
  5588
     0 is returned for zero."
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5589
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5590
    |newBytes nBytes hi|
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5591
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5592
    "/ assume I am normalized
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5593
    nBytes := digitByteArray size.
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5594
    hi := digitByteArray at:nBytes.
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5595
    self assert:(hi ~~ 0).
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5596
    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5597
    newBytes := ByteArray new:(nBytes) withAll:16rFF.
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5598
    (hi isPowerOf:2) ifTrue:[
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5599
        newBytes at:nBytes put:((hi bitShift:1)- 1).
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5600
    ] ifFalse:[    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5601
        newBytes at:nBytes put:(hi nextPowerOf2 - 1).
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5602
    ].    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5603
    ^ (LargeInteger digitBytes:newBytes) + 1
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5604
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5605
    "
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5606
     100 factorial nextPowerOf2  isPowerOf:2  
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5607
     1000 factorial nextPowerOf2  isPowerOf:2
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5608
     Time millisecondsToRun:[
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5609
         |v|
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5610
         v := 1000 factorial.
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5611
         1000 timesRepeat:[
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5612
            v nextPowerOf2    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5613
         ]    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5614
     ]    
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5615
    "
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5616
! !
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5617
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5618
!LargeInteger methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5619
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5620
even
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5621
    "return true if the receiver is even"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5622
19102
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5623
%{  /* NOCONTEXT */
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5624
#ifdef __SCHTEAM__
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5625
    return context._RETURN( ((STLargeInteger)self).isEven() ? STObject.True : STObject.False);
19102
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5626
#else
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5627
    OBJ digits = __INST(digitByteArray);
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5628
    
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5629
    if (__isByteArray(digits)){
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5630
        if (__byteArraySize(digits) > 0) {
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5631
            unsigned char b = __ByteArrayInstPtr(digits)->ba_element[0];
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5632
            RETURN( (b & 1) ? false : true );
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5633
        }
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5634
    }    
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5635
#endif
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5636
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5637
    ^ (digitByteArray at:1) even
19102
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5638
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5639
    "
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5640
     16r100000000000000000001 even
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5641
     16r100000000000000000001 odd
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5642
     16r100000000000000000000 even
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5643
     16r100000000000000000000 odd
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5644
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5645
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5646
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5647
negative
18855
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  5648
    "return true, if the receiver is less than zero"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5649
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5650
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5651
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5652
    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
  5653
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5654
%}.
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5655
    ^ (sign < 0)
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5656
!
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  5657
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5658
odd
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5659
    "return true if the receiver is odd"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5660
19102
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5661
%{  /* NOCONTEXT */
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5662
#ifdef __SCHTEAM__
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5663
    return context._RETURN( ((STLargeInteger)self).isEven() ? STObject.False : STObject.True);
19102
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5664
#else
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5665
    OBJ digits = __INST(digitByteArray);
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5666
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5667
    if (__isByteArray(digits)){
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5668
        if (__byteArraySize(digits) > 0) {
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5669
            unsigned char b = __ByteArrayInstPtr(digits)->ba_element[0];
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5670
            RETURN( (b & 1) ? true : false );
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5671
        }
3ba85ef22c95 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19059
diff changeset
  5672
    }    
18903
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5673
#endif
5fc592d100b5 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18900
diff changeset
  5674
%}.
1983
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5675
    ^ (digitByteArray at:1) odd
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5676
c5a2c4655d10 oops - odd was wrong
Claus Gittinger <cg@exept.de>
parents: 1893
diff changeset
  5677
    "Modified: 18.11.1996 / 22:19:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5678
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5679
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5680
positive
18855
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  5681
    "return true, if the receiver is greater or equal to zero (not negative)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5682
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5683
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5684
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5685
    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
  5686
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5687
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5688
    ^ (sign >= 0)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5689
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5690
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5691
sign
7471
c5d4bd612d9f comments
Claus Gittinger <cg@exept.de>
parents: 7446
diff changeset
  5692
    "return the sign of the receiver (-1, 0 or 1)"
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5693
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5694
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5695
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5696
    return context._RETURN( STInteger._new( ((STLargeInteger)self).largeValue.signum() ));
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5697
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5698
%}.
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5699
    ^ sign
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5700
!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5701
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5702
strictlyPositive
18855
85ed6541937d #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18848
diff changeset
  5703
    "return true, if the receiver is greater than zero"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5704
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5705
%{
18240
28af09029a8b ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents: 18235
diff changeset
  5706
#ifdef __SCHTEAM__
18235
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5707
    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
  5708
#endif
9f0914e019a8 experimental java support
Claus Gittinger <cg@exept.de>
parents: 17648
diff changeset
  5709
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5710
    ^ (sign > 0)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5711
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5712
1794
c8f7736d1c12 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1717
diff changeset
  5713
!LargeInteger class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5714
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  5715
version
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5716
    ^ '$Header$'
12121
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5717
!
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5718
38c8767f98f2 changed: #bitAnd:
Claus Gittinger <cg@exept.de>
parents: 11959
diff changeset
  5719
version_CVS
18621
78b4a4b916a5 schteam change
Claus Gittinger <cg@exept.de>
parents: 18381
diff changeset
  5720
    ^ '$Header$'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  5721
! !
18845
ee0ed4f54c44 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18709
diff changeset
  5722