1
0
mirror of https://github.com/golang/go synced 2024-10-04 16:21:22 -06:00
Commit Graph

215 Commits

Author SHA1 Message Date
Michael T. Jones
eea28f6701 math/big: add support for general encoding interfaces
TextMarshaller and TextUnmarshaller to ease transport of
unlimited precision rational numbers.

Fixes #7287.

Consists of encode and decode functions and two test
functions, one using JSON and one using XML. Each
verifies round trips for integers (rationals with
denominator == 1) and for fractional vaues.

LGTM=gri
R=gri, cookieo9, bradfitz, mtj
CC=golang-codereviews
https://golang.org/cl/61180043
2014-02-13 08:42:19 -08:00
Richard Musiol
547a82c36c math/big: replace goto with for loop
I just added support for goto statements to my GopherJS project and now I am trying to get rid of my patches. These occurrences of goto however are a bit problematic:
GopherJS has to emulate gotos, so there is some performance drawback when doing so. In this case the drawback is major, since this is a core function of math/big which is called quite often. Additionally I can't see any reason here why the implementation with gotos should be preferred over my proposal.
That's why I would kindly ask to include this patch, even though it is functional equivalent to the existing code.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/55470046
2014-02-06 14:44:30 -08:00
Jeff R. Allen
17dc712c18 math/rand: Float32/64 must only return values in [0,1)
Float32 and Float64 are now both created by taking the ratio
of two integers which are chosen to fit entirely into the
precision of the desired float type. The previous code
could cast a Float64 with more than 23 bits of ".99999"
into a Float32 of 1.0, which is not in [0,1).

Float32 went from 15 to 21 ns/op (but is now correct).

Fixes #6721.

R=golang-dev, iant, rsc
CC=golang-dev
https://golang.org/cl/22730043
2013-12-18 15:38:53 -05:00
Josh Bleecher Snyder
4a18e0edd9 math/rand: minor optimization to Perm
Instead of writing out 0..n and then reading it
back, just use i when it is needed.

Wikipedia calls this the "inside-out" implementation:
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

This yields identical values to the previous
implementation, given the same seed. (Note that the
output from Example_rand is unchanged.)

2.8 GHz Intel Core i7, results very stable:

benchmark          old ns/op    new ns/op    delta
BenchmarkPerm3           138          136   -1.45%
BenchmarkPerm30          825          803   -2.67%

Stock Raspberry Pi, minimum improvement out of three runs:

benchmark          old ns/op    new ns/op    delta
BenchmarkPerm3          5774         5664   -1.91%
BenchmarkPerm30        32582        29381   -9.82%

R=golang-dev, dave, mtj, adg
CC=golang-dev
https://golang.org/cl/21030043
2013-12-17 13:49:34 +11:00
Russ Cox
089bc25ae2 math: remove unnecessary source file
The routines in this file are dregs from a very early copy of the math API.
There are no Go prototypes and no non-amd64 implementations.

R=golang-dev, minux.ma
CC=golang-dev
https://golang.org/cl/15750046
2013-10-22 10:37:33 -04:00
Rob Pike
9795882704 math: the trig functions work in radians; document that
Fixes #6543
6543 is also a fine NGC object.

R=golang-dev, dsymonds, kamil.kisiel, minux.ma
CC=golang-dev
https://golang.org/cl/14515044
2013-10-07 16:32:47 -07:00
Rob Pike
fa7791e922 all: fix some mistakes found by go tool vet .
R=golang-dev, iant, adg
CC=golang-dev
https://golang.org/cl/14000043
2013-09-27 10:09:15 +10:00
Kamil Kisiel
b0ef6aeff9 math/big: Fix variable name in documentation for *Int.Bytes and BitLen.
R=golang-dev, iant, gri
CC=golang-dev
https://golang.org/cl/13869043
2013-09-24 16:32:23 -07:00
Rob Pike
bc6bb3efb4 math/big: fix nil bug in GobEncode
Update #5305.
This handles the case where the nil pointers are inside a slice.
A top-level nil pointer is harder, maybe fundamentally broken by gob's model.
Thinking required.
However, a slice is the important case since people don't expect to be sending
top-level nils much, but they can arise easily in slices.

R=golang-dev, josharian, adg
CC=golang-dev
https://golang.org/cl/13042044
2013-08-19 11:22:09 +10:00
Keith Randall
8b789e1738 all: change textflags from numbers to symbols.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12774043
2013-08-12 10:25:36 -07:00
Keith Randall
1f7966346e math: convert textflags from numbers to symbols
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12773044
2013-08-12 10:25:18 -07:00
Brad Fitzpatrick
d8e27db395 undo CL 12486043 / ab644299d124
Uglier.

««« original CL description
all: use strings.IndexByte instead of Index where possible

R=golang-dev, khr
CC=golang-dev
https://golang.org/cl/12486043
»»»

R=golang-dev
CC=golang-dev
https://golang.org/cl/12485044
2013-08-05 16:27:24 -07:00
Brad Fitzpatrick
4c772cda54 all: use strings.IndexByte instead of Index where possible
R=golang-dev, khr
CC=golang-dev
https://golang.org/cl/12486043
2013-08-05 15:46:06 -07:00
Robert Griesemer
5bd61e0603 math/big: better documentation for Rat.Float64
R=adonovan, r
CC=golang-dev
https://golang.org/cl/11961043
2013-07-29 13:06:34 -07:00
Andrew Gerrand
e97c870692 math/rand: mention that the default Source is thread-safe
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/11709043
2013-07-24 08:27:20 +10:00
Andrew Gerrand
6efb6b9e38 math/rand: mention Seed in overview, add another top-level example
Fixes #5937.

R=golang-dev, mirtchovski, r
CC=golang-dev
https://golang.org/cl/11705043
2013-07-23 16:07:28 +10:00
Rob Pike
abe384f68a all: be more idiomatic when documenting boolean return values.
Phrases like "returns whether or not the image is opaque" could be
describing what the function does (it always returns, regardless of
the opacity) or what it returns (a boolean indicating the opacity).
Even when the "or not" is missing, the phrasing is bizarre.

Go with "reports whether", which is still clunky but at least makes
it clear we're talking about the return value.

These were edited by hand. A few were cleaned up in other ways.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/11699043
2013-07-23 11:59:49 +10:00
Robert Griesemer
6154ae8e24 math/big: fix Errorf verb
Pointed out by go vet.

R=r
CC=golang-dev
https://golang.org/cl/10368048
2013-06-18 14:16:40 -07:00
Adam Langley
133cdb6707 math/big: save some copies in binaryGCD.
This patch resulted from a bit of quick optimisation in response to a
golang-nuts post. It looks like one could save a couple other copies in
this function, but this addresses the inner loop and is fairly simple.

benchmark                    old ns/op    new ns/op    delta
BenchmarkGCD10x10                 1964         1711  -12.88%
BenchmarkGCD10x100                2019         1736  -14.02%
BenchmarkGCD10x1000               2471         2171  -12.14%
BenchmarkGCD10x10000              6040         5778   -4.34%
BenchmarkGCD10x100000            43204        43025   -0.41%
BenchmarkGCD100x100              11004         8520  -22.57%
BenchmarkGCD100x1000             11820         9446  -20.08%
BenchmarkGCD100x10000            23846        21382  -10.33%
BenchmarkGCD100x100000          133691       131505   -1.64%
BenchmarkGCD1000x1000           120041        95591  -20.37%
BenchmarkGCD1000x10000          136887       113600  -17.01%
BenchmarkGCD1000x100000         295370       273912   -7.26%
BenchmarkGCD10000x10000        2556126      2205198  -13.73%
BenchmarkGCD10000x100000       3159512      2808038  -11.12%
BenchmarkGCD100000x100000    150543094    139986045   -7.01%

R=gri, remyoudompheng
CC=bradfitz, gobot, golang-dev, gri
https://golang.org/cl/9424043
2013-05-15 10:03:22 -04:00
Brad Fitzpatrick
e9546a01dc math/rand: fix typo and add better crash message
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/9000043
2013-04-27 18:50:38 -07:00
Robert Griesemer
1b37ba931f math/big: minor cleanups
- comment fixes
- s/z/x/ in (*rat).Float64 to match convention for functions
  returning a non-*Rat
- minor test output tweaking

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/8327044
2013-04-03 13:24:32 -07:00
Russ Cox
60a1f54d6b math: make Hypot special cases look like others
The edit makes Hypot's description match the form
used in the other routines in this package.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/8003046
2013-03-25 17:01:40 -04:00
Oling Cat
1693e14bc4 math: modify a comment to the convention format.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/8012043
2013-03-25 08:43:51 -07:00
Russ Cox
f6d0adedb6 build: remove dead code
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7877045
2013-03-22 17:28:22 -04:00
Russ Cox
07720b67b3 build: update assembly variable names for vet
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7834046
2013-03-22 12:57:55 -04:00
Oling Cat
79ae1ad489 math: remove repeated comment.
R=golang-dev, nigeltao
CC=golang-dev
https://golang.org/cl/7835046
2013-03-22 14:54:20 +11:00
Brad Fitzpatrick
e15c0ac693 all: remove now-unnecessary unreachable panics
Take advantage of the new terminating statement rule.

R=golang-dev, r, gri
CC=golang-dev
https://golang.org/cl/7712044
2013-03-11 14:16:55 -07:00
Russ Cox
65ed3f1073 math/big: fix comment
The variable is named b not bit.

R=golang-dev, agl
CC=golang-dev
https://golang.org/cl/7699043
2013-03-11 10:20:33 -04:00
Rob Pike
1bf66f081f all: fix a few more printf arg bugs found by go vet
R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/7413045
2013-02-28 11:33:08 -08:00
Alan Donovan
19bb42d637 math/big: make tests faster by reducing coverage in --test.short mode.
The time to test all of math/big is now:
 default      => ~3min
 --test.short => 150ms

R=rsc
CC=golang-dev
https://golang.org/cl/7223054
2013-01-30 18:19:58 -05:00
Alan Donovan
961195ae69 math/big: add Rat.{,Set}Float64 methods for IEEE 754 conversions.
Added tests, using input data from strconv.ParseFloat.
Thanks to rsc for most of the test code.

math/big could use some good package-level documentation.

R=remyoudompheng, rsc
CC=golang-dev
https://golang.org/cl/6930059
2013-01-28 18:00:15 -05:00
Russ Cox
92b2643c92 math/big: fix typo
Fixes #4678.

TBR=gri
CC=golang-dev
https://golang.org/cl/7135059
2013-01-18 17:30:34 -05:00
Matthew Dempsky
46811d27ce src: Use bytes.Equal instead of bytes.Compare where possible.
bytes.Equal is simpler to read and should also be faster because
of short-circuiting and assembly implementations.

Change generated automatically using:
  gofmt -r 'bytes.Compare(a, b) == 0 -> bytes.Equal(a, b)'
  gofmt -r 'bytes.Compare(a, b) != 0 -> !bytes.Equal(a, b)'

R=golang-dev, dave, adg, rsc
CC=golang-dev
https://golang.org/cl/7038051
2013-01-07 10:03:49 +11:00
Russ Cox
a3677b5f22 math: handle exponent separately in Log2
This guarantees that powers of two return exact answers.

We could do a multiprecision approximation for the
rest of the answer too, but this seems like it should be
good enough.

Fixes #4567.

R=golang-dev, iant, remyoudompheng
CC=golang-dev
https://golang.org/cl/6943074
2012-12-20 12:23:27 -05:00
Shenghou Ma
d1ef9b56fb all: fix typos
caught by https://github.com/lyda/misspell-check.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/6949072
2012-12-19 03:04:09 +08:00
Luit van Drongelen
f4fc163d17 math/big: add SetUint64 and Uint64 functions to *Int
Implementation is mostly identical to passing a non-negative int64 to
SetInt64, and calling Int64 with a non-negative value in the *Int.
Fixes #4389.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6929048
2012-12-11 12:19:10 -05:00
David Symonds
ec45952670 math/rand: fix typo in example comment.
R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/6901056
2012-12-08 19:20:38 +11:00
Russ Cox
b99161e41f math/rand: add example / regression test
This makes sure the outputs do not change for a fixed seed.
See also https://golang.org/cl/6905049.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6907048
2012-12-07 11:58:59 -05:00
Russ Cox
ff2534e076 undo CL 6845121 / 79603a5e4cda
This changes the output of

rand.Seed(0)
perm := rand.Perm(100)

When giving the same seeds to Go 1.0 and Go 1.1 programs
I would like them to generate the same random numbers.

««« original CL description
math/rand: remove noop iteration in Perm

The first iteration always do `m[0], m[0] = m[0], m[0]`, because
`rand.Intn(1)` is 0.

fun note: IIRC in TAOCP version of this algorithm, `i` goes
backward (n-1->1), meaning that the "already" shuffled part of the
array is never altered betweens iterations, while in the current
implementation the "not-yet" shuffled part of the array is
conserved between iterations.

R=golang-dev
CC=golang-dev
https://golang.org/cl/6845121

»»»

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6905049
2012-12-07 11:58:44 -05:00
Johan Euphrosine
d7b0f2a524 math/rand: remove noop iteration in Perm
The first iteration always do `m[0], m[0] = m[0], m[0]`, because
`rand.Intn(1)` is 0.

fun note: IIRC in TAOCP version of this algorithm, `i` goes
backward (n-1->1), meaning that the "already" shuffled part of the
array is never altered betweens iterations, while in the current
implementation the "not-yet" shuffled part of the array is
conserved between iterations.

R=golang-dev
CC=golang-dev
https://golang.org/cl/6845121
2012-12-01 14:11:46 -08:00
Shenghou Ma
9e30b708a1 all: set GOMAXPROCS to 1 when counting mallocs
also fix an annoying test that relies on $GOROOT be set.
Fixes #3690.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6844086
2012-12-01 00:38:01 +08:00
Robert Griesemer
465b9c35e5 gofmt: apply gofmt -w src misc
Remove trailing whitespace in comments.
No other changes.

R=r
CC=golang-dev
https://golang.org/cl/6815053
2012-10-30 13:38:01 -07:00
Adam Langley
73f11171b4 math/big: add 4-bit, fixed window exponentiation.
A 4-bit window is convenient because 4 divides both 32 and 64,
therefore we never have a window spanning words of the exponent.
Additionaly, the benefit of a 5-bit window is only 2.6% at 1024-bits
and 3.3% at 2048-bits.

This code is still not constant time, however.

benchmark                        old ns/op    new ns/op    delta
BenchmarkRSA2048Decrypt           17108590     11180370  -34.65%
Benchmark3PrimeRSA2048Decrypt     13003720      7680390  -40.94%

R=gri
CC=golang-dev
https://golang.org/cl/6716048
2012-10-17 11:19:26 -04:00
Robert Griesemer
7565726875 math/big: fix big.Exp and document better
- always return 1 for y <= 0
- document that the sign of m is ignored
- protect against div-0 panics by treating
  m == 0 the same way as m == nil
- added extra tests

Fixes #4239.

R=agl, remyoudompheng, agl
CC=golang-dev
https://golang.org/cl/6724046
2012-10-16 13:46:27 -07:00
Robert Griesemer
4bee88d45f math/big: more conservative use of lock for divisor table
Minor performance impact running sequentially:

benchmark                      old ns/op    new ns/op    delta
BenchmarkString10Base2               389          391   +0.51%
BenchmarkString100Base2             1530         1534   +0.26%
BenchmarkString1000Base2           11789        11787   -0.02%
BenchmarkString10000Base2         111443       112030   +0.53%
BenchmarkString100000Base2       1017483      1015347   -0.21%
BenchmarkString10Base8               339          344   +1.47%
BenchmarkString100Base8              753          756   +0.40%
BenchmarkString1000Base8            4618         4641   +0.50%
BenchmarkString10000Base8          43217        43534   +0.73%
BenchmarkString100000Base8        397518       400602   +0.78%
BenchmarkString10Base10              630          630   +0.00%
BenchmarkString100Base10            1975         1960   -0.76%
BenchmarkString1000Base10          10179        10174   -0.05%
BenchmarkString10000Base10         44527        44416   -0.25%
BenchmarkString100000Base10     14404694     14425308   +0.14%
BenchmarkString10Base16              283          288   +1.77%
BenchmarkString100Base16             597          598   +0.17%
BenchmarkString1000Base16           3189         3186   -0.09%
BenchmarkString10000Base16         29403        29364   -0.13%
BenchmarkString100000Base16       265657       265587   -0.03%

Note that due to other improvements (faster assembly routines,
better code generation by compiler), these benchmarks now run
up to 37% faster than they used to at the last time measured (1/9/2012).

Minor performance impact for StringPiParallel running in parallel:

Current CL but with Lock/Unlock commented out (removed):

BenchmarkStringPiParallel	    5000	    343581 ns/op
BenchmarkStringPiParallel-2	   10000	    184511 ns/op
BenchmarkStringPiParallel-3	   10000	    129768 ns/op
BenchmarkStringPiParallel-4	   10000	    102326 ns/op

Current CL:

BenchmarkStringPiParallel	    5000	    345169 ns/op
BenchmarkStringPiParallel-2	   10000	    185827 ns/op
BenchmarkStringPiParallel-3	   10000	    131168 ns/op
BenchmarkStringPiParallel-4	   10000	    102353 ns/op

Fixes #4218.

R=dvyukov, michael.jones, dave
CC=golang-dev
https://golang.org/cl/6643053
2012-10-11 16:04:03 -07:00
Adam Langley
9070d5759f math/big: avoid some allocation in Exp
benchmark                        old ns/op    new ns/op    delta
BenchmarkRSA1024Decrypt             745686       644964  -13.51%
BenchmarkRSA2048Decrypt            5517318      5049200   -8.48%
Benchmark3PrimeRSA2048Decrypt      3767386      3288048  -12.72%

R=gri
CC=gobot, golang-dev
https://golang.org/cl/6566043
2012-10-01 17:31:35 -04:00
Russ Cox
10ea6519e4 build: make int 64 bits on amd64
The assembly offsets were converted mechanically using
code.google.com/p/rsc/cmd/asmlint. The instruction
changes were done by hand.

Fixes #2188.

R=iant, r, bradfitz, remyoudompheng
CC=golang-dev
https://golang.org/cl/6550058
2012-09-24 20:57:01 -04:00
Russ Cox
8572950383 all: match asm variable names to Go func declarations
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6548046
2012-09-21 00:35:56 -04:00
Charles L. Dorian
0f8f5d2140 math: Faster Tanh
From 159 to 47.6 ns/op; slightly more accurate.

R=rsc, golang-dev, mtj, dave, remyoudompheng
CC=golang-dev
https://golang.org/cl/6500121
2012-09-17 17:18:16 -04:00
Christopher Swenson
baf426f10f math/big: Replace RCLQ + ANDQ with SETCS in unrolled arithmetic assembly.
benchmark             old ns/op    new ns/op    delta
BenchmarkAddVW_1              8            8   +0.60%
BenchmarkAddVW_2             10            9   -8.64%
BenchmarkAddVW_3             10           10   -4.63%
BenchmarkAddVW_4             10           11   +3.67%
BenchmarkAddVW_5             11           12   +5.98%
BenchmarkAddVW_1e1           18           20   +6.38%
BenchmarkAddVW_1e2          129          115  -10.85%
BenchmarkAddVW_1e3         1270         1089  -14.25%
BenchmarkAddVW_1e4        13376        12145   -9.20%
BenchmarkAddVW_1e5       130392       125260   -3.94%

benchmark              old MB/s     new MB/s  speedup
BenchmarkAddVW_1        7709.10      7661.92    0.99x
BenchmarkAddVW_2       12451.10     13604.00    1.09x
BenchmarkAddVW_3       17727.81     18721.54    1.06x
BenchmarkAddVW_4       23552.64     22708.81    0.96x
BenchmarkAddVW_5       27411.40     25816.22    0.94x
BenchmarkAddVW_1e1     34063.19     32023.06    0.94x
BenchmarkAddVW_1e2     49529.97     55360.55    1.12x
BenchmarkAddVW_1e3     50380.44     58764.18    1.17x
BenchmarkAddVW_1e4     47843.59     52696.10    1.10x
BenchmarkAddVW_1e5     49082.60     51093.66    1.04x

R=gri, rsc, r
CC=golang-dev
https://golang.org/cl/6480063
2012-08-28 09:29:45 -07:00