Module: IEEE_FPU

Defined in:
lib/ieee-fpu.rb,
lib/ieee-fpu.rb

Overview

use ieee_fpu extension

Defined Under Namespace

Classes: Error

Constant Summary collapse

Controlfp =
Win32API.new('msvcrt', '_controlfp', 'II', 'I')
MCW_DN =

Denormal control mask

0x03000000
DN_SAVE =
0x00000000
DN_FLUSH =
0x01000000
MCW_EM =

Interrupt exception mask

0x0008001F
EM_INVALID =
0x00000010
EM_DENORMAL =
0x00080000
EM_ZERODIVIDE =
0x00000008
EM_OVERFLOW =
0x00000004
EM_UNDERFLOW =
0x00000002
EM_INEXACT =
0x00000001
MCW_IC =

Infinity control mask

0x00040000
IC_AFFINE =
0x00040000
IC_PROJECTIVE =
0x00000000
MCW_RC =

Rounding control mask

0x00000300
RC_CHOP =
0x00000300
RC_UP =
0x00000200
RC_DOWN =
0x00000100
RC_NEAR =
0x00000000
MCW_PC =

Precision control mask

0x00030000
PC_24 =

24 bits

0x00020000
PC_53 =

53 bits

0x00010000
PC_64 =

64 bits

0x00000000

Class Method Summary collapse

Class Method Details

.get_statusObject



85
86
87
# File 'lib/ieee-fpu.rb', line 85

def self.get_status
  Controlfp.call(0,0)
end

.precisionObject



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ieee-fpu.rb', line 124

def self.precision
  v = get_status & MCW_PC
  p = nil
  case v
    when PC_24
      p = :single
    when PC_53
      p = :double
    when PC_64
      p = :extended
  end
  p
end

.precision=(p) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ieee-fpu.rb', line 137

def self.precision=(p)
  v = nil
  case p
    when :single, 24
      v = PC_24
    when :double, 53
      v = PC_53
    when :extended, 64, 112
      v = PC_64
    else
      raise Error, "Invalid precision #{p.inspect}"
  end
  Controlfp.call(v,MCW_PC) unless v.nil?
  p
end

.roundingObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ieee-fpu.rb', line 153

def self.rounding
  v = get_status & MCW_RC
  r = nil
  case v
    when RC_UP
      r = :up
    when RC_DOWN
      r = :down
    when RC_CHOP
      r = :zero
    when RC_NEAR
      r = :even
  end
  r
end

.rounding=(r) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ieee-fpu.rb', line 169

def self.rounding=(r)
  v = nil
  case r
    when :up, :plus_infinity, :positive_infinity
      v = RC_UP
    when :down, :minus_inifinity, :negative_infinity
      v = RC_DOWN
    when :zero, :truncate, :chop
      v = RC_CHOP
    when :even, :near, :unbiased
      v = RC_NEAR
    else
      raise Error, "Invalid rounding mode #{r}"
    end
  Controlfp.call(v,MCW_RC) unless v.nil?
  r
end

.scope(assignments = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ieee-fpu.rb', line 91

def self.scope(assignments={})
  v = nil
  s = get_status
  prc = assignments[:precision]
  rnd = assignments[:rounding]
  self.precision = prc if prc && !(Array===prc)
  self.rounding =rnd if rnd && !(Array===rnd)
  if Array===prc
    param = assignments[:parameters] || [nil]*prc.size
    v = []
    i = 0
    prc.each do |p|
      self.precision = p
      v << yield(self, param[i])
      i += 1
    end
  elsif Array===rnd
    param = assignments[:parameters] || [nil]*rnd.size
    v = []
    i = 0
    rnd.each do |r|
      self.rounding = r
      v << yield(self, param[i])
      i += 1
    end
  else
    v = yield(self)
  end
  v
  ensure
    set_status s
end

.set_status(s) ⇒ Object



88
89
90
# File 'lib/ieee-fpu.rb', line 88

def self.set_status(s)
  Controlfp.call(s,MCW_DN|MCW_EM|MCW_IC|MCW_RC|MCW_PC)
end

.supported_precisionsObject



187
188
189
# File 'lib/ieee-fpu.rb', line 187

def self.supported_precisions
  [:single, :double, :extended]
end