| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // align.hfa --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Mon Nov 28 12:27:26 2016
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Sat Nov 16 18:58:22 2019
 | 
|---|
| 13 | // Update Count     : 3
 | 
|---|
| 14 | //
 | 
|---|
| 15 | // This  library is free  software; you  can redistribute  it and/or  modify it
 | 
|---|
| 16 | // under the terms of the GNU Lesser General Public License as published by the
 | 
|---|
| 17 | // Free Software  Foundation; either  version 2.1 of  the License, or  (at your
 | 
|---|
| 18 | // option) any later version.
 | 
|---|
| 19 | //
 | 
|---|
| 20 | // This library is distributed in the  hope that it will be useful, but WITHOUT
 | 
|---|
| 21 | // ANY  WARRANTY;  without even  the  implied  warranty  of MERCHANTABILITY  or
 | 
|---|
| 22 | // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 | 
|---|
| 23 | // for more details.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // You should  have received a  copy of the  GNU Lesser General  Public License
 | 
|---|
| 26 | // along  with this library.
 | 
|---|
| 27 | //
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #pragma once
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include <assert.h>
 | 
|---|
| 32 | #include <stdbool.h>
 | 
|---|
| 33 | 
 | 
|---|
| 34 | // Minimum size used to align memory boundaries for memory allocations.
 | 
|---|
| 35 | //#define libAlign() (sizeof(double))
 | 
|---|
| 36 | // gcc-7 uses xmms instructions, which require 16 byte alignment.
 | 
|---|
| 37 | #define libAlign() (16)
 | 
|---|
| 38 | 
 | 
|---|
| 39 | // Check for power of 2
 | 
|---|
| 40 | static inline bool libPow2( unsigned long int value ) {
 | 
|---|
| 41 |     // clears all bits below value, rounding value down to the next lower multiple of value
 | 
|---|
| 42 |     return (value & (value - 1ul)) == 0ul;
 | 
|---|
| 43 | } // libPow2
 | 
|---|
| 44 | 
 | 
|---|
| 45 | 
 | 
|---|
| 46 | // Returns value aligned at the floor of align.
 | 
|---|
| 47 | static inline unsigned long int libFloor( unsigned long int value, unsigned long int align ) {
 | 
|---|
| 48 |     assert( libPow2( align ) );
 | 
|---|
| 49 |     // clears all bits above or equal to align, getting (value % align), the phase of value with regards to align
 | 
|---|
| 50 |     return value & -align;
 | 
|---|
| 51 | } // libFloor
 | 
|---|
| 52 | 
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // Returns value aligned at the ceiling of align.
 | 
|---|
| 55 | 
 | 
|---|
| 56 | static inline unsigned long int libCeiling( unsigned long int value, unsigned long int align ) {
 | 
|---|
| 57 |     assert( libPow2( align ) );
 | 
|---|
| 58 |     // "negate, round down, negate" is the same as round up
 | 
|---|
| 59 |     return -libFloor( -value, align );
 | 
|---|
| 60 | } // uCeiling
 | 
|---|
| 61 | 
 | 
|---|
| 62 | // Local Variables: //
 | 
|---|
| 63 | // compile-command: "make install" //
 | 
|---|
| 64 | // End: //
 | 
|---|