1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2010-2011 Marcin Kościelnicki <koriakin@0x04.net> 3bf215546Sopenharmony_ci * Copyright (C) 2010 Francisco Jerez <currojerez@riseup.net> 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 14bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 15bf215546Sopenharmony_ci * Software. 16bf215546Sopenharmony_ci * 17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 24bf215546Sopenharmony_ci */ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#ifndef UTIL_H 27bf215546Sopenharmony_ci#define UTIL_H 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <stdlib.h> 30bf215546Sopenharmony_ci#include <stdio.h> 31bf215546Sopenharmony_ci#include <inttypes.h> 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#define ADDARRAY(a, e) \ 34bf215546Sopenharmony_ci do { \ 35bf215546Sopenharmony_ci if ((a ## num) >= (a ## max)) { \ 36bf215546Sopenharmony_ci if (!(a ## max)) \ 37bf215546Sopenharmony_ci (a ## max) = 16; \ 38bf215546Sopenharmony_ci else \ 39bf215546Sopenharmony_ci (a ## max) *= 2; \ 40bf215546Sopenharmony_ci (a) = realloc((a), (a ## max)*sizeof(*(a))); \ 41bf215546Sopenharmony_ci } \ 42bf215546Sopenharmony_ci (a)[(a ## num)++] = (e); \ 43bf215546Sopenharmony_ci } while(0) 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci#define FINDARRAY(a, tmp, pred) \ 46bf215546Sopenharmony_ci ({ \ 47bf215546Sopenharmony_ci int __i; \ 48bf215546Sopenharmony_ci \ 49bf215546Sopenharmony_ci for (__i = 0; __i < (a ## num); __i++) { \ 50bf215546Sopenharmony_ci tmp = (a)[__i]; \ 51bf215546Sopenharmony_ci if (pred) \ 52bf215546Sopenharmony_ci break; \ 53bf215546Sopenharmony_ci } \ 54bf215546Sopenharmony_ci \ 55bf215546Sopenharmony_ci tmp = ((pred) ? tmp : NULL); \ 56bf215546Sopenharmony_ci }) 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci/* ceil(log2(x)) */ 59bf215546Sopenharmony_cistatic inline int clog2(uint64_t x) { 60bf215546Sopenharmony_ci if (!x) 61bf215546Sopenharmony_ci return x; 62bf215546Sopenharmony_ci int r = 0; 63bf215546Sopenharmony_ci while (x - 1 > (1ull << r) - 1) 64bf215546Sopenharmony_ci r++; 65bf215546Sopenharmony_ci return r; 66bf215546Sopenharmony_ci} 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci#define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a)) 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci#define min(a,b) \ 71bf215546Sopenharmony_ci ({ \ 72bf215546Sopenharmony_ci typeof (a) _a = (a); \ 73bf215546Sopenharmony_ci typeof (b) _b = (b); \ 74bf215546Sopenharmony_ci _a < _b ? _a : _b; \ 75bf215546Sopenharmony_ci }) 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci#define max(a,b) \ 78bf215546Sopenharmony_ci ({ \ 79bf215546Sopenharmony_ci typeof (a) _a = (a); \ 80bf215546Sopenharmony_ci typeof (b) _b = (b); \ 81bf215546Sopenharmony_ci _a > _b ? _a : _b; \ 82bf215546Sopenharmony_ci }) 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci#define CEILDIV(a, b) (((a) + (b) - 1)/(b)) 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci#define extr(a, b, c) ((uint64_t)(a) << (64 - (b) - (c)) >> (64 - (c))) 87bf215546Sopenharmony_ci#define extrs(a, b, c) ((int64_t)(a) << (64 - (b) - (c)) >> (64 - (c))) 88bf215546Sopenharmony_ci#define sext(a, b) extrs(a, 0, b+1) 89bf215546Sopenharmony_ci#define bflmask(a) ((2ull << ((a)-1)) - 1) 90bf215546Sopenharmony_ci#define insrt(a, b, c, d) ((a) = ((a) & ~(bflmask(c) << (b))) | ((d) & bflmask(c)) << (b)) 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_cistruct envy_loc { 93bf215546Sopenharmony_ci int lstart; 94bf215546Sopenharmony_ci int cstart; 95bf215546Sopenharmony_ci int lend; 96bf215546Sopenharmony_ci int cend; 97bf215546Sopenharmony_ci const char *file; 98bf215546Sopenharmony_ci}; 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci#define LOC_FORMAT(loc, str) "%s:%d.%d-%d.%d: " str, (loc).file, (loc).lstart, (loc).cstart, (loc).lend, (loc).cend 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ciuint32_t elf_hash(const char *str); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ciFILE *find_in_path(const char *name, const char *path, char **pfullname); 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_cistruct astr { 107bf215546Sopenharmony_ci char *str; 108bf215546Sopenharmony_ci size_t len; 109bf215546Sopenharmony_ci}; 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_civoid print_escaped_astr(FILE *out, struct astr *astr); 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_cichar *aprintf(const char *format, ...); 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci#endif 116