From: Michal Schmidt <xschmi00@stud.feec.vutbr.cz>
To: "Rafael J. Wysocki" <rjw@sisk.pl>
Cc: linux-pm@lists.osdl.org, Pavel Machek <pavel@ucw.cz>,
linux-kernel@vger.kernel.org
Subject: Re: [linux-pm] [PATCH] swsusp: simpler calculation of number of pages in PBE list
Date: Fri, 29 Jul 2005 23:14:32 +0200 [thread overview]
Message-ID: <42EA9C38.90905@stud.feec.vutbr.cz> (raw)
In-Reply-To: <200507292243.28276.rjw@sisk.pl>
[-- Attachment #1: Type: text/plain, Size: 1261 bytes --]
Rafael J. Wysocki wrote:
> On Friday, 29 of July 2005 21:46, Michal Schmidt wrote:
>
>>The function calc_nr uses an iterative algorithm to calculate the number
>>of pages needed for the image and the pagedir. Exactly the same result
>>can be obtained with a one-line expression.
>
>
> Could you please post the proof?
>
> Rafael
OK, attached is a proof-by-brute-force program. It compares the results
of the original function and the simplified one.
This is its output:
$ ./calc_nr2
checked 0 ...
checked 100000000 ...
checked 200000000 ...
checked 300000000 ...
checked 400000000 ...
checked 500000000 ...
checked 600000000 ...
checked 700000000 ...
checked 800000000 ...
checked 900000000 ...
checked 1000000000 ...
checked 1100000000 ...
checked 1200000000 ...
checked 1300000000 ...
checked 1400000000 ...
checked 1500000000 ...
checked 1600000000 ...
checked 1700000000 ...
checked 1800000000 ...
checked 1900000000 ...
checked 2000000000 ...
checked 2100000000 ...
First difference at 2130706433: -2147483646 x -2147483647
It means that the two functions give the same results for sensible
values of the input argument.
They results only differ when they overflow into negative values. At
this point both of the results are useless.
Michal
[-- Attachment #2: calc_nr2.c --]
[-- Type: text/x-csrc, Size: 986 bytes --]
#include <stdio.h>
#include <limits.h>
typedef struct {
unsigned long val;
} swp_entry_t;
typedef struct pbe {
unsigned long address;
unsigned long orig_address;
swp_entry_t swap_address;
struct pbe *next;
} suspend_pagedir_t;
#define PAGE_SIZE 4096
#define PBES_PER_PAGE (PAGE_SIZE/sizeof(struct pbe))
static int calc_nr_orig(int nr_copy)
{
int extra = 0;
int mod = !!(nr_copy % PBES_PER_PAGE);
int diff = (nr_copy / PBES_PER_PAGE) + mod;
do {
extra += diff;
nr_copy += diff;
mod = !!(nr_copy % PBES_PER_PAGE);
diff = (nr_copy / PBES_PER_PAGE) + mod - extra;
} while (diff > 0);
return nr_copy;
}
static int calc_nr(int nr_copy)
{
return nr_copy + (nr_copy+PBES_PER_PAGE-2)/(PBES_PER_PAGE-1);
}
int main()
{
int i;
for (i=0; i>=0; i++) {
if (i%100000000 == 0)
printf("checked %d ...\n", i);
if (calc_nr(i) != calc_nr_orig(i)) {
printf("First difference at %d: %d x %d\n", i, calc_nr(i), calc_nr_orig(i));
break;
}
}
return 0;
}
next prev parent reply other threads:[~2005-07-29 21:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-29 19:46 [PATCH] swsusp: simpler calculation of number of pages in PBE list Michal Schmidt
2005-07-29 20:43 ` [linux-pm] " Rafael J. Wysocki
2005-07-29 21:14 ` Michal Schmidt [this message]
2005-07-29 21:53 ` Rafael J. Wysocki
2005-07-30 1:35 ` Alan Stern
2005-07-30 13:16 ` Rafael J. Wysocki
2005-07-30 13:13 ` Pavel Machek
2005-07-30 13:32 ` Rafael J. Wysocki
2005-07-30 14:37 ` Alan Stern
2005-07-30 13:20 ` Pavel Machek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=42EA9C38.90905@stud.feec.vutbr.cz \
--to=xschmi00@stud.feec.vutbr.cz \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@lists.osdl.org \
--cc=pavel@ucw.cz \
--cc=rjw@sisk.pl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox