1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::{i32, i64, u32, u64, f32};
use std::io;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use interpreter::Error;
use interpreter::variable::VariableType;

/// Runtime value.
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeValue {
	/// Null value.
	Null,
	/// Reference to the function in the given module' function index space.
	AnyFunc(String, u32),
	/// 32b-length signed/unsigned int.
	I32(i32),
	/// 64b-length signed/unsigned int.
	I64(i64),
	/// 32b-length float.
	F32(f32),
	/// 64b-length float.
	F64(f64),
}

/// Try to convert into trait.
pub trait TryInto<T, E> {
	/// Try to convert self into other value.
	fn try_into(self) -> Result<T, E>;
}

/// Convert one type to another by wrapping.
pub trait WrapInto<T> {
	/// Convert one type to another by wrapping.
	fn wrap_into(self) -> T;
}

/// Convert one type to another by rounding to the nearest integer towards zero.
pub trait TryTruncateInto<T, E> {
	/// Convert one type to another by rounding to the nearest integer towards zero.
	fn try_truncate_into(self) -> Result<T, E>;
}

/// Convert one type to another by extending with leading zeroes.
pub trait ExtendInto<T> {
	/// Convert one type to another by extending with leading zeroes.
	fn extend_into(self) -> T;
}

/// Reinterprets the bits of a value of one type as another type.
pub trait TransmuteInto<T> {
	/// Reinterprets the bits of a value of one type as another type.
	fn transmute_into(self) -> T;
}

/// Convert from and to little endian.
pub trait LittleEndianConvert where Self: Sized {
	/// Convert to little endian buffer.
	fn into_little_endian(self) -> Vec<u8>;
	/// Convert from little endian buffer.
	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error>;
}

/// Arithmetic operations.
pub trait ArithmeticOps<T> {
	/// Add two values.
	fn add(self, other: T) -> T;
	/// Subtract two values.
	fn sub(self, other: T) -> T;
	/// Multiply two values.
	fn mul(self, other: T) -> T;
	/// Divide two values.
	fn div(self, other: T) -> Result<T, Error>;
}

/// Integer value.
pub trait Integer<T>: ArithmeticOps<T> {
	/// Counts leading zeros in the bitwise representation of the value.
	fn leading_zeros(self) -> T;
	/// Counts trailing zeros in the bitwise representation of the value.
	fn trailing_zeros(self) -> T;
	/// Counts 1-bits in the bitwise representation of the value.
	fn count_ones(self) -> T;
	/// Get left bit rotation result.
	fn rotl(self, other: T) -> T;
	/// Get right bit rotation result.
	fn rotr(self, other: T) -> T;
	/// Get division remainder.
	fn rem(self, other: T) -> Result<T, Error>;
}

/// Float-point value.
pub trait Float<T>: ArithmeticOps<T> {
	/// Get absolute value.
	fn abs(self) -> T;
	/// Returns the largest integer less than or equal to a number.
	fn floor(self) -> T;
	/// Returns the smallest integer greater than or equal to a number.
	fn ceil(self) -> T;
	/// Returns the integer part of a number.
	fn trunc(self) -> T;
	/// Returns the nearest integer to a number. Round half-way cases away from 0.0.
	fn round(self) -> T;
	/// Returns the nearest integer to a number. Ties are round to even number.
	fn nearest(self) -> T;
	/// Takes the square root of a number.
	fn sqrt(self) -> T;
	/// Returns the minimum of the two numbers.
	fn min(self, other: T) -> T;
	/// Returns the maximum of the two numbers.
	fn max(self, other: T) -> T;
	/// Sets sign of this value to the sign of other value.
	fn copysign(self, other: T) -> T;
}

impl RuntimeValue {
	/// Creates new default value of given type.
	pub fn default(variable_type: VariableType) -> Self {
		match variable_type {
			VariableType::AnyFunc => RuntimeValue::AnyFunc("".into(), 0),
			VariableType::I32 => RuntimeValue::I32(0),
			VariableType::I64 => RuntimeValue::I64(0),
			VariableType::F32 => RuntimeValue::F32(0f32),
			VariableType::F64 => RuntimeValue::F64(0f64),
		}
	}

	/// Creates new value by interpreting passed u32 as f32.
	pub fn decode_f32(val: u32) -> Self {
		RuntimeValue::F32(f32_from_bits(val))
	}

	/// Creates new value by interpreting passed u64 as f64.
	pub fn decode_f64(val: u64) -> Self {
		RuntimeValue::F64(f64_from_bits(val))
	}

	/// Returns true if value is null.
	pub fn is_null(&self) -> bool {
		match *self {
			RuntimeValue::Null => true,
			_ => false,
		}
	}

	/// Get variable type for this value.
	pub fn variable_type(&self) -> Option<VariableType> {
		match *self {
			RuntimeValue::Null => None,
			RuntimeValue::AnyFunc(_, _) => Some(VariableType::AnyFunc),
			RuntimeValue::I32(_) => Some(VariableType::I32),
			RuntimeValue::I64(_) => Some(VariableType::I64),
			RuntimeValue::F32(_) => Some(VariableType::F32),
			RuntimeValue::F64(_) => Some(VariableType::F64),
		}
	}
}

impl From<i32> for RuntimeValue {
	fn from(val: i32) -> Self {
		RuntimeValue::I32(val)
	}
}

impl From<i64> for RuntimeValue {
	fn from(val: i64) -> Self {
		RuntimeValue::I64(val)
	}
}

impl From<f32> for RuntimeValue {
	fn from(val: f32) -> Self {
		RuntimeValue::F32(val)
	}
}

impl From<f64> for RuntimeValue {
	fn from(val: f64) -> Self {
		RuntimeValue::F64(val)
	}
}

impl TryInto<bool, Error> for RuntimeValue {
	fn try_into(self) -> Result<bool, Error> {
		match self {
			RuntimeValue::I32(val) => Ok(val != 0),
			_ => Err(Error::Value(format!("32-bit int value expected"))),
		}
	}
}

impl TryInto<i32, Error> for RuntimeValue {
	fn try_into(self) -> Result<i32, Error> {
		match self {
			RuntimeValue::I32(val) => Ok(val),
			_ => Err(Error::Value(format!("32-bit int value expected"))),
		}
	}
}

impl TryInto<i64, Error> for RuntimeValue {
	fn try_into(self) -> Result<i64, Error> {
		match self {
			RuntimeValue::I64(val) => Ok(val),
			_ => Err(Error::Value(format!("64-bit int value expected"))),
		}
	}
}

impl TryInto<f32, Error> for RuntimeValue {
	fn try_into(self) -> Result<f32, Error> {
		match self {
			RuntimeValue::F32(val) => Ok(val),
			_ => Err(Error::Value(format!("32-bit float value expected"))),
		}
	}
}

impl TryInto<f64, Error> for RuntimeValue {
	fn try_into(self) -> Result<f64, Error> {
		match self {
			RuntimeValue::F64(val) => Ok(val),
			_ => Err(Error::Value(format!("64-bit float value expected"))),
		}
	}
}

impl TryInto<u32, Error> for RuntimeValue {
	fn try_into(self) -> Result<u32, Error> {
		match self {
			RuntimeValue::I32(val) => Ok(val as u32),
			_ => Err(Error::Value(format!("32-bit int value expected"))),
		}
	}
}

impl TryInto<u64, Error> for RuntimeValue {
	fn try_into(self) -> Result<u64, Error> {
		match self {
			RuntimeValue::I64(val) => Ok(val as u64),
			_ => Err(Error::Value(format!("64-bit int value expected"))),
		}
	}
}

macro_rules! impl_wrap_into {
	($from: ident, $into: ident) => {
		impl WrapInto<$into> for $from {
			fn wrap_into(self) -> $into {
				self as $into
			}
		}
	}
}

impl_wrap_into!(i32, i8);
impl_wrap_into!(i32, i16);
impl_wrap_into!(i64, i8);
impl_wrap_into!(i64, i16);
impl_wrap_into!(i64, i32);
impl_wrap_into!(i64, f32);
impl_wrap_into!(u64, f32);
// Casting from an f64 to an f32 will produce the closest possible value (rounding strategy unspecified)
// NOTE: currently this will cause Undefined Behavior if the value is finite but larger or smaller than the
// largest or smallest finite value representable by f32. This is a bug and will be fixed.
impl_wrap_into!(f64, f32);

macro_rules! impl_try_truncate_into {
	($from: ident, $into: ident) => {
		impl TryTruncateInto<$into, Error> for $from {
			fn try_truncate_into(self) -> Result<$into, Error> {
				// Casting from a float to an integer will round the float towards zero
				// NOTE: currently this will cause Undefined Behavior if the rounded value cannot be represented by the
				// target integer type. This includes Inf and NaN. This is a bug and will be fixed.
				if self.is_nan() || self.is_infinite() {
					return Err(Error::Value("invalid float value for this operation".into()));
				}

				// range check
				let result = self as $into;
				if result as $from != self.trunc() {
					return Err(Error::Value("invalid float value for this operation".into()));
				}

				Ok(self as $into)
			}
		}
	}
}

impl_try_truncate_into!(f32, i32);
impl_try_truncate_into!(f32, i64);
impl_try_truncate_into!(f64, i32);
impl_try_truncate_into!(f64, i64);
impl_try_truncate_into!(f32, u32);
impl_try_truncate_into!(f32, u64);
impl_try_truncate_into!(f64, u32);
impl_try_truncate_into!(f64, u64);

macro_rules! impl_extend_into {
	($from: ident, $into: ident) => {
		impl ExtendInto<$into> for $from {
			fn extend_into(self) -> $into {
				self as $into
			}
		}
	}
}

impl_extend_into!(i8, i32);
impl_extend_into!(u8, i32);
impl_extend_into!(i16, i32);
impl_extend_into!(u16, i32);
impl_extend_into!(i8, i64);
impl_extend_into!(u8, i64);
impl_extend_into!(i16, i64);
impl_extend_into!(u16, i64);
impl_extend_into!(i32, i64);
impl_extend_into!(u32, i64);
impl_extend_into!(u32, u64);
impl_extend_into!(i32, f32);
impl_extend_into!(i32, f64);
impl_extend_into!(u32, f32);
impl_extend_into!(u32, f64);
impl_extend_into!(i64, f64);
impl_extend_into!(u64, f64);
impl_extend_into!(f32, f64);

macro_rules! impl_transmute_into_self {
	($type: ident) => {
		impl TransmuteInto<$type> for $type {
			fn transmute_into(self) -> $type {
				self
			}
		}
	}
}

impl_transmute_into_self!(i32);
impl_transmute_into_self!(i64);
impl_transmute_into_self!(f32);
impl_transmute_into_self!(f64);

macro_rules! impl_transmute_into_as {
	($from: ident, $into: ident) => {
		impl TransmuteInto<$into> for $from {
			fn transmute_into(self) -> $into {
				self as $into
			}
		}
	}
}

impl_transmute_into_as!(i8, u8);
impl_transmute_into_as!(u8, i8);
impl_transmute_into_as!(i32, u32);
impl_transmute_into_as!(u32, i32);
impl_transmute_into_as!(i64, u64);
impl_transmute_into_as!(u64, i64);

// TODO: rewrite these safely when `f32/f32::to_bits/from_bits` stabilized.
impl TransmuteInto<i32> for f32 {
	fn transmute_into(self) -> i32 { unsafe { ::std::mem::transmute(self) } }
}

impl TransmuteInto<i64> for f64 {
	fn transmute_into(self) -> i64 { unsafe { ::std::mem::transmute(self) } }
}

impl TransmuteInto<f32> for i32 {
	fn transmute_into(self) -> f32 { f32_from_bits(self as _) }
}

impl TransmuteInto<f64> for i64 {
	fn transmute_into(self) -> f64 { f64_from_bits(self as _) }
}

impl LittleEndianConvert for i8 {
	fn into_little_endian(self) -> Vec<u8> {
		vec![self as u8]
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		buffer.get(0)
			.map(|v| *v as i8)
			.ok_or(Error::Value("invalid little endian buffer".into()))
	}
}

impl LittleEndianConvert for u8 {
	fn into_little_endian(self) -> Vec<u8> {
		vec![self]
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		buffer.get(0)
			.cloned()
			.ok_or(Error::Value("invalid little endian buffer".into()))
	}
}

impl LittleEndianConvert for i16 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(2);
		vec.write_i16::<LittleEndian>(self)
			.expect("i16 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_i16::<LittleEndian>()
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for u16 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(2);
		vec.write_u16::<LittleEndian>(self)
			.expect("u16 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_u16::<LittleEndian>()
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for i32 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(4);
		vec.write_i32::<LittleEndian>(self)
			.expect("i32 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_i32::<LittleEndian>()
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for u32 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(4);
		vec.write_u32::<LittleEndian>(self)
			.expect("u32 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_u32::<LittleEndian>()
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for i64 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(8);
		vec.write_i64::<LittleEndian>(self)
			.expect("i64 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_i64::<LittleEndian>()
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for f32 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(4);
		vec.write_f32::<LittleEndian>(self)
			.expect("f32 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_u32::<LittleEndian>()
			.map(f32_from_bits)
			.map_err(|e| Error::Value(e.to_string()))
	}
}

impl LittleEndianConvert for f64 {
	fn into_little_endian(self) -> Vec<u8> {
		let mut vec = Vec::with_capacity(8);
		vec.write_f64::<LittleEndian>(self)
			.expect("i64 is written without any errors");
		vec
	}

	fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
		io::Cursor::new(buffer).read_u64::<LittleEndian>()
			.map(f64_from_bits)
			.map_err(|e| Error::Value(e.to_string()))
	}
}

// Convert u32 to f32 safely, masking out sNAN
fn f32_from_bits(mut v: u32) -> f32 {
	const EXP_MASK: u32   = 0x7F800000;
	const QNAN_MASK: u32  = 0x00400000;
	const FRACT_MASK: u32 = 0x007FFFFF;

	if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
		// If we have a NaN value, we
		// convert signaling NaN values to quiet NaN
		// by setting the the highest bit of the fraction
		// TODO: remove when https://github.com/BurntSushi/byteorder/issues/71 closed.
		// or `f32::from_bits` stabilized.
		v |= QNAN_MASK;
	}

	unsafe { ::std::mem::transmute(v) }
}

// Convert u64 to f64 safely, masking out sNAN
fn f64_from_bits(mut v: u64) -> f64 {
	const EXP_MASK: u64   = 0x7FF0000000000000;
	const QNAN_MASK: u64  = 0x0001000000000000;
	const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;

	if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
		// If we have a NaN value, we
		// convert signaling NaN values to quiet NaN
		// by setting the the highest bit of the fraction
		// TODO: remove when https://github.com/BurntSushi/byteorder/issues/71 closed.
		// or `f64::from_bits` stabilized.
		v |= QNAN_MASK;
	}

	unsafe { ::std::mem::transmute(v) }
}

macro_rules! impl_integer_arithmetic_ops {
	($type: ident) => {
		impl ArithmeticOps<$type> for $type {
			fn add(self, other: $type) -> $type { self.wrapping_add(other) }
			fn sub(self, other: $type) -> $type { self.wrapping_sub(other) }
			fn mul(self, other: $type) -> $type { self.wrapping_mul(other) }
			fn div(self, other: $type) -> Result<$type, Error> {
				if other == 0 { Err(Error::Value("Division by zero".to_owned())) }
				else {
					let (result, overflow) = self.overflowing_div(other);
					if overflow {
						return Err(Error::Value("Attempt to divide with overflow".to_owned()));
					} else {
						Ok(result)
					}
				}
			}
		}
	}
}

impl_integer_arithmetic_ops!(i32);
impl_integer_arithmetic_ops!(u32);
impl_integer_arithmetic_ops!(i64);
impl_integer_arithmetic_ops!(u64);

macro_rules! impl_float_arithmetic_ops {
	($type: ident) => {
		impl ArithmeticOps<$type> for $type {
			fn add(self, other: $type) -> $type { self + other }
			fn sub(self, other: $type) -> $type { self - other }
			fn mul(self, other: $type) -> $type { self * other }
			fn div(self, other: $type) -> Result<$type, Error> { Ok(self / other) }
		}
	}
}

impl_float_arithmetic_ops!(f32);
impl_float_arithmetic_ops!(f64);

macro_rules! impl_integer {
	($type: ident) => {
		impl Integer<$type> for $type {
			fn leading_zeros(self) -> $type { self.leading_zeros() as $type }
			fn trailing_zeros(self) -> $type { self.trailing_zeros() as $type }
			fn count_ones(self) -> $type { self.count_ones() as $type }
			fn rotl(self, other: $type) -> $type { self.rotate_left(other as u32) }
			fn rotr(self, other: $type) -> $type { self.rotate_right(other as u32) }
			fn rem(self, other: $type) -> Result<$type, Error> {
				if other == 0 { Err(Error::Value("Division by zero".to_owned())) }
				else { Ok(self.wrapping_rem(other)) }
			}
		}
	}
}

impl_integer!(i32);
impl_integer!(u32);
impl_integer!(i64);
impl_integer!(u64);

macro_rules! impl_float {
	($type: ident, $int_type: ident) => {
		impl Float<$type> for $type {
			fn abs(self) -> $type { self.abs() }
			fn floor(self) -> $type { self.floor() }
			fn ceil(self) -> $type { self.ceil() }
			fn trunc(self) -> $type { self.trunc() }
			fn round(self) -> $type { self.round() }
			fn nearest(self) -> $type {
				let round = self.round();
				if self.fract().abs() != 0.5 {
					return round;
				}

				use std::ops::Rem;
				if round.rem(2.0) == 1.0 {
					self.floor()
				} else if round.rem(2.0) == -1.0 {
					self.ceil()
				} else {
					round
				}
			}
			fn sqrt(self) -> $type { self.sqrt() }
			// This instruction corresponds to what is sometimes called "minNaN" in other languages.
			fn min(self, other: $type) -> $type {
				if self.is_nan() || other.is_nan() {
					use std::$type;
					return $type::NAN;
				}

				self.min(other)
			}
			// This instruction corresponds to what is sometimes called "maxNaN" in other languages.
			fn max(self, other: $type) -> $type {
				if self.is_nan() || other.is_nan() {
					use std::$type;
					return $type::NAN;
				}

				self.max(other)
			}
			fn copysign(self, other: $type) -> $type {
				use std::mem::size_of;

				if self.is_nan() {
					return self;
				}

				let sign_mask: $int_type = 1 << ((size_of::<$int_type>() << 3) - 1);
				let self_int: $int_type = self.transmute_into();
				let other_int: $int_type = other.transmute_into();
				let is_self_sign_set = (self_int & sign_mask) != 0;
				let is_other_sign_set = (other_int & sign_mask) != 0;
				if is_self_sign_set == is_other_sign_set {
					self
				} else if is_other_sign_set {
					(self_int | sign_mask).transmute_into()
				} else {
					(self_int & !sign_mask).transmute_into()
				}
			}
		}
	}
}

impl_float!(f32, i32);
impl_float!(f64, i64);