Carp/core/Double.carp
Scott Olsen 5d530b7491
feat: register MAX and MIN macros for stdint types (#1412)
* feat: register MAX and MIN macros for stdint types

Adds MAX and MIN for each INT<N> type and MAX for each UINT<N> type.
These are macros defined by stdint.h and are sometimes useful for bounds
determinations and conversions and such.

* feat: make MAX and MIN interfaces

Since several numeric types define maximum and minimum values, it makes
sense for these to be defined as interfaces. This commit also makes
existing definitions of MAX and MIN for Carp's numeric types implement
the interfaces.

* fix: respect let binding shadowing in memory management (#1413)

* fix: respect let binding shadowing in memory management

Previously, we didn't account for shadowing in let bindings in our
memory management routines. This led to rare situations in which
multiple deleters might be added for a single variable name, for
example:

```clojure
(defn n [xs]
  (let [xs [1 2 3]
        n &xs]
    n))
```

The borrow checker would fail on this code since it would assign `xs`
two deleters, one for the untyped argument and another for the let
binding.

Instead, we now perform *exclusive* ownership transfer for the duration
of the let scope--when a shadow is introduced, the previous deleters for
that variable name are dropped until the end of the let scope, since we
evaluate all instances of the shadowed name to the more local binding.
At the end of the let scope, the original deleter is restored.

Fixes issue #597

* refactor: improved dead reference error for let

Since let scopes resolve to their bodies, we can report the body of the
let as the xobj producing an error when a dead reference is returned.

* test: update error message for dead refs in let

* test: add regression test for issue #597

Ensure we don't regress and fail to manage memory when let bindings
shadow function argument names.

* fix: respect symbol modes on interface concretization (#1415)

* fix: respect symbol modes on interface concretization

When concretizing interfaces (finding the appropriate implementation at
a call site) we previously set the lookup mode of all such resolved
symbols to CarpLand AFunction. This incorrectly overwrites the lookup
mode of Externally registered types, causing them to emit incorrect C
when the user specifies an override.

We now preserve whatever lookup mode is assigned to the implementation
the concretization resolves the interface to. This not only fixes the
external override emission issue, but should be more correct in general.

fixes #1414

* test: add regression test for issue #1414
2022-04-13 09:32:46 +02:00

122 lines
3.5 KiB
Plaintext

(system-include "carp_double.h")
(doc Double "is the default floating point number type.")
(defmodule Double
(def pi 3.141592653589793)
(implements pi Double.pi)
(def e 2.718281828459045)
(register MAX Double "CARP_DBL_MAX")
(implements MAX MAX)
(register = (Fn [Double Double] Bool))
(register < (Fn [Double Double] Bool))
(register > (Fn [Double Double] Bool))
(register neg (Fn [Double] Double))
(register + (Fn [Double Double] Double))
(register - (Fn [Double Double] Double))
(register * (Fn [Double Double] Double))
(register / (Fn [Double Double] Double))
(register to-int (Fn [Double] Int))
(register from-int (Fn [Int] Double))
(register to-float (Fn [Double] Float))
(register from-float (Fn [Float] Double))
(register to-long (Fn [Double] Long))
(register from-long (Fn [Long] Double))
(register to-bytes (Fn [Double] Long))
(register copy (Fn [(Ref Double)] Double))
(implements + Double.+)
(implements - Double.-)
(implements * Double.*)
(implements / Double./)
(implements < Double.<)
(implements > Double.>)
(implements = Double.=)
(implements copy Double.copy)
(implements neg Double.neg)
(implements to-int Double.to-int)
(implements from-int Double.from-int)
(register abs (Fn [Double] Double))
(register acos (Fn [Double] Double))
(register asin (Fn [Double] Double))
(register atan (Fn [Double] Double))
(register atan2 (Fn [Double Double] Double))
(register ceil (Fn [Double] Double))
(register cos (Fn [Double] Double))
(register cosh (Fn [Double] Double))
(register exp (Fn [Double] Double))
(register floor (Fn [Double] Double))
(register frexp (Fn [Double (Ref Int)] Double))
(register ldexp (Fn [Double Int] Double))
(register log (Fn [Double] Double))
(register log10 (Fn [Double] Double))
(register mod (Fn [Double Double] Double))
(register modf (Fn [Double (Ref Double)] Double))
(register pow (Fn [Double Double] Double))
(register sin (Fn [Double] Double))
(register sinh (Fn [Double] Double))
(register sqrt (Fn [Double] Double))
(register tan (Fn [Double] Double))
(register tanh (Fn [Double] Double))
(implements abs Double.abs)
(implements acos Double.acos)
(implements asin Double.asin)
(implements atan Double.atan)
(implements atan2 Double.atan2)
(implements ceil Double.ceil)
(implements cos Double.cos)
(implements cosh Double.cosh)
(implements exp Double.exp)
(implements floor Double.floor)
(implements frexp Double.frexp)
(implements ldexp Double.ldexp)
(implements log Double.log)
(implements log10 Double.log10)
(implements mod Double.mod)
(implements modf Double.modf)
(implements pow Double.pow)
(implements sin Double.sin)
(implements sinh Double.sinh)
(implements sqrt Double.sqrt)
(implements tan Double.tan)
(implements tanh Double.tanh)
(doc approx "checks whether `x` and `y` are approximately equal.
The margin of error is `0.00001`.")
(defn approx [x y]
(Generics.approx x y))
(doc zero "returns the value `0.0`.")
(defn zero []
0.0)
(implements zero Double.zero)
(defn inc [x]
(+ 1.0 x))
(implements inc Double.inc)
(defn dec [x]
(- x 1.0))
(implements dec Double.dec)
(defn add-ref [x y]
(Double.+ @x @y))
(implements add-ref Double.add-ref)
)
(defmodule DoubleRef
(defn = [a b]
(Double.= @a @b))
(implements = DoubleRef.=)
(defn < [a b]
(Double.< @a @b))
(implements < DoubleRef.<)
(defn > [a b]
(Double.> @a @b))
(implements > DoubleRef.>)
)