aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/i64.lux
blob: b7922e45d094ed9e8fdca1318585120287b07450 (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
(.require
 [library
  [lux (.except and or not false true)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    [monoid (.only Monoid)]]
   [control
    ["[0]" 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))

(with_template [<parameter_type> <name> <op>]
  [(def .public (<name> parameter subject)
     (All (_ s) (-> <parameter_type> (I64 s) (I64 s)))
     (<op> parameter subject))]

  [(I64 Any) or .i64_or#]
  [(I64 Any) xor .i64_xor#]
  [(I64 Any) and .i64_and#]
  
  [Nat left_shifted .i64_left#]
  [Nat right_shifted .i64_right#]
  )

... https://en.wikipedia.org/wiki/Mask_(computing)
(type .public Mask
  I64)

(def .public (bit position)
  (-> Nat Mask)
  (|> 1 .i64 (..left_shifted (n.% ..width position))))

(def .public sign
  Mask
  (..bit (-- ..width)))

(def .public not
  (All (_ s) (-> (I64 s) (I64 s)))
  (..xor (.i64 (-- 0))))

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

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

(def .public (mask amount_of_bits)
  (-> Nat Mask)
  (when amount_of_bits
    0 ..false
    bits (when (n.% ..width bits)
           0 ..true
           bits (|> 1 .i64 (..left_shifted (n.% ..width bits)) .--))))

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

(def .public (ones it)
  (-> (I64 Any) Nat)
  (let [ones' (n.- (|> it (right_shifted 1) (..and 6148914691236517205) i64)
                   (i64 it))]
    (|> 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)
  (All (_ s) (-> Nat (I64 s) (I64 s)))
  (|> index ..bit ..not (..and input)))

(with_template [<name> <op>]
  [(def .public (<name> index input)
     (All (_ s) (-> Nat (I64 s) (I64 s)))
     (|> index ..bit (<op> input)))]

  [one     ..or]
  [flipped ..xor]
  )

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

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

(with_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)
  (-> Nat Nat Mask)
  (..left_rotated offset (..mask size)))

(def .public equivalence
  (All (_ a) (Equivalence (I64 a)))
  (implementation
   (def (= reference sample)
     (.i64_=# reference sample))))

(def .public hash
  (All (_ a) (Hash (I64 a)))
  (implementation
   (def equivalence ..equivalence)

   (def hash (|>> .nat))))

(with_template [<monoid> <identity> <composite>]
  [(def .public <monoid>
     (All (_ a) (Monoid (I64 a)))
     (implementation
      (def identity <identity>)
      (def composite <composite>)))]

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

(def .public reversed
  (All (_ a) (-> (I64 a) (I64 a)))
  (let [swapper (is (-> Nat (All (_ a) (-> (I64 a) (I64 a))))
                    (function (_ power)
                      (let [size (..left_shifted power 1)
                            repetitions (is (-> Nat Text Text)
                                            (function (_ times char)
                                              (loop (again [iterations 1
                                                            output char])
                                                (if (n.< times iterations)
                                                  (again (++ iterations)
                                                         (.text_composite# char output))
                                                  output))))
                            pattern (repetitions (n./ (n.+ size size) ..width)
                                                 (.text_composite# (repetitions size "1")
                                                                   (repetitions size "0")))

                            high (try.trusted (at n.binary decoded 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)))

(type .public (Sub size)
  (Interface
   (is (Equivalence (I64 size))
       sub_equivalence)
   (is Nat
       bits)
   (is (-> I64 (I64 size))
       narrow)
   (is (-> (I64 size) I64)
       wide)))

(def .public (sub width)
  (Ex (_ size) (-> Nat (Maybe (Sub size))))
  (if (.and (n.> 0 width)
            (n.< ..width width))
    (let [sign_shift (n.- width ..width)
          sign (..bit (-- width))
          mantissa (..mask (-- width))
          co_mantissa (..xor (.i64 -1) mantissa)]
      {.#Some (is Sub
                  (implementation
                   (def sub_equivalence ..equivalence)
                   (def bits width)
                   (def (narrow value)
                     (..or (|> value (..and ..sign) (..right_shifted sign_shift))
                           (|> value (..and mantissa))))
                   (def (wide value)
                     (.i64 (when (.nat (..and sign value))
                             0 value
                             _ (..or co_mantissa value))))))})
    {.#None}))