1
- # # type aliases ##
2
-
3
- if Int === Int32
4
- typealias SmallSigned Union (Int8,Int16)
5
- typealias SmallUnsigned Union (Uint8,Uint16)
6
- else
7
- typealias SmallSigned Union (Int8,Int16,Int32)
8
- typealias SmallUnsigned Union (Uint8,Uint16,Uint32)
9
- end
10
-
11
1
# # integer arithmetic ##
12
2
13
3
const IntTypes = (Int8, Uint8, Int16, Uint16, Int32, Uint32,
@@ -34,11 +24,10 @@ iseven(n::Integer) = !isodd(n)
34
24
signbit (x:: Integer ) = x < 0
35
25
signbit (x:: Unsigned ) = false
36
26
37
- flipsign (x :: Int , y :: Int ) = box (Int, flipsign_int ( unbox (Int,x), unbox (Int,y)) )
38
- flipsign (x:: Int64 , y:: Int64 ) = box (Int64 ,flipsign_int (unbox (Int64 ,x),unbox (Int64 ,y)))
39
- flipsign (x :: Int128 , y :: Int128 ) = box (Int128, flipsign_int ( unbox (Int128,x), unbox (Int128,y)))
27
+ for T in (Int8,Int16,Int32,Int64,Int128 )
28
+ @eval flipsign (x:: $T , y:: $T ) = box ($ T ,flipsign_int (unbox ($ T ,x),unbox ($ T ,y)))
29
+ end
40
30
41
- flipsign {T<:Signed} (x:: T ,y:: T ) = flipsign (int (x),int (y))
42
31
flipsign (x:: Signed , y:: Signed ) = flipsign (promote (x,y)... )
43
32
flipsign (x:: Signed , y:: Float32 ) = flipsign (x, reinterpret (Int32,y))
44
33
flipsign (x:: Signed , y:: Float64 ) = flipsign (x, reinterpret (Int64,y))
@@ -100,9 +89,13 @@ for T in IntTypes
100
89
($ )(x:: $T , y:: $T ) = box ($ T,xor_int (unbox ($ T,x),unbox ($ T,y)))
101
90
102
91
<< (x:: $T , y:: Int32 ) = box ($ T, shl_int (unbox ($ T,x),unbox (Int32,y)))
103
- >> (x:: $T , y:: Int32 ) = box ($ T,ashr_int (unbox ($ T,x),unbox (Int32,y)))
104
92
>>> (x:: $T , y:: Int32 ) = box ($ T,lshr_int (unbox ($ T,x),unbox (Int32,y)))
105
93
end
94
+ if issubtype (T,Unsigned)
95
+ @eval >> (x:: $T , y:: Int32 ) = box ($ T,lshr_int (unbox ($ T,x),unbox (Int32,y)))
96
+ else
97
+ @eval >> (x:: $T , y:: Int32 ) = box ($ T,ashr_int (unbox ($ T,x),unbox (Int32,y)))
98
+ end
106
99
end
107
100
108
101
bswap (x:: Int8 ) = x
@@ -129,29 +122,15 @@ trailing_ones(x::Integer) = trailing_zeros(~x)
129
122
130
123
# # integer comparisons ##
131
124
132
- < (x:: Int8 , y:: Int8 ) = slt_int (unbox (Int8,x),unbox (Int8,y))
133
- < (x:: Int16 , y:: Int16 ) = slt_int (unbox (Int16,x),unbox (Int16,y))
134
- < (x:: Int32 , y:: Int32 ) = slt_int (unbox (Int32,x),unbox (Int32,y))
135
- < (x:: Int64 , y:: Int64 ) = slt_int (unbox (Int64,x),unbox (Int64,y))
136
- < (x:: Int128 , y:: Int128 ) = slt_int (unbox (Int128,x),unbox (Int128,y))
137
-
138
- < (x:: Uint8 , y:: Uint8 ) = ult_int (unbox (Uint8,x),unbox (Uint8,y))
139
- < (x:: Uint16 , y:: Uint16 ) = ult_int (unbox (Uint16,x),unbox (Uint16,y))
140
- < (x:: Uint32 , y:: Uint32 ) = ult_int (unbox (Uint32,x),unbox (Uint32,y))
141
- < (x:: Uint64 , y:: Uint64 ) = ult_int (unbox (Uint64,x),unbox (Uint64,y))
142
- < (x:: Uint128 , y:: Uint128 ) = ult_int (unbox (Uint128,x),unbox (Uint128,y))
143
-
144
- <= (x:: Int8 , y:: Int8 ) = sle_int (unbox (Int8,x),unbox (Int8,y))
145
- <= (x:: Int16 , y:: Int16 ) = sle_int (unbox (Int16,x),unbox (Int16,y))
146
- <= (x:: Int32 , y:: Int32 ) = sle_int (unbox (Int32,x),unbox (Int32,y))
147
- <= (x:: Int64 , y:: Int64 ) = sle_int (unbox (Int64,x),unbox (Int64,y))
148
- <= (x:: Int128 , y:: Int128 ) = sle_int (unbox (Int128,x),unbox (Int128,y))
149
-
150
- <= (x:: Uint8 , y:: Uint8 ) = ule_int (unbox (Uint8,x),unbox (Uint8,y))
151
- <= (x:: Uint16 , y:: Uint16 ) = ule_int (unbox (Uint16,x),unbox (Uint16,y))
152
- <= (x:: Uint32 , y:: Uint32 ) = ule_int (unbox (Uint32,x),unbox (Uint32,y))
153
- <= (x:: Uint64 , y:: Uint64 ) = ule_int (unbox (Uint64,x),unbox (Uint64,y))
154
- <= (x:: Uint128 , y:: Uint128 ) = ule_int (unbox (Uint128,x),unbox (Uint128,y))
125
+ for T in IntTypes
126
+ if issubtype (T,Signed)
127
+ @eval < ( x:: $T , y:: $T ) = slt_int (unbox ($ T,x),unbox ($ T,y))
128
+ @eval <= (x:: $T , y:: $T ) = sle_int (unbox ($ T,x),unbox ($ T,y))
129
+ else
130
+ @eval < ( x:: $T , y:: $T ) = ult_int (unbox ($ T,x),unbox ($ T,y))
131
+ @eval <= (x:: $T , y:: $T ) = ule_int (unbox ($ T,x),unbox ($ T,y))
132
+ end
133
+ end
155
134
156
135
== (x:: Signed , y:: Unsigned ) = (x >= 0 ) & (unsigned (x) == y)
157
136
== (x:: Unsigned , y:: Signed ) = (y >= 0 ) & (x == unsigned (y))
@@ -162,11 +141,8 @@ trailing_ones(x::Integer) = trailing_zeros(~x)
162
141
163
142
# # integer conversions ##
164
143
165
- const _inttypes = (Bool, Int8, Uint8, Int16, Uint16, Int32, Uint32, Char,
166
- Int64, Uint64, Int128, Uint128)
167
-
168
- for to in _inttypes, from in _inttypes
169
- if ! (to === from) && ! (to === Bool)
144
+ for to in tuple (IntTypes... ,Char), from in tuple (IntTypes... ,Char,Bool)
145
+ if ! (to === from)
170
146
if to. size < from. size
171
147
if issubtype (to, Signed)
172
148
@eval convert (:: Type{$to} , x:: ($from) ) = box ($ to,checked_trunc_sint ($ to,unbox ($ from,x)))
@@ -218,21 +194,18 @@ function convert(::Type{Uint128}, x::FloatingPoint)
218
194
end
219
195
convert (:: Type{Uint128} , x:: Float32 ) = convert (Uint128, float64 (x))
220
196
221
- convert (:: Type{Char} , x:: Float32 ) = char (convert (Int, x))
222
- convert (:: Type{Char} , x:: Float64 ) = char (convert (Int, x))
223
-
224
- convert (:: Type{Signed} , x:: Uint8 ) = convert (Int,x)
225
- convert (:: Type{Signed} , x:: Uint16 ) = convert (Int,x)
226
- convert (:: Type{Signed} , x:: Uint32 ) = convert (Int,x)
197
+ convert (:: Type{Signed} , x:: Uint8 ) = convert (Int8,x)
198
+ convert (:: Type{Signed} , x:: Uint16 ) = convert (Int16,x)
199
+ convert (:: Type{Signed} , x:: Uint32 ) = convert (Int32,x)
227
200
convert (:: Type{Signed} , x:: Uint64 ) = convert (Int64,x)
228
201
convert (:: Type{Signed} , x:: Uint128 ) = convert (Int128,x)
229
202
convert (:: Type{Signed} , x:: Float32 ) = convert (Int,x)
230
203
convert (:: Type{Signed} , x:: Float64 ) = convert (Int,x)
231
204
convert (:: Type{Signed} , x:: Char ) = convert (Int,x)
232
205
233
- convert (:: Type{Unsigned} , x:: Int8 ) = convert (Uint ,x)
234
- convert (:: Type{Unsigned} , x:: Int16 ) = convert (Uint ,x)
235
- convert (:: Type{Unsigned} , x:: Int32 ) = convert (Uint ,x)
206
+ convert (:: Type{Unsigned} , x:: Int8 ) = convert (Uint8 ,x)
207
+ convert (:: Type{Unsigned} , x:: Int16 ) = convert (Uint16 ,x)
208
+ convert (:: Type{Unsigned} , x:: Int32 ) = convert (Uint32 ,x)
236
209
convert (:: Type{Unsigned} , x:: Int64 ) = convert (Uint64,x)
237
210
convert (:: Type{Unsigned} , x:: Int128 ) = convert (Uint128,x)
238
211
convert (:: Type{Unsigned} , x:: Float32 ) = convert (Uint,x)
@@ -372,12 +345,12 @@ typemax(::Type{Uint64}) = 0xffffffffffffffff
372
345
typemax (:: Type{Int128} ) = $ (int128 ((uint128 (- 1 ))>> int32 (1 )))
373
346
end
374
347
375
- widen (:: Type{Int8} ) = Int
376
- widen (:: Type{Int16} ) = Int
348
+ widen (:: Type{Int8} ) = Int16
349
+ widen (:: Type{Int16} ) = Int32
377
350
widen (:: Type{Int32} ) = Int64
378
351
widen (:: Type{Int64} ) = Int128
379
- widen (:: Type{Uint8} ) = Uint
380
- widen (:: Type{Uint16} ) = Uint
352
+ widen (:: Type{Uint8} ) = Uint16
353
+ widen (:: Type{Uint16} ) = Uint32
381
354
widen (:: Type{Uint32} ) = Uint64
382
355
widen (:: Type{Uint64} ) = Uint128
383
356
@@ -506,20 +479,18 @@ end
506
479
507
480
for T in (Int8,Uint8)
508
481
@eval function checked_mul (x:: $T , y:: $T )
509
- xy = x* y
510
- xy8 = convert ($ T,xy)
511
- xy == xy8 || throw (OverflowError ())
512
- return xy8
482
+ xy = widemul (x,y)
483
+ (typemin ($ T) <= xy <= typemax ($ T)) || throw (OverflowError ())
484
+ return itrunc ($ T,xy)
513
485
end
514
486
end
515
487
516
488
if WORD_SIZE == 32
517
489
for T in (Int64,Uint64)
518
490
@eval function checked_mul (x:: $T , y:: $T )
519
491
xy = int128 (x)* int128 (y)
520
- xy64 = convert ($ T,xy)
521
- xy == xy64 || throw (OverflowError ())
522
- return xy64
492
+ (typemin ($ T) <= xy <= typemax ($ T)) || throw (OverflowError ())
493
+ return itrunc ($ T,xy)
523
494
end
524
495
end
525
496
else
0 commit comments