Skip to content

Commit

Permalink
Merge pull request #14 from mrLSD/feat/bits
Browse files Browse the repository at this point in the history
Cleanup Bits functions
  • Loading branch information
mrLSD authored Aug 20, 2023
2 parents fa039b1 + d9f2a13 commit e629c5b
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions Bits.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ module ISA.RISCV.Utils.Bits
type System.Int64 with
member x.bitSlice endBit startBit = // get Bit slice from range
(x >>> startBit) &&& ~~~(-1L <<< (endBit - startBit + 1))
member x.signExtend n = // Sign extend bits for x64
let bitOffset = 64 - n
(x <<< bitOffset) >>> bitOffset
member x.align = // get x64 mask with all `1` bits
x &&& (-1L)
member x.flip i = // change bit
(x ^^^ (1L <<< i))
member x.isSet i = // test if bit set at a specified position
x &&& (1L <<< i) <> 0L
member x.rotateLeft r =
(x <<< r) ||| (x >>> (32 - r))
member x.rotateRight r =
(x >>> r) ||| (x <<< (32 - r))

(* bit coersion methods *)
member x.toHex = sprintf "0x%x" x // to hexadecimal
(* bit coercion methods *)
member x.toHex = $"0x%x{x}" // to hexadecimal
member x.toBin = // to binary string
System.Convert.ToString(x, 2).PadLeft(64, '0')
member x.toResizeArray = // to Resizable array of positions set to 1
Expand All @@ -29,30 +18,19 @@ type System.Int64 with
member x.toArray = // to array of positions set to 1
let res = x.toResizeArray
res.ToArray()
member x.toList = // to list of positions set to 1
let res = x.toResizeArray
Array.toList (res.ToArray())
member x.toSeq = // to seq of positions set to 1
let res = x.toResizeArray
Array.toSeq (res.ToArray())

(* bit print methods *)
member x.print = printf "%A" x
member x.print = printf $"%A{x}"
member x.display = // helper to show bits
x.toArray |> Seq.iter(fun i -> printf "%A " i)
x.toArray |> Seq.iter(fun i -> printf $"%A{i} ")

(* misc methods *)
member x.abs = // fast math.abs
(x ^^^ (x >>> 31)) - (x >>> 31)

type System.Int32 with
member x.bitSlice endBit startBit = // get Bit slice from range
(x >>> startBit) &&& ~~~(-1 <<< (endBit - startBit + 1))
member x.signExtend n = // Sign extend bits for x32
let bitOffset = 32 - n
(x <<< bitOffset) >>> bitOffset
member x.align = // get x32 mask with all `1` bits
x &&& (-1)

let combineBytes (x : byte array) : int64 =
let xz = Array.zip [|0..x.Length-1|] x
Expand Down

0 comments on commit e629c5b

Please sign in to comment.