int signbit(double x) { union { double d; short s[4]; int i[2]; } u; u.d = x; /* * Tuis is stupid, we test for endianness every time, but that the easiest * way I can see without using platform checks - for scipy 0.8.0, we should * use npy_math */ #if SIZEOF_INT == 4 if (isbigendian()) { return u.i[1] < 0; } else { return u.i[0] < 0; } #else /* SIZEOF_INT != 4 */ if (isbigendian()) { return u.s[3] < 0; } else { return u.s[0] < 0; } #endif /* SIZEOF_INT */ }
int main() { printf("arch_sizeof_short=%d\n",sizeof(short)); printf("arch_sizeof_int=%d\n",sizeof(int)); printf("arch_sizeof_long=%d\n",sizeof(long)); printf("arch_sizeof_long_long=%d\n",sizeof(long long)); printf("arch_sizeof_char_p=%d\n",sizeof(char *)); printf("arch_bigendian=%s\n",isbigendian()); printf("arch_machine="); fflush(stdout); system("uname -m"); system("echo arch_target=`uname -m -p | tr ' ' -`-linux"); return 0; }
// Runtime check of byte ordering, throws if different from isbigendian(). void check_endianness() { union { // Force compile error if int type is not 32bit. unsigned char c[sizeof(unsigned) == 4 ? 4 : -1]; unsigned i; } x = {{1,2,3,4}}; int big = -1; switch (x.i) { case 0x01020304: big = 1; break; case 0x04030201: big = 0; break; } if (big != (isbigendian() ? 1 : 0)) throw std::logic_error("CPU endianness does not match compile time test"); }
Serial_buffer::Serial_buffer(unsigned int maxSize) : m_buffer() , m_valid(false) , m_maxsize(maxSize) , m_pos(0) , m_blen(0) , m_nativeByteOrder((isbigendian()) ? BigEndian : LittleEndian) , m_byteOrder(m_nativeByteOrder) { /* Set the initial function pointers for btyeswapping to be a NOP. */ swap16_ = _nop16; swap32_ = _nop32; swap64_ = _nop64; /* Allocate the buffer. */ m_buffer.vptr = malloc(m_maxsize); m_valid = (m_buffer.vptr != NULL); }
// Note that some versions of Darwin, at least 7H63 and earlier, // have a buggy library that treats the boolean value in // SMARTEnableDisableOperations, SMARTEnableDisableAutosave, and // SMARTExecuteOffLineImmediate as always being true. int ata_command_interface(int fd, smart_command_set command, int select, char *data) { IOATASMARTInterface **ifp = devices[fd].smartIf; IOATASMARTInterface *smartIf; IOReturn err; int timeoutCount = 5; if (! ifp) return -1; smartIf = *ifp; do { switch (command) { case STATUS: return 0; case STATUS_CHECK: { Boolean is_failing; err = smartIf->SMARTReturnStatus (ifp, &is_failing); if (err == kIOReturnSuccess && is_failing) return 1; break; } case ENABLE: case DISABLE: err = smartIf->SMARTEnableDisableOperations (ifp, command == ENABLE); break; case AUTOSAVE: err = smartIf->SMARTEnableDisableAutosave (ifp, select != 0); break; case IMMEDIATE_OFFLINE: if (select != SHORT_SELF_TEST && select != EXTEND_SELF_TEST) { errno = EINVAL; return -1; } err = smartIf->SMARTExecuteOffLineImmediate (ifp, select == EXTEND_SELF_TEST); break; case READ_VALUES: err = smartIf->SMARTReadData (ifp, (ATASMARTData *)data); break; case READ_THRESHOLDS: err = smartIf->SMARTReadDataThresholds (ifp, (ATASMARTDataThresholds *)data); break; case READ_LOG: err = smartIf->SMARTReadLogAtAddress (ifp, select, data, 512); break; case WRITE_LOG: err = smartIf->SMARTWriteLogAtAddress (ifp, select, data, 512); break; case IDENTIFY: { UInt32 dummy; err = smartIf->GetATAIdentifyData (ifp, data, 512, &dummy); if (err != kIOReturnSuccess && err != kIOReturnTimeout && err != kIOReturnNotResponding) printf ("identify failed: %#x\n", (unsigned) err); if (err == kIOReturnSuccess && isbigendian()) { int i; /* The system has already byte-swapped, undo it. */ for (i = 0; i < 256; i+=2) swap2 (data + i); } } break; case CHECK_POWER_MODE: // The information is right there in the device registry, but how // to get to it portably? default: errno = ENOTSUP; return -1; } /* This bit is a bit strange. Apparently, when the drive is spun down, the intended behaviour of these calls is that they fail, return kIOReturnTimeout and then power the drive up. So if you get a timeout, you have to try again to get the actual command run, but the drive is already powering up so you can't use this for CHECK_POWER_MODE. */ if (err == kIOReturnTimeout || err == kIOReturnNotResponding) sleep (1); } while ((err == kIOReturnTimeout || err == kIOReturnNotResponding) && timeoutCount-- > 0); if (err == kIOReturnExclusiveAccess) errno = EBUSY; return err == kIOReturnSuccess ? 0 : -1; }