OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 #ifdef __GNUC__ | 456 #ifdef __GNUC__ |
457 #if (__GNUC__ >= 4) | 457 #if (__GNUC__ >= 4) |
458 #define INLINE(header) inline header __attribute__((always_inline)) | 458 #define INLINE(header) inline header __attribute__((always_inline)) |
459 #else | 459 #else |
460 #define INLINE(header) inline __attribute__((always_inline)) header | 460 #define INLINE(header) inline __attribute__((always_inline)) header |
461 #endif | 461 #endif |
462 #else | 462 #else |
463 #define INLINE(header) inline header | 463 #define INLINE(header) inline header |
464 #endif | 464 #endif |
465 | 465 |
466 // The type-based aliasing rule allows the compiler to assume that pointers of | |
467 // different types (for some definition of different) never alias each other. | |
468 // Thus the following code does not work: | |
469 // | |
470 // float f = foo(); | |
471 // int fbits = *(int*)(&f); | |
472 // | |
473 // The compiler 'knows' that the int pointer can't refer to f since the types | |
474 // don't match, so the compiler may cache f in a register, leaving random data | |
475 // in fbits. Using C++ style casts makes no difference, however a pointer to | |
476 // char data is assumed to alias any other pointer. This is the 'memcpy | |
477 // exception'. | |
478 // | |
479 // Bit_cast uses the memcpy exception to move the bits from a variable of one | |
480 // type o a variable of another type. Of course the end result is likely to | |
481 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) | |
482 // will completely optimize bit_cast away. | |
483 // | |
484 // There is an additional use for bit_cast. | |
485 // Recent gccs will warn when they see casts that may result in breakage due to | |
486 // the type-based aliasing rule. If you have checked that there is no breakage | |
487 // you can use bit_cast to cast one pointer type to another. This confuses gcc | |
488 // enough that it can no longer see that you have cast one pointer type to | |
489 // another thus avoiding the warning. | |
490 template <class Dest, class Source> | |
491 inline Dest bit_cast(const Source& source) { | |
492 // Compile time assertion: sizeof(Dest) == sizeof(Source) | |
493 // A compile error here means your Dest and Source have different sizes. | |
494 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]; | |
495 | |
496 Dest dest; | |
497 memcpy(&dest, &source, sizeof(dest)); | |
498 return dest; | |
499 } | |
500 | |
501 | |
502 } } // namespace v8::internal | 466 } } // namespace v8::internal |
503 | 467 |
504 #endif // V8_GLOBALS_H_ | 468 #endif // V8_GLOBALS_H_ |
OLD | NEW |