percent.c revision 3984b61df41c68966bdfbb2a5e5a45ef4b9a536c
119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/* 219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * percent.c - Take percentage of a number 319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * 419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Copyright (C) 2006 Theodore Ts'o <tytso@mit.edu> 53984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * 619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * This file can be redistributed under the terms of the GNU Library General 719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Public License 819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */ 919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 1019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "e2p.h" 1119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 1219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdlib.h> 1319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 1419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/* 1519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * We work really hard to calculate this accurately, while avoiding 1619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * an overflow. "Is there a hyphen in anal-retentive?" :-) 1719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */ 1819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectunsigned int e2p_percent(int percent, unsigned int base) 1919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 2019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project unsigned int mask = ~((1 << (sizeof(unsigned int) - 1) * 8) - 1); 2119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 2219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (!percent) 2319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return 0; 2419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (100 % percent == 0) 2519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return base / (100 / percent); 263984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt if (mask & base) 2719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return (base / 100) * percent; 2819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return base * percent / 100; 2919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 3019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 3119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#ifdef DEBUG 3219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <unistd.h> 3319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdio.h> 3419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 3519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectmain(int argc, char **argv) 3619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 3719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project unsigned int base; 3819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project int percent; 3919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char *p; 4019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project int log_block_size = 0; 4119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 4219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (argc != 3) { 4319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf(stderr, "Usage: %s percent base\n", argv[0]); 4419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 4519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 4619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 4719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project percent = strtoul(argv[1], &p, 0); 4819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (p[0] && p[1]) { 4919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf(stderr, "Bad percent: %s\n", argv[1]); 5019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 5119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt 5319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project base = strtoul(argv[2], &p, 0); 5419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (p[0] && p[1]) { 5519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf(stderr, "Bad base: %s\n", argv[2]); 5619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 5719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 5819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 5919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project printf("%d percent of %u is %u.\n", percent, base, 6019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project e2p_percent(percent, base)); 6119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 6219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(0); 6319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 6419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif 65