aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/i64.lux
blob: df8d01dbe6c22a3d09ec2833ba00c42b1abff873 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(.module:
  [library
   [lux (#- and or not false true)
    [abstract
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]
     [monoid (#+ Monoid)]]
    [control
     ["." try]]]]
  [//
   ["n" nat]])

(def: .public bits_per_byte
  8)

(def: .public bytes_per_i64
  8)

(def: .public width
  Nat
  (n.* ..bits_per_byte
       ..bytes_per_i64))

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

  [(I64 Any) or          "lux i64 or"          "Bitwise or."]
  [(I64 Any) xor         "lux i64 xor"         "Bitwise xor."]
  [(I64 Any) and         "lux i64 and"         "Bitwise and."]
  
  [Nat       left_shifted  "lux i64 left-shift"  "Bitwise left-shift."]
  [Nat       right_shifted "lux i64 right-shift" "Unsigned/logic bitwise right-shift."]
  )

... https://en.wikipedia.org/wiki/Mask_(computing)
(type: .public Mask
  {#.doc (example "A pattern of bits that can be imposed on I64 values.")}
  I64)

(def: .public (bit position)
  {#.doc (example "A mask with only a specific bit set.")}
  (-> Nat Mask)
  (|> 1 .i64 (..left_shifted (n.% ..width position))))

(def: .public sign
  {#.doc (example "A mask for the sign bit of ints.")}
  Mask
  (..bit (dec ..width)))

(def: .public not
  {#.doc "Bitwise negation."}
  (All [s] (-> (I64 s) (I64 s)))
  (..xor (.i64 (dec 0))))

(def: .public false
  Mask
  (.i64 0))

(def: .public true
  Mask
  (..not ..false))

(def: .public (mask amount_of_bits)
  {#.doc (example "Mask a block of bits of the specified size.")}
  (-> Nat Mask)
  (case amount_of_bits
    0 ..false
    bits (case (n.% ..width bits)
           0 ..true
           bits (|> 1 .i64 (..left_shifted (n.% ..width bits)) .dec))))

(def: (with_shift shift value)
  (-> Nat Nat Nat)
  (|> value (right_shifted shift) (n.+ value)))

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

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

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

  [one     ..or  "Set bit at given index."]
  [flipped ..xor "Flip bit at given index."]
  )

(def: .public (one? index input)
  (-> Nat (I64 Any) Bit)
  (|> input (:as I64) (..and (..bit index)) (n.= 0) .not))

(def: .public (zero? index input)
  (-> Nat (I64 Any) Bit)
  (.not (..one? index input)))

(template [<name> <forward> <backward>]
  [(def: .public (<name> distance input)
     (All [s] (-> Nat (I64 s) (I64 s)))
     (..or (<forward> distance input)
           (<backward> (n.- (n.% ..width distance) ..width) input)))]

  [left_rotated  ..left_shifted  ..right_shifted]
  [right_rotated ..right_shifted ..left_shifted]
  )

(def: .public (region offset size)
  {#.doc (example "A mask for a block of bits of the given size, starting at the given offset.")}
  (-> Nat Nat Mask)
  (..left_rotated offset (..mask size)))

(implementation: .public equivalence
  (All [a] (Equivalence (I64 a)))

  (def: (= reference sample)
    ("lux i64 =" reference sample)))

(implementation: .public hash
  (All [a] (Hash (I64 a)))

  (def: &equivalence ..equivalence)

  (def: hash .nat))

(template [<monoid> <identity> <compose>]
  [(implementation: .public <monoid>
     (All [a] (Monoid (I64 a)))

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

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

(def: .public reversed
  (All [a] (-> (I64 a) (I64 a)))
  (let [swapper (: (-> Nat (All [a] (-> (I64 a) (I64 a))))
                   (function (_ power)
                     (let [size (..left_shifted power 1)
                           repetitions (: (-> Nat Text Text)
                                          (function (_ times char)
                                            (loop [iterations 1
                                                   output char]
                                              (if (n.< times iterations)
                                                (recur (inc iterations)
                                                       ("lux text concat" char output))
                                                output))))
                           pattern (repetitions (n./ (n.+ size size) ..width)
                                                ("lux text concat"
                                                 (repetitions size "1")
                                                 (repetitions size "0")))

                           high (try.assumed (\ n.binary decode pattern))
                           low (..right_rotated size high)]
                       (function (_ value)
                         (..or (..right_shifted size (..and high value))
                               (..left_shifted size (..and low value)))))))
        
        swap/01 (swapper 0)
        swap/02 (swapper 1)
        swap/04 (swapper 2)
        swap/08 (swapper 3)
        swap/16 (swapper 4)
        swap/32 (swapper 5)]
    (|>> swap/32
         swap/16
         swap/08
         swap/04
         swap/02
         swap/01)))

(interface: .public (Sub size)
  {#.doc (example "A sub-space of I64 with a reduce amount of bits.")}
  
  (: (Equivalence (I64 size))
     &equivalence)
  (: Nat
     width)
  (: (-> I64 (I64 size))
     narrow)
  (: (-> (I64 size) I64)
     widen))

(def: .public (sub width)
  {#.doc (example "Given a width in the interval (0,64), yields an implementation for integers of that width.")}
  (Ex [size] (-> Nat (Maybe (Sub size))))
  (if (.and (n.> 0 width)
            (n.< ..width width))
    (let [sign_shift (n.- width ..width)
          sign (..bit (dec width))
          mantissa (..mask (dec width))
          co_mantissa (..xor (.i64 -1) mantissa)]
      (#.Some (: Sub
                 (implementation
                  (def: &equivalence ..equivalence)
                  (def: width width)
                  (def: (narrow value)
                    (..or (|> value (..and ..sign) (..right_shifted sign_shift))
                          (|> value (..and mantissa))))
                  (def: (widen value)
                    (.i64 (case (.nat (..and sign value))
                            0 value
                            _ (..or co_mantissa value))))))))
    #.None))