File: signBit.c

package info (click to toggle)
mlton 20100608-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 36,624 kB
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (85 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download | duplicates (4)
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
#include "platform.h"

#if HAS_SIGNBIT

C_Int_t Real32_signBit (Real32_t f) {
  return signbit (f);
}

C_Int_t Real64_signBit (Real64_t d) {
  return signbit (d);
}

#else

enum {
  LITTLE_R32_byte = 3,
  LITTLE_R64_byte = 7,
};

enum {
  BIG_R32_byte = 0,
  BIG_R64_byte = 0,
};

C_Int_t Real32_signBit (Real32_t f) {
  int R32_byte;
  if (isBigEndian()) {
    R32_byte = BIG_R32_byte;
  } else {
    R32_byte = LITTLE_R32_byte;
  }

  /* Using memcpy. 
   * Technically correct.
   */
  unsigned char chars[4];
  memcpy(chars, &f, sizeof(Real32_t));
  return (chars[R32_byte] & 0x80) >> 7;
  /* Using cast; 
   * Technically correct, as (unsigned char*) may alias.   
   */
  /*
  return (((unsigned char*)(&f))[R32_byte] & 0x80) >> 7;
  */
  /* Using union; 
   * Technically undefined, but widely supported. 
   */
  /*
  union {float f; unsigned char c[4];} fc;
  fc.f = f;
  return (fc.c[R32_byte] & 0x80) >> 7;
  */
}

C_Int_t Real64_signBit (Real64_t d) {
  int R64_byte;
  if (isBigEndian()) {
    R64_byte = BIG_R64_byte;
  } else {
    R64_byte = LITTLE_R64_byte;
  }

  /* Using memcpy. 
   * Technically correct.
   */
  unsigned char chars[8];
  memcpy(chars, &d, sizeof(Real64_t));
  return (chars[R64_byte] & 0x80) >> 7;
  /* Using cast; 
   * Technically correct, as (unsigned char*) may alias.   
   */
  /*
  return (((unsigned char*)(&d))[R64_byte] & 0x80) >> 7;
  */
  /* Using union; 
   * Technically undefined, but widely supported. 
   */
  /*
  union {double d; unsigned char c[8];} dc;
  dc.d = d;
  return (dc.c[R64_byte] & 0x80) >> 7;
  */
}

#endif