Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perpare for more compact bit operations in JS #16728

Merged
merged 1 commit into from
Jan 19, 2021
Merged

Conversation

ringabout
Copy link
Member

@ringabout ringabout commented Jan 15, 2021

Before: it is hard to reuse same variables by emitting JS(see #16609)

when defined(js):
  proc toBitsImpl(x: float): array[2, uint32] =
    asm """
    const buffer = new ArrayBuffer(8);
    const a = new Float64Array(buffer);
    const b = new Uint32Array(buffer);
    a[0] = `x`;
    `result` = b
    """

  proc jsSetSign(x: float, sgn: bool): float =
    asm """
    function updateBit(num, bitPos, bitVal) {
      return (num & ~(1 << bitPos)) | (bitVal << bitPos);
    }
    const buffer = new ArrayBuffer(8);
    const a = new Float64Array(buffer);
    const b = new Uint32Array(buffer);
    a[0] = `x`;
    b[1] = updateBit(b[1], 31, `sgn`);
    `result` = a[0]
    """

After
You could reuse these variables in a proc.

   const buffer = new ArrayBuffer(8);
   const a = new Float64Array(buffer);
   const b = new Uint32Array(buffer);

@timotheecour
Copy link
Member

see also: #14128 which is relevant. The fact that your PR lives in std/private is good, it means we can keep improving the API until it stabilizes

func newUint32Array*(buffer: ArrayBuffer): Uint32Array {.importjs: "new Uint32Array(#)".}

func `[]`*(arr: Uint32Array, i: int): uint32 {.importjs: "#[#]".}
func `[]=`*(arr: Float64Array, i: int, v: float) {.importjs: "#[#] = #".}
Copy link
Member

@timotheecour timotheecour Jan 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be arr: Float64Array or arr: var Float64Array ? I'm really not sure here since the type is ref, but really, Float64Array + friends act like seq, so var seems to make more sense here.

what are downsides of using var here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var will generates array of typed array

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is generally true of var in nim js; the question is whether this has a performance impact in this case or whether v8 can optimize this (I'm not sure how to check other than performance timing)

Copy link
Member Author

@ringabout ringabout Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to cause some errors in some situations, but I forgot the example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that for var you need the awkard syntax like this: [#][0][0]

# eg: jsbigints:
func inc*(this: var JsBigInt; amount: JsBigInt) {.importjs: "([#][0][0] += #)".} =

(until nim-lang/RFCs#315 is resolved)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants