primops.txt.pp 110 KB
Newer Older
1
-----------------------------------------------------------------------
Jan Stolarek's avatar
Jan Stolarek committed
2
--
3
-- (c) 2010 The University of Glasgow
4
--
5
-- Primitive Operations and Types
6
--
7
-- For more information on PrimOps, see
8
--   http://ghc.haskell.org/trac/ghc/wiki/Commentary/PrimOps
9
--
10 11
-----------------------------------------------------------------------

apt's avatar
apt committed
12 13 14 15 16 17
-- This file is processed by the utility program genprimopcode to produce
-- a number of include files within the compiler and optionally to produce
-- human-readable documentation.
--
-- It should first be preprocessed.
--
18 19
-- Information on how PrimOps are implemented and the steps necessary to
-- add a new one can be found in the Commentary:
apt's avatar
apt committed
20
--
21
--  http://ghc.haskell.org/trac/ghc/wiki/Commentary/PrimOps
apt's avatar
apt committed
22

23 24
-- This file is divided into named sections, each containing or more
-- primop entries. Section headers have the format:
apt's avatar
apt committed
25
--
26
--      section "section-name" {description}
apt's avatar
apt committed
27
--
28 29
-- This information is used solely when producing documentation; it is
-- otherwise ignored.  The description is optional.
apt's avatar
apt committed
30 31
--
-- The format of each primop entry is as follows:
32
--
33
--      primop internal-name "name-in-program-text" type category {description} attributes
34

apt's avatar
apt committed
35
-- The default attribute values which apply if you don't specify
36
-- other ones.  Attribute values can be True, False, or arbitrary
Jan Stolarek's avatar
Jan Stolarek committed
37
-- text between curly brackets.  This is a kludge to enable
38 39
-- processors of this file to easily get hold of simple info
-- (eg, out_of_line), whilst avoiding parsing complex expressions
40
-- needed for strictness info.
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
-- The vector attribute is rather special. It takes a list of 3-tuples, each of
-- which is of the form <ELEM_TYPE,SCALAR_TYPE,LENGTH>. ELEM_TYPE is the type of
-- the elements in the vector; LENGTH is the length of the vector; and
-- SCALAR_TYPE is the scalar type used to inject to/project from vector
-- element. Note that ELEM_TYPE and SCALAR_TYPE are not the same; for example,
-- to broadcast a scalar value to a vector whose elements are of type Int8, we
-- use an Int#.

-- When a primtype or primop has a vector attribute, it is instantiated at each
-- 3-tuple in the list of 3-tuples. That is, the vector attribute allows us to
-- define a family of types or primops. Vector support also adds three new
-- keywords: VECTOR, SCALAR, and VECTUPLE. These keywords are expanded to types
-- derived from the 3-tuple. For the 3-tuple <Int64,INT64,2>, VECTOR expands to
-- Int64X2#, SCALAR expands to INT64, and VECTUPLE expands to (# INT64, INT64
-- #).

58 59
defaults
   has_side_effects = False
60 61
   out_of_line      = False   -- See Note Note [PrimOp can_fail and has_side_effects] in PrimOp
   can_fail         = False   -- See Note Note [PrimOp can_fail and has_side_effects] in PrimOp
62
   commutable       = False
63
   code_size        = { primOpCodeSizeDefault }
64
   strictness       = { \ arity -> mkClosedStrictSig (replicate arity topDmd) topRes }
65
   fixity           = Nothing
gmainland's avatar
gmainland committed
66
   llvm_only        = False
67
   vector           = []
68

69 70 71
-- Currently, documentation is produced using latex, so contents of
-- description fields should be legal latex. Descriptions can contain
-- matched pairs of embedded curly brackets.
apt's avatar
apt committed
72 73

#include "MachDeps.h"
74

75 76
-- We need platform defines (tests for mingw32 below).
#include "ghc_boot_platform.h"
apt's avatar
apt committed
77 78

section "The word size story."
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        {Haskell98 specifies that signed integers (type {\tt Int})
         must contain at least 30 bits. GHC always implements {\tt
         Int} using the primitive type {\tt Int\#}, whose size equals
         the {\tt MachDeps.h} constant {\tt WORD\_SIZE\_IN\_BITS}.
         This is normally set based on the {\tt config.h} parameter
         {\tt SIZEOF\_HSWORD}, i.e., 32 bits on 32-bit machines, 64
         bits on 64-bit machines.  However, it can also be explicitly
         set to a smaller number, e.g., 31 bits, to allow the
         possibility of using tag bits. Currently GHC itself has only
         32-bit and 64-bit variants, but 30 or 31-bit code can be
         exported as an external core file for use in other back ends.

         GHC also implements a primitive unsigned integer type {\tt
         Word\#} which always has the same number of bits as {\tt
         Int\#}.

         In addition, GHC supports families of explicit-sized integers
         and words at 8, 16, 32, and 64 bits, with the usual
         arithmetic operations, comparisons, and a range of
         conversions.  The 8-bit and 16-bit sizes are always
         represented as {\tt Int\#} and {\tt Word\#}, and the
         operations implemented in terms of the the primops on these
         types, with suitable range restrictions on the results (using
         the {\tt narrow$n$Int\#} and {\tt narrow$n$Word\#} families
         of primops.  The 32-bit sizes are represented using {\tt
         Int\#} and {\tt Word\#} when {\tt WORD\_SIZE\_IN\_BITS}
         $\geq$ 32; otherwise, these are represented using distinct
         primitive types {\tt Int32\#} and {\tt Word32\#}. These (when
         needed) have a complete set of corresponding operations;
         however, nearly all of these are implemented as external C
         functions rather than as primops.  Exactly the same story
         applies to the 64-bit sizes.  All of these details are hidden
         under the {\tt PrelInt} and {\tt PrelWord} modules, which use
         {\tt \#if}-defs to invoke the appropriate types and
         operators.

         Word size also matters for the families of primops for
         indexing/reading/writing fixed-size quantities at offsets
         from an array base, address, or foreign pointer.  Here, a
         slightly different approach is taken.  The names of these
         primops are fixed, but their {\it types} vary according to
         the value of {\tt WORD\_SIZE\_IN\_BITS}. For example, if word
         size is at least 32 bits then an operator like
         \texttt{indexInt32Array\#} has type {\tt ByteArray\# -> Int\#
         -> Int\#}; otherwise it has type {\tt ByteArray\# -> Int\# ->
         Int32\#}.  This approach confines the necessary {\tt
         \#if}-defs to this file; no conditional compilation is needed
         in the files that expose these primops.

         Finally, there are strongly deprecated primops for coercing
129 130 131 132 133
         between {\tt Addr\#}, the primitive type of machine
         addresses, and {\tt Int\#}.  These are pretty bogus anyway,
         but will work on existing 32-bit and 64-bit GHC targets; they
         are completely bogus when tag bits are used in {\tt Int\#},
         so are not available in this case.  }
apt's avatar
apt committed
134

Jan Stolarek's avatar
Jan Stolarek committed
135 136 137
-- Define synonyms for indexing ops.

#if WORD_SIZE_IN_BITS < 32
apt's avatar
apt committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
#define INT32 Int32#
#define WORD32 Word32#
#else
#define INT32 Int#
#define WORD32 Word#
#endif

#if WORD_SIZE_IN_BITS < 64
#define INT64 Int64#
#define WORD64 Word64#
#else
#define INT64 Int#
#define WORD64 Word#
#endif
152 153

------------------------------------------------------------------------
154
section "Char#"
155
        {Operations on 31-bit characters.}
156 157
------------------------------------------------------------------------

158
primtype Char#
159

160 161
primop   CharGtOp  "gtChar#"   Compare   Char# -> Char# -> Int#
primop   CharGeOp  "geChar#"   Compare   Char# -> Char# -> Int#
162

163
primop   CharEqOp  "eqChar#"   Compare
164
   Char# -> Char# -> Int#
apt's avatar
apt committed
165
   with commutable = True
166

167
primop   CharNeOp  "neChar#"   Compare
168
   Char# -> Char# -> Int#
apt's avatar
apt committed
169 170
   with commutable = True

171 172
primop   CharLtOp  "ltChar#"   Compare   Char# -> Char# -> Int#
primop   CharLeOp  "leChar#"   Compare   Char# -> Char# -> Int#
apt's avatar
apt committed
173 174

primop   OrdOp   "ord#"  GenPrimOp   Char# -> Int#
175
   with code_size = 0
176

177
------------------------------------------------------------------------
apt's avatar
apt committed
178
section "Int#"
179
        {Operations on native-size integers (30+ bits).}
180 181
------------------------------------------------------------------------

182 183
primtype Int#

apt's avatar
apt committed
184 185 186
primop   IntAddOp    "+#"    Dyadic
   Int# -> Int# -> Int#
   with commutable = True
187
        fixity = infixl 6
188

apt's avatar
apt committed
189
primop   IntSubOp    "-#"    Dyadic   Int# -> Int# -> Int#
190
   with fixity = infixl 6
191

Jan Stolarek's avatar
Jan Stolarek committed
192
primop   IntMulOp    "*#"
apt's avatar
apt committed
193
   Dyadic   Int# -> Int# -> Int#
194 195
   {Low word of signed integer multiply.}
   with commutable = True
196
        fixity = infixl 7
197

Jan Stolarek's avatar
Jan Stolarek committed
198
primop   IntMulMayOfloOp  "mulIntMayOflo#"
199 200
   Dyadic   Int# -> Int# -> Int#
   {Return non-zero if there is any possibility that the upper word of a
201
    signed integer multiply might contain useful information.  Return
202
    zero only if you are completely sure that no overflow can occur.
Jan Stolarek's avatar
Jan Stolarek committed
203
    On a 32-bit platform, the recommmended implementation is to do a
204
    32 x 32 -> 64 signed multiply, and subtract result[63:32] from
Jan Stolarek's avatar
Jan Stolarek committed
205
    (result[31] >>signed 31).  If this is zero, meaning that the
206 207
    upper word is merely a sign extension of the lower one, no
    overflow can occur.
208

Jan Stolarek's avatar
Jan Stolarek committed
209 210 211 212 213
    On a 64-bit platform it is not always possible to
    acquire the top 64 bits of the result.  Therefore, a recommended
    implementation is to take the absolute value of both operands, and
    return 0 iff bits[63:31] of them are zero, since that means that their
    magnitudes fit within 31 bits, so the magnitude of the product must fit
214 215 216 217
    into 62 bits.

    If in doubt, return non-zero, but do make an effort to create the
    correct answer for small args, since otherwise the performance of
218
    \texttt{(*) :: Integer -> Integer -> Integer} will be poor.
219
   }
apt's avatar
apt committed
220 221 222 223 224 225 226 227 228 229 230 231
   with commutable = True

primop   IntQuotOp    "quotInt#"    Dyadic
   Int# -> Int# -> Int#
   {Rounds towards zero.}
   with can_fail = True

primop   IntRemOp    "remInt#"    Dyadic
   Int# -> Int# -> Int#
   {Satisfies \texttt{(quotInt\# x y) *\# y +\# (remInt\# x y) == x}.}
   with can_fail = True

232 233 234 235 236
primop   IntQuotRemOp "quotRemInt#"    GenPrimOp
   Int# -> Int# -> (# Int#, Int# #)
   {Rounds towards zero.}
   with can_fail = True

237 238 239 240 241 242 243 244 245 246 247
primop   AndIOp   "andI#"   Dyadic    Int# -> Int# -> Int#
   with commutable = True

primop   OrIOp   "orI#"     Dyadic    Int# -> Int# -> Int#
   with commutable = True

primop   XorIOp   "xorI#"   Dyadic    Int# -> Int# -> Int#
   with commutable = True

primop   NotIOp   "notI#"   Monadic   Int# -> Int#

apt's avatar
apt committed
248 249
primop   IntNegOp    "negateInt#"    Monadic   Int# -> Int#
primop   IntAddCOp   "addIntC#"    GenPrimOp   Int# -> Int# -> (# Int#, Int# #)
250 251 252 253 254
         {Add signed integers reporting overflow.
          First member of result is the sum truncated to an {\tt Int#};
          second member is zero if the true sum fits in an {\tt Int#},
          nonzero if overflow occurred (the sum is either too large
          or too small to fit in an {\tt Int#}).}
255 256
   with code_size = 2

apt's avatar
apt committed
257
primop   IntSubCOp   "subIntC#"    GenPrimOp   Int# -> Int# -> (# Int#, Int# #)
258 259 260 261 262
         {Subtract signed integers reporting overflow.
          First member of result is the difference truncated to an {\tt Int#};
          second member is zero if the true difference fits in an {\tt Int#},
          nonzero if overflow occurred (the difference is either too large
          or too small to fit in an {\tt Int#}).}
263
   with code_size = 2
264

265
primop   IntGtOp  ">#"   Compare   Int# -> Int# -> Int#
266 267
   with fixity = infix 4

268
primop   IntGeOp  ">=#"   Compare   Int# -> Int# -> Int#
269
   with fixity = infix 4
apt's avatar
apt committed
270

271
primop   IntEqOp  "==#"   Compare
272
   Int# -> Int# -> Int#
apt's avatar
apt committed
273
   with commutable = True
274
        fixity = infix 4
apt's avatar
apt committed
275

276
primop   IntNeOp  "/=#"   Compare
277
   Int# -> Int# -> Int#
apt's avatar
apt committed
278
   with commutable = True
279
        fixity = infix 4
apt's avatar
apt committed
280

281
primop   IntLtOp  "<#"   Compare   Int# -> Int# -> Int#
282 283
   with fixity = infix 4

284
primop   IntLeOp  "<=#"   Compare   Int# -> Int# -> Int#
285
   with fixity = infix 4
apt's avatar
apt committed
286 287

primop   ChrOp   "chr#"   GenPrimOp   Int# -> Char#
288
   with code_size = 0
apt's avatar
apt committed
289 290

primop   Int2WordOp "int2Word#" GenPrimOp Int# -> Word#
291 292
   with code_size = 0

apt's avatar
apt committed
293 294 295
primop   Int2FloatOp   "int2Float#"      GenPrimOp  Int# -> Float#
primop   Int2DoubleOp   "int2Double#"          GenPrimOp  Int# -> Double#

tibbe's avatar
tibbe committed
296 297 298
primop   Word2FloatOp   "word2Float#"      GenPrimOp  Word# -> Float#
primop   Word2DoubleOp   "word2Double#"          GenPrimOp  Word# -> Double#

299
primop   ISllOp   "uncheckedIShiftL#" GenPrimOp  Int# -> Int# -> Int#
300
         {Shift left.  Result undefined if shift amount is not
301
          in the range 0 to word size - 1 inclusive.}
302
primop   ISraOp   "uncheckedIShiftRA#" GenPrimOp Int# -> Int# -> Int#
303
         {Shift right arithmetic.  Result undefined if shift amount is not
304
          in the range 0 to word size - 1 inclusive.}
305
primop   ISrlOp   "uncheckedIShiftRL#" GenPrimOp Int# -> Int# -> Int#
306
         {Shift right logical.  Result undefined if shift amount is not
307
          in the range 0 to word size - 1 inclusive.}
308 309

------------------------------------------------------------------------
apt's avatar
apt committed
310
section "Word#"
311
        {Operations on native-sized unsigned words (30+ bits).}
312 313
------------------------------------------------------------------------

314 315
primtype Word#

apt's avatar
apt committed
316 317
primop   WordAddOp   "plusWord#"   Dyadic   Word# -> Word# -> Word#
   with commutable = True
318

Ian Lynagh's avatar
Ian Lynagh committed
319
-- Returns (# high, low #) (or equivalently, (# carry, low #))
Ian Lynagh's avatar
Ian Lynagh committed
320 321 322 323
primop   WordAdd2Op  "plusWord2#"  GenPrimOp
   Word# -> Word# -> (# Word#, Word# #)
   with commutable = True

apt's avatar
apt committed
324 325 326
primop   WordSubOp   "minusWord#"   Dyadic   Word# -> Word# -> Word#

primop   WordMulOp   "timesWord#"   Dyadic   Word# -> Word# -> Word#
327 328
   with commutable = True

Ian Lynagh's avatar
Ian Lynagh committed
329
-- Returns (# high, low #)
Ian Lynagh's avatar
Ian Lynagh committed
330 331 332 333
primop   WordMul2Op  "timesWord2#"   GenPrimOp
   Word# -> Word# -> (# Word#, Word# #)
   with commutable = True

apt's avatar
apt committed
334 335 336 337 338 339
primop   WordQuotOp   "quotWord#"   Dyadic   Word# -> Word# -> Word#
   with can_fail = True

primop   WordRemOp   "remWord#"   Dyadic   Word# -> Word# -> Word#
   with can_fail = True

340 341 342 343
primop   WordQuotRemOp "quotRemWord#" GenPrimOp
   Word# -> Word# -> (# Word#, Word# #)
   with can_fail = True

Ian Lynagh's avatar
Ian Lynagh committed
344 345 346 347 348 349
-- Takes high word of dividend, then low word of dividend, then divisor.
-- Requires that high word is not divisible by divisor.
primop   WordQuotRem2Op "quotRemWord2#" GenPrimOp
   Word# -> Word# -> Word# -> (# Word#, Word# #)
   with can_fail = True

apt's avatar
apt committed
350
primop   AndOp   "and#"   Dyadic   Word# -> Word# -> Word#
351 352
   with commutable = True

apt's avatar
apt committed
353 354 355 356 357 358 359 360
primop   OrOp   "or#"   Dyadic   Word# -> Word# -> Word#
   with commutable = True

primop   XorOp   "xor#"   Dyadic   Word# -> Word# -> Word#
   with commutable = True

primop   NotOp   "not#"   Monadic   Word# -> Word#

361
primop   SllOp   "uncheckedShiftL#"   GenPrimOp   Word# -> Int# -> Word#
362
         {Shift left logical.   Result undefined if shift amount is not
363
          in the range 0 to word size - 1 inclusive.}
364
primop   SrlOp   "uncheckedShiftRL#"   GenPrimOp   Word# -> Int# -> Word#
365
         {Shift right logical.   Result undefined if shift  amount is not
366
          in the range 0 to word size - 1 inclusive.}
apt's avatar
apt committed
367 368

primop   Word2IntOp   "word2Int#"   GenPrimOp   Word# -> Int#
369
   with code_size = 0
apt's avatar
apt committed
370

371 372 373 374 375 376
primop   WordGtOp   "gtWord#"   Compare   Word# -> Word# -> Int#
primop   WordGeOp   "geWord#"   Compare   Word# -> Word# -> Int#
primop   WordEqOp   "eqWord#"   Compare   Word# -> Word# -> Int#
primop   WordNeOp   "neWord#"   Compare   Word# -> Word# -> Int#
primop   WordLtOp   "ltWord#"   Compare   Word# -> Word# -> Int#
primop   WordLeOp   "leWord#"   Compare   Word# -> Word# -> Int#
apt's avatar
apt committed
377

tibbe's avatar
tibbe committed
378 379 380 381 382 383
primop   PopCnt8Op   "popCnt8#"   Monadic   Word# -> Word#
    {Count the number of set bits in the lower 8 bits of a word.}
primop   PopCnt16Op   "popCnt16#"   Monadic   Word# -> Word#
    {Count the number of set bits in the lower 16 bits of a word.}
primop   PopCnt32Op   "popCnt32#"   Monadic   Word# -> Word#
    {Count the number of set bits in the lower 32 bits of a word.}
Simon Marlow's avatar
Simon Marlow committed
384
primop   PopCnt64Op   "popCnt64#"   GenPrimOp   WORD64 -> Word#
tibbe's avatar
tibbe committed
385 386 387 388
    {Count the number of set bits in a 64-bit word.}
primop   PopCntOp   "popCnt#"   Monadic   Word# -> Word#
    {Count the number of set bits in a word.}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
primop   Clz8Op   "clz8#" Monadic   Word# -> Word#
    {Count leading zeros in the lower 8 bits of a word.}
primop   Clz16Op   "clz16#" Monadic   Word# -> Word#
    {Count leading zeros in the lower 16 bits of a word.}
primop   Clz32Op   "clz32#" Monadic   Word# -> Word#
    {Count leading zeros in the lower 32 bits of a word.}
primop   Clz64Op   "clz64#" GenPrimOp WORD64 -> Word#
    {Count leading zeros in a 64-bit word.}
primop   ClzOp     "clz#"   Monadic   Word# -> Word#
    {Count leading zeros in a word.}

primop   Ctz8Op   "ctz8#"  Monadic   Word# -> Word#
    {Count trailing zeros in the lower 8 bits of a word.}
primop   Ctz16Op   "ctz16#" Monadic   Word# -> Word#
    {Count trailing zeros in the lower 16 bits of a word.}
primop   Ctz32Op   "ctz32#" Monadic   Word# -> Word#
    {Count trailing zeros in the lower 32 bits of a word.}
primop   Ctz64Op   "ctz64#" GenPrimOp WORD64 -> Word#
    {Count trailing zeros in a 64-bit word.}
primop   CtzOp     "ctz#"   Monadic   Word# -> Word#
    {Count trailing zeros in a word.}

411 412 413 414 415 416 417 418 419
primop   BSwap16Op   "byteSwap16#"   Monadic   Word# -> Word#
    {Swap bytes in the lower 16 bits of a word. The higher bytes are undefined. }
primop   BSwap32Op   "byteSwap32#"   Monadic   Word# -> Word#
    {Swap bytes in the lower 32 bits of a word. The higher bytes are undefined. }
primop   BSwap64Op   "byteSwap64#"   Monadic   WORD64 -> WORD64
    {Swap bytes in a 64 bits of a word.}
primop   BSwapOp     "byteSwap#"     Monadic   Word# -> Word#
    {Swap bytes in a word.}

apt's avatar
apt committed
420
------------------------------------------------------------------------
Jan Stolarek's avatar
Jan Stolarek committed
421
section "Narrowings"
422
        {Explicit narrowing of native-sized ints or words.}
apt's avatar
apt committed
423 424 425 426 427 428 429 430 431 432 433 434 435
------------------------------------------------------------------------

primop   Narrow8IntOp      "narrow8Int#"      Monadic   Int# -> Int#
primop   Narrow16IntOp     "narrow16Int#"     Monadic   Int# -> Int#
primop   Narrow32IntOp     "narrow32Int#"     Monadic   Int# -> Int#
primop   Narrow8WordOp     "narrow8Word#"     Monadic   Word# -> Word#
primop   Narrow16WordOp    "narrow16Word#"    Monadic   Word# -> Word#
primop   Narrow32WordOp    "narrow32Word#"    Monadic   Word# -> Word#


#if WORD_SIZE_IN_BITS < 32
------------------------------------------------------------------------
section "Int32#"
436
        {Operations on 32-bit integers ({\tt Int32\#}).  This type is only used
437
         if plain {\tt Int\#} has less than 32 bits.  In any case, the operations
438
         are not primops; they are implemented (if needed) as ccalls instead.}
apt's avatar
apt committed
439 440
------------------------------------------------------------------------

441 442
primtype Int32#

apt's avatar
apt committed
443 444
------------------------------------------------------------------------
section "Word32#"
445 446 447
        {Operations on 32-bit unsigned words. This type is only used
         if plain {\tt Word\#} has less than 32 bits. In any case, the operations
         are not primops; they are implemented (if needed) as ccalls instead.}
apt's avatar
apt committed
448 449
------------------------------------------------------------------------

450 451
primtype Word32#

Jan Stolarek's avatar
Jan Stolarek committed
452
#endif
apt's avatar
apt committed
453 454 455 456 457


#if WORD_SIZE_IN_BITS < 64
------------------------------------------------------------------------
section "Int64#"
458 459 460
        {Operations on 64-bit unsigned words. This type is only used
         if plain {\tt Int\#} has less than 64 bits. In any case, the operations
         are not primops; they are implemented (if needed) as ccalls instead.}
apt's avatar
apt committed
461 462
------------------------------------------------------------------------

463 464
primtype Int64#

apt's avatar
apt committed
465 466
------------------------------------------------------------------------
section "Word64#"
467 468 469
        {Operations on 64-bit unsigned words. This type is only used
         if plain {\tt Word\#} has less than 64 bits. In any case, the operations
         are not primops; they are implemented (if needed) as ccalls instead.}
apt's avatar
apt committed
470 471
------------------------------------------------------------------------

472 473
primtype Word64#

apt's avatar
apt committed
474 475
#endif

476
------------------------------------------------------------------------
apt's avatar
apt committed
477
section "Double#"
478
        {Operations on double-precision (64 bit) floating-point numbers.}
479 480
------------------------------------------------------------------------

481 482
primtype Double#

483
primop   DoubleGtOp ">##"   Compare   Double# -> Double# -> Int#
484 485
   with fixity = infix 4

486
primop   DoubleGeOp ">=##"   Compare   Double# -> Double# -> Int#
487
   with fixity = infix 4
488

489
primop DoubleEqOp "==##"   Compare
490
   Double# -> Double# -> Int#
491
   with commutable = True
492
        fixity = infix 4
493

494
primop DoubleNeOp "/=##"   Compare
495
   Double# -> Double# -> Int#
496
   with commutable = True
497
        fixity = infix 4
498

499
primop   DoubleLtOp "<##"   Compare   Double# -> Double# -> Int#
500 501
   with fixity = infix 4

502
primop   DoubleLeOp "<=##"   Compare   Double# -> Double# -> Int#
503
   with fixity = infix 4
504 505 506 507

primop   DoubleAddOp   "+##"   Dyadic
   Double# -> Double# -> Double#
   with commutable = True
508
        fixity = infixl 6
509 510

primop   DoubleSubOp   "-##"   Dyadic   Double# -> Double# -> Double#
511
   with fixity = infixl 6
512 513 514 515

primop   DoubleMulOp   "*##"   Dyadic
   Double# -> Double# -> Double#
   with commutable = True
516
        fixity = infixl 7
517 518 519 520

primop   DoubleDivOp   "/##"   Dyadic
   Double# -> Double# -> Double#
   with can_fail = True
521
        fixity = infixl 7
522 523 524 525

primop   DoubleNegOp   "negateDouble#"  Monadic   Double# -> Double#

primop   Double2IntOp   "double2Int#"          GenPrimOp  Double# -> Int#
526 527 528 529
   {Truncates a {\tt Double#} value to the nearest {\tt Int#}.
    Results are undefined if the truncation if truncation yields
    a value outside the range of {\tt Int#}.}

530 531 532 533
primop   Double2FloatOp   "double2Float#" GenPrimOp Double# -> Float#

primop   DoubleExpOp   "expDouble#"      Monadic
   Double# -> Double#
534 535
   with
   code_size = { primOpCodeSizeForeignCall }
536

Jan Stolarek's avatar
Jan Stolarek committed
537
primop   DoubleLogOp   "logDouble#"      Monadic
538 539
   Double# -> Double#
   with
540
   code_size = { primOpCodeSizeForeignCall }
541 542
   can_fail = True

Jan Stolarek's avatar
Jan Stolarek committed
543
primop   DoubleSqrtOp   "sqrtDouble#"      Monadic
544
   Double# -> Double#
545 546
   with
   code_size = { primOpCodeSizeForeignCall }
547

Jan Stolarek's avatar
Jan Stolarek committed
548
primop   DoubleSinOp   "sinDouble#"      Monadic
549
   Double# -> Double#
550 551
   with
   code_size = { primOpCodeSizeForeignCall }
552

Jan Stolarek's avatar
Jan Stolarek committed
553
primop   DoubleCosOp   "cosDouble#"      Monadic
554
   Double# -> Double#
555 556
   with
   code_size = { primOpCodeSizeForeignCall }
557

Jan Stolarek's avatar
Jan Stolarek committed
558
primop   DoubleTanOp   "tanDouble#"      Monadic
559
   Double# -> Double#
560 561
   with
   code_size = { primOpCodeSizeForeignCall }
562

Jan Stolarek's avatar
Jan Stolarek committed
563
primop   DoubleAsinOp   "asinDouble#"      Monadic
564 565
   Double# -> Double#
   with
566
   code_size = { primOpCodeSizeForeignCall }
567 568
   can_fail = True

Jan Stolarek's avatar
Jan Stolarek committed
569
primop   DoubleAcosOp   "acosDouble#"      Monadic
570 571
   Double# -> Double#
   with
572
   code_size = { primOpCodeSizeForeignCall }
573 574
   can_fail = True

Jan Stolarek's avatar
Jan Stolarek committed
575
primop   DoubleAtanOp   "atanDouble#"      Monadic
576 577
   Double# -> Double#
   with
578
   code_size = { primOpCodeSizeForeignCall }
579

Jan Stolarek's avatar
Jan Stolarek committed
580
primop   DoubleSinhOp   "sinhDouble#"      Monadic
581
   Double# -> Double#
582 583
   with
   code_size = { primOpCodeSizeForeignCall }
584

Jan Stolarek's avatar
Jan Stolarek committed
585
primop   DoubleCoshOp   "coshDouble#"      Monadic
586
   Double# -> Double#
587 588
   with
   code_size = { primOpCodeSizeForeignCall }
589

Jan Stolarek's avatar
Jan Stolarek committed
590
primop   DoubleTanhOp   "tanhDouble#"      Monadic
591
   Double# -> Double#
592 593
   with
   code_size = { primOpCodeSizeForeignCall }
594

Jan Stolarek's avatar
Jan Stolarek committed
595
primop   DoublePowerOp   "**##" Dyadic
596
   Double# -> Double# -> Double#
apt's avatar
apt committed
597
   {Exponentiation.}
598 599
   with
   code_size = { primOpCodeSizeForeignCall }
600

Jan Stolarek's avatar
Jan Stolarek committed
601
primop   DoubleDecode_2IntOp   "decodeDouble_2Int#" GenPrimOp
602
   Double# -> (# Int#, Word#, Word#, Int# #)
603
   {Convert to integer.
604 605 606
    First component of the result is -1 or 1, indicating the sign of the
    mantissa. The next two are the high and low 32 bits of the mantissa
    respectively, and the last is the exponent.}
607 608
   with out_of_line = True

609 610 611 612 613
primop   DoubleDecode_Int64Op   "decodeDouble_Int64#" GenPrimOp
   Double# -> (# INT64, Int# #)
   {Decode {\tt Double\#} into mantissa and base-2 exponent.}
   with out_of_line = True

614
------------------------------------------------------------------------
615
section "Float#"
616
        {Operations on single-precision (32-bit) floating-point numbers.}
617 618
------------------------------------------------------------------------

619 620
primtype Float#

621 622
primop   FloatGtOp  "gtFloat#"   Compare   Float# -> Float# -> Int#
primop   FloatGeOp  "geFloat#"   Compare   Float# -> Float# -> Int#
623

624
primop   FloatEqOp  "eqFloat#"   Compare
625
   Float# -> Float# -> Int#
626 627
   with commutable = True

628
primop   FloatNeOp  "neFloat#"   Compare
629
   Float# -> Float# -> Int#
630 631
   with commutable = True

632 633
primop   FloatLtOp  "ltFloat#"   Compare   Float# -> Float# -> Int#
primop   FloatLeOp  "leFloat#"   Compare   Float# -> Float# -> Int#
634

635
primop   FloatAddOp   "plusFloat#"      Dyadic
636 637 638 639 640
   Float# -> Float# -> Float#
   with commutable = True

primop   FloatSubOp   "minusFloat#"      Dyadic      Float# -> Float# -> Float#

641
primop   FloatMulOp   "timesFloat#"      Dyadic
642 643 644
   Float# -> Float# -> Float#
   with commutable = True

645
primop   FloatDivOp   "divideFloat#"      Dyadic
646 647 648 649 650 651
   Float# -> Float# -> Float#
   with can_fail = True

primop   FloatNegOp   "negateFloat#"      Monadic    Float# -> Float#

primop   Float2IntOp   "float2Int#"      GenPrimOp  Float# -> Int#
652 653 654
   {Truncates a {\tt Float#} value to the nearest {\tt Int#}.
    Results are undefined if the truncation if truncation yields
    a value outside the range of {\tt Int#}.}
655

Jan Stolarek's avatar
Jan Stolarek committed
656
primop   FloatExpOp   "expFloat#"      Monadic
657
   Float# -> Float#
658 659
   with
   code_size = { primOpCodeSizeForeignCall }
660

Jan Stolarek's avatar
Jan Stolarek committed
661
primop   FloatLogOp   "logFloat#"      Monadic
662
   Float# -> Float#
663 664 665
   with
   code_size = { primOpCodeSizeForeignCall }
   can_fail = True
666

Jan Stolarek's avatar
Jan Stolarek committed
667
primop   FloatSqrtOp   "sqrtFloat#"      Monadic
668
   Float# -> Float#
669 670
   with
   code_size = { primOpCodeSizeForeignCall }
671

Jan Stolarek's avatar
Jan Stolarek committed
672
primop   FloatSinOp   "sinFloat#"      Monadic
673
   Float# -> Float#
674 675
   with
   code_size = { primOpCodeSizeForeignCall }
676

Jan Stolarek's avatar
Jan Stolarek committed
677
primop   FloatCosOp   "cosFloat#"      Monadic
678
   Float# -> Float#
679 680
   with
   code_size = { primOpCodeSizeForeignCall }
681

Jan Stolarek's avatar
Jan Stolarek committed
682
primop   FloatTanOp   "tanFloat#"      Monadic
683
   Float# -> Float#
684 685
   with
   code_size = { primOpCodeSizeForeignCall }
686

Jan Stolarek's avatar
Jan Stolarek committed
687
primop   FloatAsinOp   "asinFloat#"      Monadic
688
   Float# -> Float#
689 690 691
   with
   code_size = { primOpCodeSizeForeignCall }
   can_fail = True
692

Jan Stolarek's avatar
Jan Stolarek committed
693
primop   FloatAcosOp   "acosFloat#"      Monadic
694
   Float# -> Float#
695 696 697
   with
   code_size = { primOpCodeSizeForeignCall }
   can_fail = True
698

Jan Stolarek's avatar
Jan Stolarek committed
699
primop   FloatAtanOp   "atanFloat#"      Monadic
700
   Float# -> Float#
701 702
   with
   code_size = { primOpCodeSizeForeignCall }
703

Jan Stolarek's avatar
Jan Stolarek committed
704
primop   FloatSinhOp   "sinhFloat#"      Monadic
705
   Float# -> Float#
706 707
   with
   code_size = { primOpCodeSizeForeignCall }
708

Jan Stolarek's avatar
Jan Stolarek committed
709
primop   FloatCoshOp   "coshFloat#"      Monadic
710
   Float# -> Float#
711 712
   with
   code_size = { primOpCodeSizeForeignCall }
713

Jan Stolarek's avatar
Jan Stolarek committed
714
primop   FloatTanhOp   "tanhFloat#"      Monadic
715
   Float# -> Float#
716 717
   with
   code_size = { primOpCodeSizeForeignCall }
718

Jan Stolarek's avatar
Jan Stolarek committed
719
primop   FloatPowerOp   "powerFloat#"      Dyadic
720
   Float# -> Float# -> Float#
721 722
   with
   code_size = { primOpCodeSizeForeignCall }
723 724 725

primop   Float2DoubleOp   "float2Double#" GenPrimOp  Float# -> Double#

726 727
primop   FloatDecode_IntOp   "decodeFloat_Int#" GenPrimOp
   Float# -> (# Int#, Int# #)
728
   {Convert to integers.
729 730 731
    First {\tt Int\#} in result is the mantissa; second is the exponent.}
   with out_of_line = True

732
------------------------------------------------------------------------
apt's avatar
apt committed
733
section "Arrays"
734
        {Operations on {\tt Array\#}.}
735 736
------------------------------------------------------------------------

737 738
primtype Array# a

739
primtype MutableArray# s a
740

apt's avatar
apt committed
741
primop  NewArrayOp "newArray#" GenPrimOp
742
   Int# -> a -> State# s -> (# State# s, MutableArray# s a #)
743
   {Create a new mutable array with the specified number of elements,
apt's avatar
apt committed
744 745 746 747
    in the specified state thread,
    with each element containing the specified initial value.}
   with
   out_of_line = True
748
   has_side_effects = True
749

apt's avatar
apt committed
750
primop  SameMutableArrayOp "sameMutableArray#" GenPrimOp
751
   MutableArray# s a -> MutableArray# s a -> Int#
752

apt's avatar
apt committed
753
primop  ReadArrayOp "readArray#" GenPrimOp
754
   MutableArray# s a -> Int# -> State# s -> (# State# s, a #)
apt's avatar
apt committed
755
   {Read from specified index of mutable array. Result is not yet evaluated.}
756 757
   with
   has_side_effects = True
758
   can_fail         = True
759

apt's avatar
apt committed
760
primop  WriteArrayOp "writeArray#" GenPrimOp
761
   MutableArray# s a -> Int# -> a -> State# s -> State# s
apt's avatar
apt committed
762 763 764
   {Write to specified index of mutable array.}
   with
   has_side_effects = True
765 766
   can_fail         = True
   code_size        = 2 -- card update too
767

pumpkin's avatar
pumpkin committed
768 769 770 771 772 773 774 775
primop  SizeofArrayOp "sizeofArray#" GenPrimOp
   Array# a -> Int#
   {Return the number of elements in the array.}

primop  SizeofMutableArrayOp "sizeofMutableArray#" GenPrimOp
   MutableArray# s a -> Int#
   {Return the number of elements in the array.}

apt's avatar
apt committed
776 777 778 779
primop  IndexArrayOp "indexArray#" GenPrimOp
   Array# a -> Int# -> (# a #)
   {Read from specified index of immutable array. Result is packaged into
    an unboxed singleton; the result itself is not yet evaluated.}
780 781
   with
   can_fail         = True
782

apt's avatar
apt committed
783
primop  UnsafeFreezeArrayOp "unsafeFreezeArray#" GenPrimOp
784
   MutableArray# s a -> State# s -> (# State# s, Array# a #)
apt's avatar
apt committed
785 786 787
   {Make a mutable array immutable, without copying.}
   with
   has_side_effects = True
788

apt's avatar
apt committed
789
primop  UnsafeThawArrayOp  "unsafeThawArray#" GenPrimOp
790
   Array# a -> State# s -> (# State# s, MutableArray# s a #)
apt's avatar
apt committed
791 792 793
   {Make an immutable array mutable, without copying.}
   with
   out_of_line = True
794
   has_side_effects = True
795

pumpkin's avatar
pumpkin committed
796 797
primop  CopyArrayOp "copyArray#" GenPrimOp
  Array# a -> Int# -> MutableArray# s a -> Int# -> Int# -> State# s -> State# s
tibbe's avatar
tibbe committed
798 799 800 801 802 803 804
  {Given a source array, an offset into the source array, a
   destination array, an offset into the destination array, and a
   number of elements to copy, copy the elements from the source array
   to the destination array. Both arrays must fully contain the
   specified ranges, but this is not checked. The two arrays must not
   be the same array in different states, but this is not checked
   either.}
pumpkin's avatar
pumpkin committed
805
  with
806
  out_of_line      = True
pumpkin's avatar
pumpkin committed
807
  has_side_effects = True
808
  can_fail         = True
pumpkin's avatar
pumpkin committed
809 810 811

primop  CopyMutableArrayOp "copyMutableArray#" GenPrimOp
  MutableArray# s a -> Int# -> MutableArray# s a -> Int# -> Int# -> State# s -> State# s
tibbe's avatar
tibbe committed
812 813 814 815 816 817
  {Given a source array, an offset into the source array, a
   destination array, an offset into the destination array, and a
   number of elements to copy, copy the elements from the source array
   to the destination array. The source and destination arrays can
   refer to the same array. Both arrays must fully contain the
   specified ranges, but this is not checked.}
pumpkin's avatar
pumpkin committed
818
  with
819
  out_of_line      = True
pumpkin's avatar
pumpkin committed
820
  has_side_effects = True
821
  can_fail         = True
pumpkin's avatar
pumpkin committed
822 823 824

primop  CloneArrayOp "cloneArray#" GenPrimOp
  Array# a -> Int# -> Int# -> Array# a
tibbe's avatar
tibbe committed
825 826 827 828
  {Given a source array, an offset into the source array, and a number
   of elements to copy, create a new array with the elements from the
   source array. The provided array must fully contain the specified
   range, but this is not checked.}
pumpkin's avatar
pumpkin committed
829
  with
830
  out_of_line      = True
pumpkin's avatar
pumpkin committed
831
  has_side_effects = True
832
  can_fail         = True
pumpkin's avatar
pumpkin committed
833 834 835

primop  CloneMutableArrayOp "cloneMutableArray#" GenPrimOp
  MutableArray# s a -> Int# -> Int# -> State# s -> (# State# s, MutableArray# s a #)
tibbe's avatar
tibbe committed
836 837 838 839
  {Given a source array, an offset into the source array, and a number
   of elements to copy, create a new array with the elements from the
   source array. The provided array must fully contain the specified
   range, but this is not checked.}
pumpkin's avatar
pumpkin committed
840
  with
841
  out_of_line      = True
pumpkin's avatar
pumpkin committed
842
  has_side_effects = True
843
  can_fail         = True
pumpkin's avatar
pumpkin committed