Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 32f583fbf091
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c4b27d4b369a
Choose a head ref
  • 3 commits
  • 17 files changed
  • 1 contributor

Commits on Jun 4, 2017

  1. Copy the full SHA
    4d07680 View commit details
  2. Char: fixed upcase docs

    asterite committed Jun 4, 2017
    Copy the full SHA
    09a301e View commit details
  3. 1
    Copy the full SHA
    c4b27d4 View commit details
22 changes: 11 additions & 11 deletions spec/std/float_printer/diy_fp_spec.cr
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "spec"
require "float_printer/diy_fp"
include FloatPrinter

private alias DiyFP = Float::Printer::DiyFP

describe DiyFP do
it "multiply" do
@@ -78,7 +78,7 @@ describe DiyFP do

it "converts ordered 64" do
ordered = 0x0123456789ABCDEF_u64
f = pointerof(ordered).as(Float64*).value
f = ordered.unsafe_as(Float64)
f.should eq 3512700564088504e-318 # ensure byte order

fp = DiyFP.from_f(f)
@@ -90,7 +90,7 @@ describe DiyFP do

it "converts ordered 32" do
ordered = 0x01234567_u32
f = pointerof(ordered).as(Float32*).value
f = ordered.unsafe_as(Float32)
f.should eq(2.9988165487136453e-38_f32)

fp = DiyFP.from_f(f)
@@ -103,7 +103,7 @@ describe DiyFP do

it "converts min f64" do
min = 0x0000000000000001_u64
f = pointerof(min).as(Float64*).value
f = min.unsafe_as(Float64)
f.should eq 5e-324 # ensure byte order

fp = DiyFP.from_f(f)
@@ -115,7 +115,7 @@ describe DiyFP do

it "converts min f32" do
min = 0x00000001_u32
f = pointerof(min).as(Float32*).value
f = min.unsafe_as(Float32)
fp = DiyFP.from_f(f)

fp.exp.should eq -0x7F - 23 + 1
@@ -125,7 +125,7 @@ describe DiyFP do

it "converts max f64" do
max = 0x7fefffffffffffff_u64
f = pointerof(max).as(Float64*).value
f = max.unsafe_as(Float64)
f.should eq 1.7976931348623157e308 # ensure byte order

fp = DiyFP.from_f(f)
@@ -136,7 +136,7 @@ describe DiyFP do

it "converts max f32" do
max = 0x7f7fffff_u64
f = pointerof(max).as(Float32*).value
f = max.unsafe_as(Float32)
f.should eq 3.4028234e38_f32 # ensure byte order

fp = DiyFP.from_f(f)
@@ -147,7 +147,7 @@ describe DiyFP do

it "normalizes ordered" do
ordered = 0x0123456789ABCDEF_u64
f = pointerof(ordered).as(Float64*).value
f = ordered.unsafe_as(Float64)

fp = DiyFP.from_f_normalized(f)

@@ -157,7 +157,7 @@ describe DiyFP do

it "normalizes min f64" do
min = 0x0000000000000001_u64
f = pointerof(min).as(Float64*).value
f = min.unsafe_as(Float64)

fp = DiyFP.from_f_normalized(f)

@@ -168,7 +168,7 @@ describe DiyFP do

it "normalizes max f64" do
max = 0x7fefffffffffffff_u64
f = pointerof(max).as(Float64*).value
f = max.unsafe_as(Float64)

fp = DiyFP.from_f_normalized(f)

8 changes: 3 additions & 5 deletions spec/std/float_printer/grisu3_spec.cr
Original file line number Diff line number Diff line change
@@ -27,20 +27,18 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "spec"
require "float_printer/grisu3"
include FloatPrinter

private def test_grisu(v : UInt64)
test_grisu pointerof(v).as(Float64*).value
test_grisu v.unsafe_as(Float64)
end

private def test_grisu(v : UInt32)
test_grisu pointerof(v).as(Float32*).value
test_grisu v.unsafe_as(Float32)
end

private def test_grisu(v : Float64 | Float32)
buffer = StaticArray(UInt8, 128).new(0_u8)
status, decimal_exponent, length = Grisu3.grisu3(v, buffer.to_unsafe)
status, decimal_exponent, length = Float::Printer::Grisu3.grisu3(v, buffer.to_unsafe)
point = decimal_exponent + length
return status, point, String.new(buffer.to_unsafe)
end
11 changes: 4 additions & 7 deletions spec/std/float_printer/ieee_spec.cr
Original file line number Diff line number Diff line change
@@ -27,23 +27,20 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "spec"
require "float_printer/diy_fp"
require "float_printer/ieee"
include FloatPrinter

private def gen_bound(v : UInt64)
f = pointerof(v).as(Float64*).value
f = v.unsafe_as(Float64)
gen_bound(f)
end

private def gen_bound(v : UInt32)
f = pointerof(v).as(Float32*).value
f = v.unsafe_as(Float32)
gen_bound(f)
end

private def gen_bound(v : Float64 | Float32)
fp = DiyFP.from_f_normalized(v)
b = IEEE.normalized_boundaries(v)
fp = Float::Printer::DiyFP.from_f_normalized(v)
b = Float::Printer::IEEE.normalized_boundaries(v)
b[:minus].exp.should eq fp.exp
b[:plus].exp.should eq fp.exp

8 changes: 3 additions & 5 deletions spec/std/float_printer_spec.cr
Original file line number Diff line number Diff line change
@@ -27,12 +27,10 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "spec"
require "float_printer"
include FloatPrinter

private def float_to_s(v)
String.build(22) do |buff|
FloatPrinter.print(v, buff)
Float::Printer.print(v, buff)
end
end

@@ -43,12 +41,12 @@ private def test_str(s, file = __FILE__, line = __LINE__)
end

private def test_pair(v : UInt64, str, file = __FILE__, line = __LINE__)
d = pointerof(v).as(Float64*).value
d = v.unsafe_as(Float64)
test_pair(d, str, file, line)
end

private def test_pair(v : UInt32, str, file = __FILE__, line = __LINE__)
d = pointerof(v).as(Float32*).value
d = v.unsafe_as(Float32)
test_pair(d, str, file, line)
end

4 changes: 4 additions & 0 deletions spec/std/object_spec.cr
Original file line number Diff line number Diff line change
@@ -333,4 +333,8 @@ describe Object do
obj.property10?.should be_false
end
end

it "#unsafe_as" do
0x12345678.unsafe_as(Tuple(UInt8, UInt8, UInt8, UInt8)).should eq({0x78, 0x56, 0x34, 0x12})
end
end
2 changes: 1 addition & 1 deletion src/char.cr
Original file line number Diff line number Diff line change
@@ -408,7 +408,7 @@ struct Char
#
# ```
# 'z'.upcase { |v| puts v } # prints 'Z'
# 'ffl'.upcase { |v| puts v } # prints 'F', 'F', 'F'
# 'ffl'.upcase { |v| puts v } # prints 'F', 'F', 'L'
# ```
def upcase(options = Unicode::CaseOptions::None)
Unicode.upcase(self, options) { |char| yield char }
16 changes: 7 additions & 9 deletions src/float.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "c/stdio"
require "c/string"
require "float_printer"
require "./float/printer"

# Float is the base type of all floating point numbers.
#
@@ -140,17 +140,16 @@ struct Float32

def to_s
String.build(22) do |buffer|
FloatPrinter.print(self, buffer)
Printer.print(self, buffer)
end
end

def to_s(io : IO)
FloatPrinter.print(self, io)
Printer.print(self, io)
end

def hash
n = self
pointerof(n).as(Int32*).value
unsafe_as(Int32)
end

def clone
@@ -199,17 +198,16 @@ struct Float64

def to_s
String.build(22) do |buffer|
FloatPrinter.print(self, buffer)
Printer.print(self, buffer)
end
end

def to_s(io : IO)
FloatPrinter.print(self, io)
Printer.print(self, io)
end

def hash
n = self
pointerof(n).as(Int64*).value
unsafe_as(Int64)
end

def clone
6 changes: 3 additions & 3 deletions src/float_printer.cr → src/float/printer.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "float_printer/*"
require "./printer/*"

# FloatPrinter is based on Grisu3 algorithm described in the 2004 paper
# Float::Printer is based on Grisu3 algorithm described in the 2004 paper
# "Printing Floating-Point Numbers Quickly and Accurately with Integers" by
# Florian Loitsch.
module FloatPrinter
module Float::Printer
extend self
BUFFER_SIZE = 128

Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

module FloatPrinter::CachedPowers
module Float::Printer::CachedPowers
record Power, significand : UInt64, binary_exp : Int16, decimal_exp : Int16
# The minimal and maximal target exponent define the range of w's binary
# exponent, where 'w' is the result of multiplying the input by a cached power
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ require "./ieee"
# have the most significant bit of the significand set.
# Multiplication and Subtraction do not normalize their results.
# DiyFP is not designed to contain special Floats (NaN and Infinity).
struct FloatPrinter::DiyFP
struct Float::Printer::DiyFP
SIGNIFICAND_SIZE = 64
# Also known as the significand
property frac : UInt64
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ require "./diy_fp"
require "./ieee"
require "./cached_powers"

module FloatPrinter::Grisu3
module Float::Printer::Grisu3
extend self

# Adjusts the last digit of the generated number, and screens out generated
6 changes: 3 additions & 3 deletions src/float_printer/ieee.cr → src/float/printer/ieee.cr
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

module FloatPrinter::IEEE
module Float::Printer::IEEE
extend self

EXPONENT_MASK_64 = 0x7FF0000000000000_u64
@@ -49,11 +49,11 @@ module FloatPrinter::IEEE
SIGN_MASK_32 = 0x80000000_u32

def to_uint(v : Float64)
pointerof(v).as(UInt64*).value
v.unsafe_as(UInt64)
end

def to_uint(v : Float32)
pointerof(v).as(UInt32*).value
v.unsafe_as(UInt32)
end

def sign(d64 : UInt64)
2 changes: 1 addition & 1 deletion src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ class HTTP::WebSocket::Protocol
return @io.write(data) unless @masked

key = Random::DEFAULT.next_int
mask_array = pointerof(key).as(Pointer(UInt8[4])).value
mask_array = key.unsafe_as(StaticArray(UInt8, 4))
@io.write mask_array.to_slice

data.each_with_index do |byte, index|
24 changes: 10 additions & 14 deletions src/io/byte_format.cr
Original file line number Diff line number Diff line change
@@ -78,39 +78,35 @@ module IO::ByteFormat
abstract def decode(int : Float64.class, bytes : Bytes)

def encode(float : Float32, io : IO)
encode(pointerof(float).as(Int32*).value, io)
encode(float.unsafe_as(Int32), io)
end

def encode(float : Float32, bytes : Bytes)
encode(pointerof(float).as(Int32*).value, bytes)
encode(float.unsafe_as(Int32), bytes)
end

def decode(type : Float32.class, io : IO)
int = decode(Int32, io)
pointerof(int).as(Float32*).value
decode(Int32, io).unsafe_as(Float32)
end

def decode(type : Float32.class, bytes : Bytes)
int = decode(Int32, bytes)
pointerof(int).as(Float32*).value
decode(Int32, bytes).unsafe_as(Float32)
end

def encode(float : Float64, io : IO)
encode(pointerof(float).as(Int64*).value, io)
encode(float.unsafe_as(Int64), io)
end

def encode(float : Float64, bytes : Bytes)
encode(pointerof(float).as(Int64*).value, bytes)
encode(float.unsafe_as(Int64), bytes)
end

def decode(type : Float64.class, io : IO)
int = decode(Int64, io)
pointerof(int).as(Float64*).value
decode(Int64, io).unsafe_as(Float64)
end

def decode(type : Float64.class, bytes : Bytes)
int = decode(Int64, bytes)
pointerof(int).as(Float64*).value
decode(Int64, bytes).unsafe_as(Float64)
end

module LittleEndian
@@ -130,13 +126,13 @@ module IO::ByteFormat
{% bytesize = 2 ** (i / 2) %}

def self.encode(int : {{type.id}}, io : IO)
buffer = pointerof(int).as(UInt8[{{bytesize}}]*).value
buffer = int.unsafe_as(StaticArray(UInt8, {{bytesize}}))
buffer.reverse! unless SystemEndian == self
io.write(buffer.to_slice)
end

def self.encode(int : {{type.id}}, bytes : Bytes)
buffer = pointerof(int).as(UInt8[{{bytesize}}]*).value
buffer = int.unsafe_as(StaticArray(UInt8, {{bytesize}}))
buffer.reverse! unless SystemEndian == self
buffer.to_slice.copy_to(bytes)
end
Loading