github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/sifive/clint/timer.go (about)

     1  // SiFive Core-Local Interruptor (CLINT) driver
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright 2022 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in Go LICENSE file.
     7  
     8  package clint
     9  
    10  func mulDiv(x, m, d uint64) uint64 {
    11  	divx := x / d
    12  	modx := x - divx*d
    13  	divm := m / d
    14  	modm := m - divm*d
    15  	return divx*m + modx*divm + modx*modm/d
    16  }
    17  
    18  // Nanotime returns the number of nanoseconds counted from the RTCCLK input
    19  // plus the timer offset.
    20  func (hw *CLINT) Nanotime() int64 {
    21  	return int64(mulDiv(hw.Mtime(), 1e9, hw.RTCCLK)) + hw.TimerOffset
    22  }