crmf 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d95e161c04b6b356d3b113b0a04dabdd3238473f334403e22dd200e02fb281e4
4
- data.tar.gz: f3e1eff950fbf83934d7894a112fa71082f6cbf2ef5f6a801c33db91d1b223e4
3
+ metadata.gz: 0c1ba34478ac7afadfbf114facc6a2e66a4f4971808a7d011a0f8e06e5a63b82
4
+ data.tar.gz: 6ecb3706b3455712ce0e36c0acafb08e53f684f911f4abdef45cb34213aba467
5
5
  SHA512:
6
- metadata.gz: e0394d2ac1dd08c2f1fff073775610af15bd8bd0f80c153789480b09d2cff761a02155b00f297a6c8a3339694b2c8b1ed402c197fdc7817417912d1c04027099
7
- data.tar.gz: 6b3c25abf966f2478701697b2f01d04ef68b00b5a8511132e364c0dfaa8a9c19e01e5e7eee7c50f7eccbe8151b47b87b873ad04a79e484ef3cab41731e055478
6
+ metadata.gz: 1b2d653721b7d7b05a0d3e0e43e12118bf9df99a4e589b960407008daee0967974185685e25a4dc07f1da34af40069e52ba53b8e92f5d8ccff5801d1f0644855
7
+ data.tar.gz: 3b73d46188653d99613b2808e293b5e21ff1f7a78ee2a58eb4a6d4606ff4d9f9587741d0edaa4678d5081ef9a757a2d543266c9ec95ea829c81b275f314572e7
data/crmf.gemspec CHANGED
@@ -6,10 +6,11 @@ Gem::Specification.new do |s|
6
6
  s.date = Time.now.strftime '%Y-%m-%d'
7
7
  s.summary = 'Correctly rounded math functions for Ruby floats'
8
8
  s.description = 'CRMF is a Ruby C extension which provides correctly rounded math functions for Ruby floats, using MPFR and CRlibm. Provided rounding modes are toward zero, +infinity, -infinity a to nearest even.'
9
- s.license = 'GPL-3.0+'
9
+ s.license = 'GPL-3.0-or-later'
10
10
  s.authors = ['Théotime Bollengier']
11
- s.email = 'theotime.bollengier@ensta-bretagne.fr'
11
+ s.email = 'theotime.bollengier@ensta.fr'
12
12
  s.homepage = 'https://gitlab.ensta-bretagne.fr/bollenth/crmf'
13
+ s.required_ruby_version = '>= 2.7'
13
14
  s.extensions = ['ext/crmf/extconf.rb']
14
15
  s.files = [
15
16
  'LICENSE',
data/ext/crmf/crmf.c CHANGED
@@ -1,4 +1,4 @@
1
- /* Copyright (C) 2022 Théotime Bollengier <theotime.bollengier@ensta-bretagne.fr>
1
+ /* Copyright (C) 2022,2025 Théotime Bollengier <theotime.bollengier@ensta.fr>
2
2
  *
3
3
  * This file is part of CRMF. <https://gitlab.ensta-bretagne.fr/bollenth/crmf>
4
4
  *
@@ -1736,8 +1736,100 @@ static VALUE crmf_div_rn(VALUE self, VALUE x, VALUE y)
1736
1736
  }
1737
1737
 
1738
1738
 
1739
+ /* Round a Float to a binary32 in the direction _rnd_.
1740
+ * @return [Float]
1741
+ * @overload to_f32(rnd = :nearest)
1742
+ * @param rnd [Symbol] `:nearest`, `:down`, `:up`, `:zero` or `:away`
1743
+ */
1744
+ static VALUE crmf_float_to_f32(int argc, VALUE *argv, VALUE self)
1745
+ {
1746
+ VALUE rnd = Qnil;
1747
+ mpfr_rnd_t rndt = MPFR_RNDN;
1748
+ rb_scan_args(argc, argv, "01", &rnd);
1749
+ if (rnd != Qnil) {
1750
+ rndt = crmf_sym_to_rnd(rnd);
1751
+ }
1752
+ MPFR_DECL_INIT(mp, 53);
1753
+ mpfr_set_d(mp, NUM2DBL(self), rndt);
1754
+ float f = mpfr_get_flt(mp, rndt);
1755
+ return DBL2NUM((double)f);
1756
+ }
1757
+
1758
+
1759
+ /* Interpret bits (from a 32-bit integer) as a binary32 floating point number.
1760
+ * @param bits [Integer]
1761
+ * @return [Float]
1762
+ */
1763
+ static VALUE crmf_float_bits_to_f32(VALUE self, VALUE bits)
1764
+ {
1765
+ uint32_t bts = NUM2ULL(bits);
1766
+ union {
1767
+ float f;
1768
+ uint32_t u;
1769
+ } fu = {.u=bts};
1770
+ return DBL2NUM((double)fu.f);
1771
+ }
1772
+
1773
+
1774
+ /* Interpret bits (from a 64-bit integer) as a binary64 floating point number.
1775
+ * @param bits [Integer]
1776
+ * @return [Float]
1777
+ */
1778
+ static VALUE crmf_float_bits_to_f64(VALUE self, VALUE bits)
1779
+ {
1780
+ uint64_t bts = NUM2ULL(bits);
1781
+ union {
1782
+ double d;
1783
+ uint64_t u;
1784
+ } du = {.u=bts};
1785
+ return DBL2NUM(du.d);
1786
+ }
1787
+
1788
+
1789
+ /* Return the bits (as a 64-bit integer) constituting the binary64 floating point number _f_.
1790
+ * @param f [Float]
1791
+ * @return [Integer]
1792
+ */
1793
+ static VALUE crmf_float_f64_to_bits(VALUE self, VALUE f)
1794
+ {
1795
+ double d = NUM2DBL(f);
1796
+ union {
1797
+ double d;
1798
+ uint64_t u;
1799
+ } du = {.d=d};
1800
+ return ULL2NUM(du.u);
1801
+ }
1802
+
1803
+
1804
+ /* Converts the binary64 floating point number _f_ to a binary32 floating point in the direction _rnd_,
1805
+ * then returns the bits (as a 32-bit integer) constituting this 32-bit floating-point number.
1806
+ * @return [Integer]
1807
+ * @overload f32_to_bits(f, rnd = :nearest)
1808
+ * @param f [Float]
1809
+ * @param rnd [Symbol] `:nearest`, `:down`, `:up`, `:zero` or `:away`
1810
+ */
1811
+ static VALUE crmf_float_f32_to_bits(int argc, VALUE *argv, VALUE self)
1812
+ {
1813
+ VALUE f;
1814
+ VALUE rnd = Qnil;
1815
+ mpfr_rnd_t rndt = MPFR_RNDN;
1816
+ rb_scan_args(argc, argv, "11", &f, &rnd);
1817
+ if (rnd != Qnil) {
1818
+ rndt = crmf_sym_to_rnd(rnd);
1819
+ }
1820
+ double d = NUM2DBL(f);
1821
+ MPFR_DECL_INIT(mp, 53);
1822
+ mpfr_set_d(mp, d, rndt);
1823
+ float flt = mpfr_get_flt(mp, rndt);
1824
+ union {
1825
+ float f;
1826
+ uint32_t u;
1827
+ } fu = {.f=flt};
1828
+ return UINT2NUM(fu.u);
1829
+ }
1830
+
1739
1831
 
1740
- CRMF_PUBLIC void Init_crmf()
1832
+ CRMF_PUBLIC void Init_crmf(void)
1741
1833
  {
1742
1834
  id_to_f = rb_intern("to_f");
1743
1835
  id_to_s = rb_intern("to_s");
@@ -1802,11 +1894,16 @@ CRMF_PUBLIC void Init_crmf()
1802
1894
  rb_define_module_function(rb_cFloat, "asinh", crmf_float_asinh, -1);
1803
1895
  rb_define_module_function(rb_cFloat, "atanh", crmf_float_atanh, -1);
1804
1896
 
1805
- rb_define_module_function(m_CRMF, "to_float", crmf_object_to_float, -1);
1806
- rb_define_module_function(rb_cFloat, "build_up", crmf_float_build_up, 3);
1897
+ rb_define_module_function(m_CRMF, "to_float", crmf_object_to_float, -1);
1898
+ rb_define_module_function(rb_cFloat, "build_up", crmf_float_build_up, 3);
1899
+ rb_define_module_function(rb_cFloat, "bits_to_f32", crmf_float_bits_to_f32, 1);
1900
+ rb_define_module_function(rb_cFloat, "bits_to_f64", crmf_float_bits_to_f64, 1);
1901
+ rb_define_module_function(rb_cFloat, "f64_to_bits", crmf_float_f64_to_bits, 1);
1902
+ rb_define_module_function(rb_cFloat, "f32_to_bits", crmf_float_f32_to_bits, -1);
1807
1903
 
1808
1904
  rb_define_method(rb_cFloat, "break_down", crmf_float_break_down, 0);
1809
1905
  rb_define_method(rb_cFloat, "subnormal?", crmf_float_is_subnormal, 0);
1906
+ rb_define_method(rb_cFloat, "to_f32", crmf_float_to_f32, -1);
1810
1907
 
1811
1908
  double d;
1812
1909
 
data/lib/crmf/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module CRMF
2
2
  # @return [String] CRMF gem version
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'.freeze
4
4
  end
metadata CHANGED
@@ -1,19 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crmf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Théotime Bollengier
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-07-22 00:00:00.000000000 Z
10
+ date: 2025-02-07 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: CRMF is a Ruby C extension which provides correctly rounded math functions
14
13
  for Ruby floats, using MPFR and CRlibm. Provided rounding modes are toward zero,
15
14
  +infinity, -infinity a to nearest even.
16
- email: theotime.bollengier@ensta-bretagne.fr
15
+ email: theotime.bollengier@ensta.fr
17
16
  executables: []
18
17
  extensions:
19
18
  - ext/crmf/extconf.rb
@@ -134,9 +133,8 @@ files:
134
133
  - tests/perf.rb
135
134
  homepage: https://gitlab.ensta-bretagne.fr/bollenth/crmf
136
135
  licenses:
137
- - GPL-3.0+
136
+ - GPL-3.0-or-later
138
137
  metadata: {}
139
- post_install_message:
140
138
  rdoc_options: []
141
139
  require_paths:
142
140
  - lib
@@ -144,15 +142,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
142
  requirements:
145
143
  - - ">="
146
144
  - !ruby/object:Gem::Version
147
- version: '0'
145
+ version: '2.7'
148
146
  required_rubygems_version: !ruby/object:Gem::Requirement
149
147
  requirements:
150
148
  - - ">="
151
149
  - !ruby/object:Gem::Version
152
150
  version: '0'
153
151
  requirements: []
154
- rubygems_version: 3.2.3
155
- signing_key:
152
+ rubygems_version: 3.6.2
156
153
  specification_version: 4
157
154
  summary: Correctly rounded math functions for Ruby floats
158
155
  test_files: []