aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/number/i64.lux
blob: 79bf7632d74ca8ea0eaa726a87bd11e4d4dbf549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(.module:
  [lux (#- and or not false true)
   [abstract
    [equivalence (#+ Equivalence)]
    [hash (#+ Hash)]
    [monoid (#+ Monoid)]]
   [data
    [number
     ["n" nat]]]])

(def: #export bits-per-byte
  8)

(def: #export bytes-per-i64
  8)

(def: #export width
  Nat
  (n.* ..bits-per-byte
       ..bytes-per-i64))

(template [<parameter-type> <name> <op> <doc>]
  [(def: #export (<name> parameter subject)
     {#.doc <doc>}
     (All [s] (-> <parameter-type> (I64 s) (I64 s)))
     (<op> parameter subject))]

  [(I64 Any) and                    "lux i64 and"                    "Bitwise and."]
  [(I64 Any) or                     "lux i64 or"                     "Bitwise or."]
  [(I64 Any) xor                    "lux i64 xor"                    "Bitwise xor."]
  
  [Nat       left-shift             "lux i64 left-shift"             "Bitwise left-shift."]
  [Nat       logic-right-shift      "lux i64 logical-right-shift"    "Unsigned bitwise logic-right-shift."]
  [Nat       arithmetic-right-shift "lux i64 arithmetic-right-shift" "Signed bitwise arithmetic-right-shift."]
  )

(def: #export not
  {#.doc "Bitwise negation."}
  (All [s] (-> (I64 s) (I64 s)))
  (xor (:coerce I64 -1)))

(type: #export Mask
  I64)

(def: #export false
  Mask
  (.i64 0))

(def: #export true
  Mask
  (..not ..false))

(def: #export (mask amount-of-bits)
  (-> Nat Mask)
  (case amount-of-bits
    0 ..false
    bits (case (n.% ..width bits)
           0 ..true
           bits (|> 1 .i64 (..left-shift (n.% ..width bits)) .dec))))

(def: #export (bit position)
  (-> Nat Mask)
  (|> 1 .i64 (..left-shift (n.% ..width position))))

(def: #export sign
  Mask
  (..bit 63))

(def: (add-shift shift value)
  (-> Nat Nat Nat)
  (|> value (logic-right-shift shift) (n.+ value)))

(def: #export (count subject)
  {#.doc "Count the number of 1s in a bit-map."}
  (-> (I64 Any) Nat)
  (let [count' (n.- (|> subject (logic-right-shift 1) (..and 6148914691236517205) i64)
                    (i64 subject))]
    (|> count'
        (logic-right-shift 2) (..and 3689348814741910323) (n.+ (..and 3689348814741910323 count'))
        (add-shift 4) (..and 1085102592571150095)
        (add-shift 8)
        (add-shift 16)
        (add-shift 32)
        (..and 127))))

(def: #export (clear idx input)
  {#.doc "Clear bit at given index."}
  (All [s] (-> Nat (I64 s) (I64 s)))
  (|> idx ..bit ..not (..and input)))

(template [<name> <op> <doc>]
  [(def: #export (<name> idx input)
     {#.doc <doc>}
     (All [s] (-> Nat (I64 s) (I64 s)))
     (|> idx ..bit (<op> input)))]

  [set  ..or  "Set bit at given index."]
  [flip ..xor "Flip bit at given index."]
  )

(def: #export (set? idx input)
  (-> Nat (I64 Any) Bit)
  (|> input (:coerce I64) (..and (..bit idx)) (n.= 0) .not))

(template [<name> <main> <comp>]
  [(def: #export (<name> distance input)
     (All [s] (-> Nat (I64 s) (I64 s)))
     (let [backwards-distance (n.- (n.% width distance) width)]
       (|> input
           (<comp> backwards-distance)
           (..or (<main> distance input)))))]

  [rotate-left  left-shift        logic-right-shift]
  [rotate-right logic-right-shift left-shift]
  )

(def: #export (region size offset)
  (-> Nat Nat I64)
  (|> 1 (:coerce I64) (left-shift size) dec (left-shift offset)))

(structure: #export equivalence
  (All [a] (Equivalence (I64 a)))

  (def: (= parameter subject)
    ("lux i64 =" parameter subject)))

(structure: #export hash
  (All [a] (Hash (I64 a)))

  (def: &equivalence ..equivalence)

  (def: hash .nat))

(template [<monoid> <identity> <compose>]
  [(structure: #export <monoid>
     (All [a] (Monoid (I64 a)))

     (def: identity <identity>)
     (def: compose <compose>)
     )]

  [disjunction ..false ..or]
  [conjunction ..true ..and]
  )

(type: #export (Sub size)
  {#narrow (-> I64 (I64 size))
   #wide (-> (I64 size) I64)
   #equivalence (Equivalence (I64 size))})

(def: #export (sub width)
  (Ex [size] (-> Nat (Maybe (Sub size))))
  (if (.and (n.> 0 width)
            (n.< ..width width))
    (let [top (dec width)
          shift (n.- width ..width)
          sign (..bit top)
          number (..mask (dec width))]
      (#.Some {#narrow (function (narrow value)
                         (..or (|> value (..and ..sign) (..logic-right-shift shift))
                               (|> value (..and number))))
               #wide (function (wide value)
                       (|> (..or (|> value (..and sign) (..left-shift shift))
                                 (|> value (..clear top)))
                           .i64))
               #equivalence ..equivalence}))
    #.None))