Skip to content

Conversation

gradha
Copy link
Contributor

@gradha gradha commented Feb 6, 2014

No description provided.

@reactormonk
Copy link
Contributor

You forgot the actual code.

@gradha
Copy link
Contributor Author

gradha commented Feb 6, 2014

Added code.

@Araq
Copy link
Member

Araq commented Feb 7, 2014

Make it work for integers too. And I prefer an inline proc.

@gradha
Copy link
Contributor Author

gradha commented Feb 9, 2014

Made inline proc. Added also to complex module. How is this to work with integers? I see no pow for int.

@reactormonk
Copy link
Contributor

math.h only defines it for float.

@Araq Araq added the feature label Feb 24, 2014
@Araq
Copy link
Member

Araq commented Mar 3, 2014

So implement something like:

proc pow(a, b: int): int =
  var a = a
  var b = b
  result = 1
  while true:
    if (b and 1) == 1:
      result *= a
    b = b shr 1
    if b == 0: break
    a *= a

@reactormonk
Copy link
Contributor

proc pow(a, b: int): int =
  var a = a
  var b = b
  result = 1
  while true:
    if (b and 1) == 1:
      result *= a
    b = b shr 1
    if b == 0: break
    a *= a

for i in 1..10000000:
  var foo = pow(2, 30)

gives me

➜  /tmp  time ./inttest  
./inttest  1.48s user 0.00s system 98% cpu 1.493 total

vs. casting

import math 
for i in 1..10000000:
  var f = 2
  var p = 30
  var foo = int(pow(float(f), float(p)))

which results in

➜  /tmp  time ./floattest
./floattest  0.71s user 0.00s system 98% cpu 0.722 total

@zielmicha
Copy link
Contributor

The float version is faster, but incorrect for numbers over 2**60

@Varriount
Copy link
Contributor

You could always have it use the second method for numbers above 2**60, if you're especially concerned about speed.

@Araq
Copy link
Member

Araq commented Mar 5, 2014

The benchmark is flawed, you need to try different exponents and weight them by a reasonable likelihood. Hint: exponent 3 is more likely than exponent 30...

@Araq
Copy link
Member

Araq commented Apr 7, 2014

** is weird, it should be ^

@Araq Araq closed this Apr 7, 2014
@gradha gradha deleted the pr_adds_pow_alias branch April 8, 2014 10:04
Clyybber pushed a commit to Clyybber/Nim that referenced this pull request Sep 16, 2023
<!--- The Pull Request (=PR) message is what will get automatically used
as
the commit message when the PR is merged. Make sure that no line is
longer
than 72 characters -->

## Summary

Convert `gorge` and `staticExec` in the `system` module from magics to
VM callback operations.

## Details

Removal of the magic requires creating a conditional compilation step
based on the `nimskullReworkStaticExec` conditional symbol (see:
`condsym`). Two definitions of `gorge` and `staticExec` now exist, one
with the magic and one without. Without this, bootstrapping would no
longer work.

Also marked `gorge` as deprecated, `staticExec` is a far better name,
and noted that `staticExec` will have its return type revised to return
the exit code in addition to the output. The reason for the deprecation
is that `gorge` is just not as clear as `staticExec`, same for `slurp`.

This change enables future removal of the `mStaticExec` magic enum, and
more importantly the `opcGorge` VM opcode in the near future.
Clyybber added a commit to Clyybber/Nim that referenced this pull request Sep 24, 2023
## Summary
* Remove `mStaticExec` and `opcGorge` as only the csources compiler
  needs to have them in order to bootstrap 

## Details
* Follow up to 'vm: convert gorge and staticExec to vmops nim-lang#878'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants