Comments (4)
example: https://gist.github.com/missinglink/59d4ddb377c9f3a08365cffa6a5ec052
function trailingZeros64(a) {
for(let i=7; i>=0; i--) {
let lsb = (a[i]) & (-a[i])
if(lsb > 0) {
return (32 - 1 - Math.clz32(lsb)) + ((7-i) * 8)
}
}
return 64
}
trailingZeros64([0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00])
from proposal-math-extensions.
Should there also be clo32
and cto32
? (Leading and Trailing 1s, respectively). What about popcount32 (Hamming Weight, AKA Bit Sum) and rev32
(reverse 32bit register, reflecting around its absolute center)?
What about non 32bit bitwise functions? Popcount can be implemented internally by doing popcnt
on the mantissa and adding 1 (implicit bit). CTZ can be defined as trial division by 2 (internally implemented by exponent masking), it can also be defined for non-int values by returning negative ints that correspond to the 0s after the radix point, like this CTZ(0b0.1) = CTZ(0b1.1) = -1 && CTZ(0b0.01) = -2 && CTZ(0b0.00101) = CTZ(0b0.001) = -3
.
The reverse fn would be implemented using a relative axis of reflection, so the flip point would depend on the binary logarithm of the input. The rev32
fn could have a "Byte mode" instead of "bit mode", therefore becoming a big-to-little (and viceversa) endianess converter, although that seems useless for a high-level lang, except when working with an ArrayBuffer
and many View
s.
By adding these extended methods that aren't restricted to int32
s, they can be compatible with both Number
s and BigInt
s when the BigInt Math proposal gets to Stage 4 (if it actually takes the path of polymorphism, instead of dedicated methods on a different namespace)
from proposal-math-extensions.
What about Math.clmul32
? (carryless multiply)
from proposal-math-extensions.
@missinglink That's an interesting implementation! But I'm afraid it won't be accepted by tc39, as it lacks consistency with the existing standards
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Methods to be added HOT 8
- [Suggestion] Math.mean and Math.roundBy HOT 2
- [Suggestion] Constants to be added HOT 1
- Is it still active? HOT 7
- [Suggestion] Math.lerp and Math.normalize HOT 2
- alternatives to Math.radians and Math.degrees HOT 5
- Math.exp2
- modulo function HOT 8
- Math.pow should accept Bigints, modulo and pow=-1 HOT 12
- Proposal Math.clamp separately HOT 1
- In the spec draft missed conversion of arguments to numbers
- Repository should be transferred to tc39 organization HOT 2
- Should `factorial` be defined in all or some domains? HOT 2
- Dot product?
- New rounding modes HOT 1
- Tuple methods
- `Number.prototype.toByteSize` / `Intl.SizeFormat` HOT 2
- `Math.copysign`? HOT 1
- `Math.MAX_RANDOM_BITS`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-math-extensions.